在MSBuild中将程序集版本号指定为命令行参数

时间:2011-12-09 14:10:46

标签: msbuild versioning

我希望能够在构建过程中将所有程序集的版本号指定为MSBuild命令参数,如下所示:

MSBuild.exe /p:version=5.4.3.0 

我查看了AssemblyInfoTask,但在这种情况下我似乎不是一个好的解决方案。

3 个答案:

答案 0 :(得分:13)

我一直使用你在评论中描述的AssemblyInfo任务。

  <!-- update standard assembly attribute in all projects -->
  <Target Name="BeforeBuild" >
    <Message Text="Updating AssemblyInfo to Version $(VersionNumber)"></Message>
    <Message Text="Writing to AssemblyInfo files in $(SolutionRoot)"></Message>
    <AssemblyInfo AssemblyInfoFiles="@(AssemblyInfoFiles)" 
                  AssemblyCopyright="$(AssemblyCopyright)" 
                  AssemblyVersion="$(VersionNumber)"
                  AssemblyFileVersion="$(VersionNumber)"
                  >
    </AssemblyInfo>
  </Target>

VersionNumber值完全按照您的描述从MSBuild项目文件外部传递:

  MSBuild <project_file> /p:VersionNumber=<value>;...

我们使用BeforeBuild目标来确保在构建开始之前所有的AssemblyInfo.cs文件都已处理完毕。这不是你想要的吗?

答案 1 :(得分:12)

我知道这是一个老问题,但谷歌引导我到这里作为最佳结果。

我在this中遵循了一个简单的解决方案。不需要扩展包。

基本上你需要做的是添加一个&#34; BuildCommon.targets&#34;文件并相应地修改你的csproj文件,以便在msbuild中指定版本号,如:

msbuild.exe abc.sln /p:Configuration=Release;VersionAssembly=1.2.3.4

希望这有帮助。

答案 2 :(得分:2)

对于使用dotnet.exe构建的SDK样式项目,程序集版本属性为generated automatically,因此您可以直接使用/p:Version=5.4.3.0

如果您使用旧的项目格式,则需要在BeforeBuild文件中添加以下.csproj步骤。无需使用额外的.targets和扩展包,因为MSBuild已经有一个不错的built-in task,它可以完成大部分工作。

<Target Name="BeforeBuild">
  <ItemGroup>
    <AssemblyAttributes Include="AssemblyVersion">
      <_Parameter1>$(Version)</_Parameter1>
    </AssemblyAttributes>
  </ItemGroup>
  <MakeDir Directories="$(IntermediateOutputPath)" />
  <WriteCodeFragment Language="C#"
                     OutputFile="$(IntermediateOutputPath)Version.cs"
                     AssemblyAttributes="@(AssemblyAttributes)" />
  <ItemGroup>
    <Compile Include="$(IntermediateOutputPath)Version.cs" />
  </ItemGroup>
</Target>

只需确保删除现有的AssemblyVersion属性,因为它现在将在构建过程中生成。

相关问题