【问题标题】:Getting Project Version Information During MSbuild build在 MSbuild 构建期间获取项目版本信息
【发布时间】:2013-01-11 02:57:24
【问题描述】:

我正在尝试通过 MSBuild 自动构建我们的安装程序。我遇到的问题是获取调用自定义 MSBuild 脚本的 C# 项目的版本信息,然后在构建过程中将版本号传递给 Wix。

我想做的是将版本设置为这样的一些属性:

<ProductVersion>$(MajorVersion).$(MinorVersion).$(PatchVersion).$(BuildVersion)</ProductVersion>
<InstallerName>"$(ProductName)-$(ProductVersion).msi"</InstallerName>

版本作为我们持续集成构建的一部分进行更新,并将版本号合并到构建在我们持续集成服务器上的每个安装程序中,这有助于我们生成可持续部署的应用程序。

任何帮助将不胜感激。

【问题讨论】:

    标签: msbuild continuous-integration version


    【解决方案1】:

    我解决这个问题的方法是在我的代码存储库中创建一个“version.xml”文件。该文件包含以下数据

    <?xml version="1.0" encoding="utf-8"?>
    <Project ToolsVersion="3.5"
             DefaultTargets="Build"
             xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
        <PropertyGroup>
            <VersionMajor>0</VersionMajor>
            <VersionMinor>1</VersionMinor>
            <VersionBuild>1</VersionBuild>
            <VersionRevision>0</VersionRevision>
        </PropertyGroup>
    </Project>
    

    在我的情况下,此文件已签入,但使用来自构建服务器的信息或任何所需的信息生成此文件应该不会太难。

    在构建过程中,自定义 MsBuild 任务(类似于 TemplateFile task)会从它们各自的模板文件中创建一个程序集信息文件和一个 Wix 包含文件。可以通过将“version.xml”文件包含在 MsBuild 脚本中来访问它。比如这样:

    <?xml version="1.0" encoding="utf-8"?>
    <Project 
        ToolsVersion="4.0" 
        DefaultTargets="Build" 
        xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    
        <!-- Include the version info file so that we can pull the version info from it -->
        <Import 
            Project="$(DirWorkspace)\version.xml" 
            Condition="Exists('$(DirWorkspace)\version.xml')" />
    
        <!-- Generate a file with the version information -->
        <Target Name="GenerateAssemblyInfoVersionNumber">
        <ItemGroup>
          <VersionTokens Include="Major">
            <ReplacementValue>$(VersionMajor)</ReplacementValue>
          </VersionTokens>
          <VersionTokens Include="Minor">
            <ReplacementValue>$(VersionMinor)</ReplacementValue>
          </VersionTokens>
          <VersionTokens Include="Build">
            <ReplacementValue>$(VersionBuild)</ReplacementValue>
          </VersionTokens>
          <VersionTokens Include="Revision">
            <ReplacementValue>$(VersionRevision)</ReplacementValue>
          </VersionTokens>
        </ItemGroup>
        <TemplateFile 
            Template="$(FileTemplateAssemblyVersion)"
            OutputFileName="$(FileGeneratedAssemblyVersion)" 
            Tokens="@(VersionTokens)" />
      </Target>
    </Project>
    

    C# 项目中包含的 AssemblyInfo.VersionNumber.cs 文件是从如下模板文件生成的:

    //-----------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //
    //     Changes to this file may cause incorrect behavior and will be lost
    //     if the code is regenerated.
    // </auto-generated>
    //-----------------------------------------------------------------------
    
    using System.Reflection;
    
    [assembly: AssemblyVersion("${Major}.${Minor}.${Build}.${Revision}")]
    [assembly: AssemblyFileVersion("${Major}.${Minor}.${Build}.${Revision}")]
    
    // The AssemblyInformationalVersion stores the version that will be displayed in
    // Windows explorer.
    [assembly: AssemblyInformationalVersion("${Major}.${Minor}.${Build}.${Revision}")]
    

    在替换过程中,${TEXT_HERE} 部分将替换为其各自的值。

    Wix 包含模板文件如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <Include xmlns="http://schemas.microsoft.com/wix/2006/wi">
        <!--
          This is a generated file.
          Do NOT make changes to this file.
          They will be undone next time the file is generated.
        -->
    
        <!-- The current version -->
        <?define CurrentVersion = "${Major}.${Minor}.${Build}"?>
    
        <!-- The install version string -->
        <?define ProductVersionFolder = "${Major}.${Minor}"?>
    </Include>
    

    包含此文件后,可以在需要时在 Wix 安装程序中引用 CurrentVersionProductVersionFolder 变量。

    通过使用这种方法,版本信息存储在一个位置,并且可以被构建的所有部分访问。

    【讨论】:

      猜你喜欢
      • 2014-08-21
      • 2015-08-30
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多