我無法回答你所有的問題,因爲我沒有使用TFS的經驗。
但是我可以推薦一個比使用AssemblyInfo任務更好的方法來更新您的AssemblyInfo.cs文件。該任務似乎只是從頭開始重新創建標準AssemblyInfo文件,並丟失了您可能添加的任何自定義部分。
因此,我建議您查看一下MSBuild社區任務項目中的FileUpdate任務。它可以尋找特定的內容在文件中,並取代它,像這樣:
<FileUpdate
Files="$(WebDir)\Properties\AssemblyInfo.cs"
Regex="(\d+)\.(\d+)\.(\d+)\.(\d+)"
ReplacementText="$(Major).$(ServicePack).$(Build).$(Revision)"
Condition="'$(Configuration)' == 'Release'"
/>
有幾種方法可以控制內部版本號的遞增。因爲我只希望構建號碼增加,如果構建完全成功,我使用兩步法:
- 從文本文件中讀取一個數字(文件中唯一的數字)並添加1而不更改文件;
- 作爲構建過程的最後一步,如果一切都成功了,請將增加的數字保存迴文本文件。
有任務,如ReadLinesFromFile,可以幫助你這一點,但我發現它最容易寫一個小的自定義任務:
using System;
using System.IO;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
namespace CredibleCustomBuildTasks
{
public class IncrementTask : Task
{
[Required]
public bool SaveChange { get; set; }
[Required]
public string IncrementFileName { get; set; }
[Output]
public int Increment { get; set; }
public override bool Execute()
{
if (File.Exists(IncrementFileName))
{
string lines = File.ReadAllText(IncrementFileName);
int result;
if(Int32.TryParse(lines, out result))
{
Increment = result + 1;
}
else
{
Log.LogError("Unable to parse integer in '{0}' (contents of {1})");
return false;
}
}
else
{
Increment = 1;
}
if (SaveChange)
{
File.Delete(IncrementFileName);
File.WriteAllText(IncrementFileName, Increment.ToString());
}
return true;
}
}
}
我用這個FileUpdateTask之前得到的未來建設數量:
<IncrementTask
IncrementFileName="$(BuildNumberFile)"
SaveChange="false">
<Output TaskParameter="Increment" PropertyName="Build" />
</IncrementTask>
和我的最後一步,在構建(通知其他前):
<IncrementTask
IncrementFileName="$(BuildNumberFile)"
SaveChange="true"
Condition="'$(Configuration)' == 'Release'" />
如何更新,只有當源代碼已經改變了版本號你的另一個問題是高度依賴於您的構建過程中你是如何與你的源代碼控制交互。通常,檢入源文件更改應啓動持續集成構建。這是用來更新相關版本號的人。