2013-01-11 36 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變量之後。

通過使用此方法,版本信息存儲在單個位置,並且可以由構建的所有部分訪問。