Nant <version> 任务

4

如何使用nant任务来增加构建版本?更具体地说,我怎样将此与assemblyinfo.cs中的版本号相关联?

4个回答

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执行nant时设置。

1
这太棒了!但我不太明白何时在我的脚本中实现它?我应该在BUILD/COMPILE之前执行吗?这个例子只是生成一个“AssemblyInfo.cs”文件。如何让它使用我解决方案中现有的AssemblyInfo.cs文件?我有很多。 - D3vtr0n
1
应该在编译目标发生之前完成。在具有许多现有的AssemblyInfo.cs文件的解决方案中使用最佳方法是将版本属性和其他公共信息(即AssemblyVersion、Company等)隔离到一个名为“GlobalAssemblyInfo.cs”的文件中,该文件包含在解决方案的所有项目中。您可以修改上面的asminfo任务以修改该文件。 - Saul Dolgin

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文件,其中包含指定的版本号,需要将其链接到解决方案中的所有项目。


3

2
我正在使用多个引用的项目(Windows Forms、类库和BatchConsole)。
最好的例子是从nAnt Build文件中复制“Assemblyinfo部分”(您可以从Github下载)。
诀窍在于,您可以使用一个commonAssemblyinfo文件,您的nAnt目标将引用它。
以下是nAnt文件中的目标。
<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接