17

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.

2
  • Does the answer to this question help at all? Commented Sep 17, 2012 at 22:51
  • @adrianbanks: That sets the assembly version from some variable in the batch script. I'm hoping to set the deployment version from the assembly version (where the assembly version is stored in the AssemblyInfo.cs for the project). Commented Sep 17, 2012 at 22:53

3 Answers 3

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I eventually went with another solution (changing the rules a bit), but this appears like it would work if my way doesn't pan out. Thanks!
I came back to this, and it didn't work exactly as I expected. My hope was to have the deployment version defined in the deployment manifest that is generated. However, this is generated before publish. Doing a similar step as shown on BeforeBuld would work, but the assembly isn't available by then. My workaround is to pass in the version to msbuild on the command line, and use that in the BeforeBuild task.
bin\$(Configuration)\WpfApplication1.exe could be replaced by $(TargetPath)
Agreed @rudolf_franek, that's much better. I've updated my answer.
$(TargetPath) was not working, changing it to <GetAssemblyIdentity AssemblyFiles="$(IntermediateOutputPath)\$(TargetFileName)"> fixed it for me.
14
msbuild xxx.csproj /target:clean;publish /property:ApplicationVersion=1.2.3.4

3 Comments

If using VSTS you can use /property:ApplicationVersion=1.0.0.$(Build.SourceVersion) to get the changeset / commit ID automatically appended. Of course you can still do major versioning here if you wish, but it means a new build will always have a new version and you can always track back to the source version.
That feels amazing - for the last 7 years I've been auto-incrementing it myself
This is the simplest and most minimal approach, works awesome with Jenkins! Thanks!
10

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>

1 Comment

It seems that $(ProjectDir)obj\$(ConfigurationName) can be replaced with $(IntermediateOutputPath).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.