我正在尝试通过 MSBuild 自动构建我们的安装程序。我遇到的问题是获取调用自定义 MSBuild 脚本的 C# 项目的版本信息,然后该脚本会在构建过程中将版本号传递到 Wix 中。
我想做的是将版本设置为一些属性,如下所示:
<ProductVersion>$(MajorVersion).$(MinorVersion).$(PatchVersion).$(BuildVersion)</ProductVersion>
<InstallerName>"$(ProductName)-$(ProductVersion).msi"</InstallerName>
该版本作为我们持续集成构建的一部分进行更新,并将版本号合并到在我们的持续集成服务器上构建的每个安装程序中,这有助于我们生成可持续部署的应用程序。
任何帮助将不胜感激。
最佳答案
我解决这个问题的方法是在我的代码存储库中创建一个“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>
在我的例子中,此文件已 checkin ,但使用来自构建服务器的信息或所需的任何信息生成此文件应该不会太难。
在构建过程中,自定义 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 安装程序中需要的地方引用 CurrentVersion
和 ProductVersionFolder
变量。
通过使用此方法,版本信息存储在单个位置,并且可以由构建的所有部分访问。
关于msbuild - 在 MSbuild 构建期间获取项目版本信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270942/