2013-01-11 38 views
1

我试图通过MSBuild自动化安装程序的构建。我碰到的问题是获取调用自定义MSBuild脚本的C#项目的版本信息,该脚本会在构建过程中将版本号传递给Wix。获取项目版本信息在MSbuild构建

我想什么做的是版本设置为某些属性是这样的:

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

版本更新为我们的持续集成构建的一部分,并纳入版本号到每个所生成的安装程序在我们的持续集成服务器上帮助我们生产一个持续部署的应用程序。

任何帮助将不胜感激。

回答

2

我解决这个问题的方法是在我的代码库中创建'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 taks(类似于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}部被替换为它们各自的值。

的维克斯包括模板文件看起来是这样的:

<?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> 

含有该文件有可能指的是CurrentVersion,并在维克斯安装在以往任何时候需要它ProductVersionFolder变量之后。

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