MSBuild: How to make several builds with different versions from same sources?

夙愿已清 提交于 2019-12-11 00:30:40

问题


I have a MSBuild *.proj file that builds my solution and sets build's version using a changeset number.
Now I need to make two builds of the same solution, with one difference between: version of the first should be i.e. "5.0.0.{chanset_number}", but version of the second - "2.0.0.{chanset_number}".

I use the following code to get a number of the latest changeset and to set version of a build:

<ItemGroup>
  <FilesToVersion Include="$(SolutionRoot)\GUI\Properties\AssemblyInfo.cs" />
</ItemGroup>
<!-- Added for using the latest changeset id as build number -->
<Target Name="BuildNumberOverrideTarget">
  <BuildNumberGenerator>
    <Output TaskParameter="BuildNumber" PropertyName="BuildNumber" />
  </BuildNumberGenerator>
  <GetLatestChangeset>
    <Output TaskParameter="LatestChangeset" PropertyName="LatestChangeset" />
  </GetLatestChangeset>
</Target>
<Target Name="AfterGet" Condition="'$(IsDesktopBuild)'!='true' ">
  <MSBuild.ExtensionPack.VisualStudio.TfsVersion 
     TaskAction="SetVersion" Files="%(FilesToVersion.Identity)"
     Version="$(VersionMajor).$(VersionMinor).$(VersionService).$(LatestChangeset)" 
AssemblyVersion="$(VersionMajor).$(VersionMinor).$(VersionService).$(LatestChangeset)"
SetAssemblyVersion="true" />
</Target>

回答1:


Adding a PropertyGroup may help here.

<PropertyGroup>
    <MyVersionMajor Condition="$(MyVersionMajor)==''">$(VersionMajor)</MyVersionMajor>
</PropertyGroup>

This will set a property called MyVersionMajor to the VersionMajor property if you do not explicitly set it via an MSBuild Parameter.

To set MyVersionMajor as an MSBuild parameter add the following to your MSBuild Command

MSBuild.exe <yourprojectfile> /p:MyVersionMajor=2

You now need to change the build target to include your new property:

<Target Name="AfterGet" Condition="'$(IsDesktopBuild)'!='true' ">
  <MSBuild.ExtensionPack.VisualStudio.TfsVersion 
     TaskAction="SetVersion" Files="%(FilesToVersion.Identity)"
     Version="$(MyVersionMajor).$(VersionMinor).$(VersionService).$(LatestChangeset)" 
AssemblyVersion="$(MyVersionMajor).$(VersionMinor).$(VersionService).$(LatestChangeset)"
SetAssemblyVersion="true" />
</Target>

Ensuring your new Property group appears before this target.

When you run MSBuild against this project without specifying the parameter you should get "5.0.0.{changeset_number}" and when you specify the parameter you would get "2.0.0.{changeset_number}"



来源:https://stackoverflow.com/questions/12725542/msbuild-how-to-make-several-builds-with-different-versions-from-same-sources

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