Dynamic Versioning

不羁的心 提交于 2019-12-03 13:47:40

You can use the MsbuildCommunityTasks to generate the build number and to customize the assembly file version on pre-build time.

  • Download the zip at MsbuildCommunityTasks

  • Unzip to the folder [SolutionFolder]\MsBuildExtensions\MSBuildCommunityTasks

  • Add the sample below on your project (csproj), just after the Microsoft.CSharp.Targets import.

  <PropertyGroup>
  <MSBuildCommunityTasksPath>$(MSBuildThisFileDirectory)..\MsBuildExtensions\MSBuildCommunityTasks</MSBuildCommunityTasksPath>  
    <My-PropertiesDir>Properties</My-PropertiesDir>
  </PropertyGroup>
  <Import Project="$(MSBuildCommunityTasksPath)\MSBuild.Community.Tasks.Targets"/>
  <Target Name="BeforeBuild">
    <Time Format="yyyy.MM.dd.HHmm">
      <Output TaskParameter="FormattedTime" PropertyName="My-VersionNumber" />
    </Time>
    <Message Text="Building $(My-VersionNumber) ...">
    </Message>
    <ItemGroup>
      <My-AssemblyInfo Include="$(My-PropertiesDir)\AssemblyVersionInfo.cs" />
      <Compile Include="@(My-AssemblyInfo)" />
    </ItemGroup>
    <MakeDir Directories="$(My-PropertiesDir)" />
    <AssemblyInfo OutputFile="@(My-AssemblyInfo)"
          CodeLanguage="CS"
          AssemblyFileVersion="$(My-VersionNumber)"
          AssemblyInformationalVersion="$(My-VersionNumber)"
          Condition="$(My-VersionNumber) != '' "
          />
  </Target>
  <Target Name="AfterBuild">
    <Delete Files="@(My-AssemblyInfo)" />
  </Target>

  • Wipe the AssemblyFileVersion attribute from your AssemblyInfo.cs. It will be generated at build time.

  • You'll see the version number being printed on the console when you build. The generated file is deleted on the AfterBuild target, to keep your source control clean.

    BeforeBuild:

    Building 2013.01.14.1016 ... Created AssemblyInfo file "Properties\AssemblyVersionInfo.cs".

    (...)

    AfterBuild:

    Deleting file "Properties\AssemblyVersionInfo.cs".

  • If you want do this to many projects with less msbuild code, it will be necessary to create a customized build script to wrap up your solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!