Specify assembly version number as a command line argument in MSBuild

╄→гoц情女王★ 提交于 2020-02-12 07:51:12

问题


I would like to be able to specify the version number for all assemblies to be generated during a build as a MSBuild command argument like this:

MSBuild.exe /p:version=5.4.3.0 

I have looked over AssemblyInfoTask but it does not seem to me like a good solution in this case.


回答1:


I use the AssemblyInfo task as you describe in your comment all the time.

  <!-- 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>

The VersionNumber value is passed from outside the MSBuild project file exactly as you describe:

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

We use the BeforeBuild target to ensure the AssemblyInfo.cs files all get worked on before the build starts. Is this not what you want?




回答2:


I know this is an old question but Google leads me to here as top result.

I followed a simple solution in this. No need for extension pack.

Basically what you need to do is add a "BuildCommon.targets" files and modify your csproj file accordingly to have the version number specified in msbuild like:

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

Hope this helps.




回答3:


For SDK-style projects that are built using dotnet.exe, assembly version attributes are generated automatically, so you can use /p:Version=5.4.3.0 right out of the box.

If you use the old project format, you'd need to add the following BeforeBuild step to your .csproj file. No need to use extra .targets and extension packs, because MSBuild already has a nice built-in task which does most of the stuff.

<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>

Just make sure you remove the existing AssemblyVersion attribute, because it will now be generated during build.



来源:https://stackoverflow.com/questions/8446693/specify-assembly-version-number-as-a-command-line-argument-in-msbuild

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