简体   繁体   English

动态版本控制

[英]Dynamic Versioning

I have a situation where i want the versioning to be dynamic at build time. 我有一种情况,我希望版本控制在构建时是动态的。

Version Pattern: <year>.<month>.<day>.<hhmm> 版本模式: <year>.<month>.<day>.<hhmm>

But i have read where the String value used in the Attribute is reparsed at compile time. 但是我已经阅读了在编译时重新解析Attribute中使用的String值的位置。

Any advise on how to get this dynamic versioning completed? 关于如何完成此动态版本的任何建议?

Ideal situation: 理想情况:

<Assembly: AssemblyVersion("4.0.0.0")> 
<Assembly: AssemblyFileVersion(Year(Now) & "." & Month(Now()) & "." & Day(Now()) & "." & String.format("hhmm", now()))> 

I know it wont work but should get the point acrossed. 我知道这行不通,但应该指出这一点。

You can use the MsbuildCommunityTasks to generate the build number and to customize the assembly file version on pre-build time. 您可以使用MsbuildCommunityTasks生成内部版本号并在构建前时间自定义程序集文件版本。

  • Download the zip at MsbuildCommunityTasks MsbuildCommunityTasks下载zip 文件

  • Unzip to the folder [SolutionFolder]\\MsBuildExtensions\\MSBuildCommunityTasks 解压缩到文件夹[SolutionFolder] \\ MsBuildExtensions \\ MSBuildCommunityTasks

  • Add the sample below on your project (csproj), just after the Microsoft.CSharp.Targets import. 在导入Microsoft.CSharp.Targets之后,将以下示例添加到您的项目(csproj)中。

  <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. 从AssemblyInfo.cs中擦除AssemblyFileVersion属性。 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. 生成的文件在AfterBuild目标上被删除,以保持源代码管理的干净。

    BeforeBuild: 构建之前:

    Building 2013.01.14.1016 ... Created AssemblyInfo file "Properties\\AssemblyVersionInfo.cs". 建筑物2013.01.14.1016 ...创建了AssemblyInfo文件“ Properties \\ AssemblyVersionInfo.cs”。

    (...) (...)

    AfterBuild: AfterBuild:

    Deleting file "Properties\\AssemblyVersionInfo.cs". 删除文件“ 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. 如果要对使用较少msbuild代码的许多项目执行此操作,则有必要创建自定义的生成脚本来包装您的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM