Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a simple batch file that I'm running from a DOS command line that is used for building a small C# application that publishes a ClickOnce project. One line is this:

msbuild MyApp.csproj /t:publish /property:PublishDir="deploy/"

This currently publishes the application, but it uses the Publish Version that I set up in Visual Studio's "Publish" tab. I'm hoping to be able to set the publish version at the command line, and specifically, I'd like to use the Assembly Version of the project. Something like:

msbuild MyApp.csproj /t:publish /property:PublishDir="deploy/" /property:PublishVersion="$(Proj.AssemblyVersion)"

I'm hoping to do without creating a custom task, since this is just an interim solution, and I will replace it with a more proper build system later.

Alternatively, I've looked at updating the published manifest version using the Mage Command Line Tool with the -Update flag, but I did not know how to retrieve the assembly version number from the project or built assembly without using PowerShell or some program that would need to be downloaded. If I could use something that comes with Visual Studio, that would work as well.

like image 310
Mark Hildreth Avatar asked Sep 17 '12 22:09

Mark Hildreth


2 Answers

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.

like image 56
Dan Nolan Avatar answered Sep 23 '22 12:09

Dan Nolan


msbuild xxx.csproj /target:clean;publish /property:ApplicationVersion=1.2.3.4
like image 26
Robin B Avatar answered Sep 23 '22 12:09

Robin B