Specify publish version with MSBuild command line as assembly version of project

对着背影说爱祢 提交于 2019-12-03 05:44:14
Dan Nolan

Try adding this to your .csproj file. The target will retrieve the version from the output assembly and update the ApplicationVersion before the Publish:

<Target Name="AfterCompile">
  <GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
    <Output TaskParameter="Assemblies" ItemName="fooAssemblyInfo"/>
  </GetAssemblyIdentity>
  <PropertyGroup>
    <ApplicationVersion>%(fooAssemblyInfo.Version)</ApplicationVersion>
  </PropertyGroup>
</Target>

There's probably a nicer way to dynamically get the assembly name but for your purpose it should do the trick.

Credit to this answer for the GetAssemblyIdentity syntax: https://stackoverflow.com/a/443364/266882

Questioner Edit:

See comment below for update.

Robin B
msbuild xxx.csproj /target:clean;publish /property:ApplicationVersion=1.2.3.4

In order to correctly update the version declared in the deployment manifest you need to modify the ApplicationVersion at the "AfterCompile" step rather than the "BeforePublish" step, since the application manifest is generated at build time. But then you can't rely on the $(TargetPath) property to point to the assembly and instead use the following path: $(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)

So here's the updated Target code snippet that you can add to the .csproj file:

<Target Name="AfterCompile">
  <GetAssemblyIdentity AssemblyFiles="$(ProjectDir)obj\$(ConfigurationName)\$(TargetFileName)">
     <Output TaskParameter="Assemblies" ItemName="AssemblyInfo" />
  </GetAssemblyIdentity>
  <PropertyGroup>
    <ApplicationVersion>%(AssemblyInfo.Version)</ApplicationVersion>
  </PropertyGroup>
</Target>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!