Put build number in web.config
I'm trying to get my build number into 开发者_运维技巧the web.config of an ASP.Net MVC project (long story, but there is a reason)... something like this:
<appSettings>
<add key="version" value="1.0.3936.27150" />
</appSettings>
Right now, I'm doing it with an executable on the post-build event by looking at the assembly to get the version and stuff that into the web.config. It works, but isn't exactly what I was looking for.
Any easy way to do this easier / cleaner? Thanks for any advice / suggestions.
You have a few options here, the first being the one you picked, which is old skewl. The next option would be to use a Visual Studio Macro... this will only work in Visual Studio and on the system that it was configured on. The next two options would be the best. Use an MSBuild script as a post build event... or a NAnt script as a post build event to make the mod. I would lean in favor of the NAnt my self, but then I have a black belt in NAnt. :-)
I had to also place the build number in the web.config before deploying the project. I followed the tutorial here http://www.ewaldhofman.nl/post/2010/05/13/Customize-Team-Build-2010-e28093-Part-5-Increase-AssemblyVersion.aspx.
Since the above tutorial did not exactly show me how to modify the web.config I had to modify the custom activity (code below):
[BuildActivity(HostEnvironmentOption.All)]
public sealed class UpdateWebConfigWithBuild : CodeActivity {
// Define the activity arguments of type string
[RequiredArgument]
public InArgument<IBuildDetail> BuildDetail { get; set; }
// The SourcesDirectory as initialized in the Build Process Template
[RequiredArgument]
public InArgument<string> SourcesDirectory { get; set; }
// The SourcesDirectory as initialized in the Build Process Template
[RequiredArgument]
public InArgument<string> TFSBuildNumberAppSettingName { get; set; }
public OutArgument<string> TextOut { get; set; }
// If your activity returns a value, derive from CodeActivity<TResult>
// and return the value from the Execute method.
protected override void Execute(CodeActivityContext context) {
// Obtain the runtime value of the Text input argument
// Contains the build number
IBuildDetail buildDetail = context.GetValue(this.BuildDetail);
// The TFS directoty
string sourcesDirectory = context.GetValue(this.SourcesDirectory);
// web config app setting
string TFSBuildNumberSetting = context.GetValue(this.TFSBuildNumberAppSettingName);
// output String
string output = String.Empty;
// Get all AssemblyInfo files
foreach (string file in Directory.EnumerateFiles(sourcesDirectory,"web.config", SearchOption.AllDirectories)) {
// IS tag founds
bool settingFound = false;
FileAttributes attributes = File.GetAttributes(file);
// Remove readonly attribute
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly) {
//Remove readonly
File.SetAttributes(file,attributes^FileAttributes.ReadOnly);
}
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(file);
// iterate through all the app settings to find the TFS BUILD
foreach (XmlNode node in xmlDoc.SelectNodes("/configuration/appSettings/add")) {
// MATCH
if (node.Attributes.GetNamedItem("key") != null && node.Attributes.GetNamedItem("value") != null && node.Attributes.GetNamedItem("key").Value.Equals("TFSBuildNumber")) {
node.Attributes.GetNamedItem("value").Value = buildDetail.BuildNumber;
settingFound = true;
output += "MODIFIED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
break;
}
}
// if the app setting is not found then add it
if (!settingFound) {
XmlNode node = xmlDoc.SelectSingleNode("/configuration/appSettings");
if (null != node) {
XmlElement elem = xmlDoc.CreateElement("add");
XmlAttribute attrKey = xmlDoc.CreateAttribute("key");
attrKey.Value = "TFSBuildNumber";
XmlAttribute attrVal = xmlDoc.CreateAttribute("value");
attrVal.Value = buildDetail.BuildNumber;
elem.Attributes.Append(attrKey);
elem.Attributes.Append(attrVal);
node.AppendChild(elem);
output += "ADDED with BuildNumber " + buildDetail.BuildNumber.ToString() + " :" + file + "\n";
}
}
// Save file
xmlDoc.Save(file);
//add readonly attribute back to web.config file
File.SetAttributes(file,attributes | FileAttributes.ReadOnly);
}
// Set the values
context.SetValue<string>(this.TextOut,output);
}
精彩评论