2011-03-15 52 views

回答

8

你會想考慮某種類型的系統來管理你的版本增量。一種常見的做法是通過持續集成,例如CruiseControl.NET。如果你走這條路線,你可以使用一個構建的目標是這樣的:

<target name="set.version" description="generates the version number"> 
    <echo message="Setting the build version to ${CCNetLabel}..." /> 
    <attrib file="AssemblyInfo.cs" readonly="false" /> 
    <asminfo output="AssemblyInfo.cs" language="CSharp"> 
     <imports> 
      <import namespace="System" /> 
      <import namespace="System.Reflection" /> 
     </imports> 
     <attributes> 
      <attribute type="AssemblyVersionAttribute" value="${CCNetLabel}" /> 
      <attribute type="AssemblyFileVersionAttribute" value="${CCNetLabel}" /> 
     </attributes> 
    </asminfo> 
    <attrib file="AssemblyInfo.cs" readonly="true" /> 
</target> 

哪裏CCNetLabel是從CruiseControl的設置,當它執行楠動態屬性。

+1

這是真棒!但我不太明白何時在腳本中實現這一點?我應該在BUILD/COMPILE實際發生之前執行此操作嗎?這個例子只是產生一個「AssemblyInfo.cs」文件。我怎樣才能讓它在我的解決方案中使用現有的AssemblyInfo.cs文件?我有很多。 – D3vtr0n 2011-03-21 21:20:37

+1

它應該在編譯目標發生之前完成。在包含許多現有AssemblyInfo.cs文件的解決方案中使用此方法的最佳方式是,將解決方案(即AssemblyVersion,Company等)的版本屬性和其他常見信息隔離到包含在所有文件中的「GlobalAssemblyInfo.cs」文件中您的解決方案的項目。您可以修改上面的asminfo任務來修改該文件。 – 2011-03-21 21:47:09

+0

^完全有效。謝謝! – D3vtr0n 2011-03-24 14:21:40

3

我們使用TeamCity爲NAnt提供版本號。然後,將版本號注入的AssemblyInfo這樣.:

<asminfo output="${solutionDir}/CommonAssemblyInfo.cs" language="CSharp"> 
     <imports> 
     <import namespace="System" /> 
     <import namespace="System.Reflection" /> 
     </imports> 
     <attributes> 
     <attribute type="AssemblyVersionAttribute" value="${version}" /> 
     </attributes> 
    </asminfo> 

這將創建一個CommonAssemblyInfo.cs與指定的版本,這需要鏈接到您的解決方案的所有項目文件。

2

我使用多個引用的項目(Windows窗體,類庫和BatchConsole)

最好的例子是在「集信息的部分」,從南特複製生成文件(你可以從Github下載)

技巧是你可以使用你的nAnt目標將引用它的commonAssemblyinfo文件。

下面從惡性文件的目標

<target name="create-common-assemblyinfo" if="${create.assemblyinfo}"> 
    <!-- ensure src/CommonAssemblyInfo.cs is writable if it already exists --> 
    <attrib file="src/CommonAssemblyInfo.cs" readonly="false" if="${file::exists('src/CommonAssemblyInfo.cs')}" /> 
    <!-- generate the source file holding the common assembly-level attributes --> 
    <asminfo output="src/CommonAssemblyInfo.cs" language="CSharp"> 
     <imports> 
      <import namespace="System" /> 
      <import namespace="System.Reflection" /> 
      <import namespace="System.Runtime.InteropServices" /> 
     </imports> 
     <attributes> 
      <attribute type="ComVisibleAttribute" value="false" /> 
      <attribute type="CLSCompliantAttribute" value="true" /> 
      <attribute type="AssemblyTitleAttribute" value="NAnt" /> 
      <attribute type="AssemblyDescriptionAttribute" value="A .NET Build Tool" /> 
      <attribute type="AssemblyConfigurationAttribute" value="${project.release.type}" /> 
      <attribute type="AssemblyCompanyAttribute" value="http://nant.sourceforge.net" /> 
      <attribute type="AssemblyProductAttribute" value="NAnt" /> 
      <attribute type="AssemblyCopyrightAttribute" value="Copyright (C) 2001-${datetime::get-year(datetime::now())} Gerry Shaw" /> 
      <attribute type="AssemblyTrademarkAttribute" value="" /> 
      <attribute type="AssemblyCultureAttribute" value="" /> 
      <attribute type="AssemblyVersionAttribute" value="${project.version}.${build.number}.0" /> 
      <attribute type="AssemblyInformationalVersionAttribute" value="${project.version}" /> 
     </attributes> 
    </asminfo> 
</target>