8

We have an NAnt script that checks out from CVS and then runs MSBuild to publish the application. The problem is we have to remember to always increment the version in Visual Studio.

We have the option to auto increment this on publish, but this gets wiped on the next checkout and I would rather not have to get the build script to check in the project file.

Is there a simple way to do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Butler
  • 3,023
  • 5
  • 35
  • 40

3 Answers3

10

Updating the MinimumRequiredVersion Automatically

Introduction to Project Editor

  1. In Solution Explorer, right click on your project and select unload project.

    Screenshot of Unloading

  2. Once the project has become unavailable, right click again and select edit <project_name>.<lang> proj.

    Screenshot of Opening Editor

Introduction to MSBuild

  1. Properties use key/value pairs to extract information

    • Using the property name as an alias, you can use $(OutputPath) to obtain the value for the element <OutputPath>.\bin</OutputPath>
  2. We’ll use the following properties generated for a ClickOnce deployment

    <MinimumRequiredVersion>1.0.0.6</MinimumRequiredVersion>
    <ApplicationRevision>7</ApplicationRevision>
    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
    
  3. MSBuild Tasks can be specified in the project (*.proj) file and invoked during a build event.

    • FormatVersion is a built-in task for .NET 4.0 and later that formats the ApplicationVersion and ApplicationRevision into a single version number.

Implementation

  1. Copy and Paste the following code into the opened project file as a child element to the root <Project> element.

    <Target Name="AutoSetMinimumRequiredVersion" BeforeTargets="GenerateDeploymentManifest">
      <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
        <Output PropertyName="MinimumRequiredVersion" TaskParameter="OutputVersion"  />
      </FormatVersion>
      <FormatVersion Version="$(ApplicationVersion)" Revision="$(ApplicationRevision)">
        <Output PropertyName="_DeploymentBuiltMinimumRequiredVersion" TaskParameter="OutputVersion"  />
      </FormatVersion>
    </Target>
    

    This code will take ApplicationVersion and ApplicationRevision as parameters in the Format Version task and will save the output by overwriting the MinimumRequiredVersion with the full publish version.

  2. Save and reload your project. Every ClickOnce deployment will now automatically update to the most recently published version.


Many thanks to Kev for their answer which I have basically rehashed here with a little bit of added clarification for any beginners. Here's a blog post I made about the issue that expands even more on my answer here.

Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • 6
    MinimumRequiredVersion is not the app version/revision. One can deploy an update to the app with a higher revision number, and clients can consume it with an optional update prompt. Synchronizing the MinimumRequiredVersion only eliminates the optional update prompt, and forces the update to happen without prompting. I don't think that this answer is an answer. There is no incrementing the published version. – Jon Davis May 11 '15 at 17:55
2

In the end I did this using NAnt xmlpoke, so for the version we end up with 20.0.dayofyear.hourminute - it is mostly unique across builds.

There is no need for custom tasks - and the newer version of MSBuild has a pokexml too, so it might work with that.

<target name="pokerevision" depends="init">
    <property name="projectname" value="MyProject.GUI" />

    <!-- This is a bit flawed because 231 could mean 02:31 or 23:01, but we never build before 3 am. -->
    <property
        name="app.revision"
        value="${datetime::get-hour(datetime::now())}${datetime::get-minute(datetime::now())}" />

    <echo message="revision: ${app.revision}" />

    <xmlpoke
        file="${Solution.Path}\${projectname}\${projectname}.csproj"
        xpath="//x:Project/x:PropertyGroup[1]/x:ApplicationRevision"
        value="${app.revision}"
    >
        <namespaces>
            <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
        </namespaces>
    </xmlpoke>

    <property
        name="app.version"
        value="20.0.${datetime::get-day-of-year(datetime::now())}.${app.revision}" />

    <echo message="version: ${app.version}" />

    <xmlpoke
        file="${Solution.Path}\${projectname}\${projectname}.csproj"
        xpath="//x:Project/x:PropertyGroup[1]/x:ApplicationVersion"
        value="${app.version}"
    >
        <namespaces>
            <namespace prefix="x" uri="http://schemas.microsoft.com/developer/msbuild/2003" />
        </namespaces>
    </xmlpoke>
</target>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adam Butler
  • 3,023
  • 5
  • 35
  • 40
  • Hi Adem, does this solution also update the Major, Minor and Build version for inside VS or just for publishing? Do you know a solution to read the version of `ApplicationVersion` first and only replace the revision? – Stefan Over Jul 19 '13 at 11:31
  • these are for the publish versions for click once - for the version inside the dlls I have a line like this in my AssemblyInfo.cs [assembly: AssemblyVersion("2.1.*")] - sorry for the delay, hth – Adam Butler Jan 17 '14 at 01:14
1

You have several options, here are two:

  1. Specify an asterisks in lieu of the build version number to have it automatically incremented
    http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx

    [assembly: AssemblyVersion("1.0.*")]

  2. Use the AsssemblyInfo msbuild task from the MSBuild Extension Pack.
    http://msbuildextensionpack.codeplex.com
    Example:
    http://www.msbuildextensionpack.com/help/4.0.4.0/html/d6c3b5e8-00d4-c826-1a73-3cfe637f3827.htm

Edit
Sorry I misread your question.

See the accepted answer by Jason Stangroome here:
How do I get the ClickOnce Publish version to match the AssemblyInfo.cs File version?

Community
  • 1
  • 1
Rami A.
  • 10,302
  • 4
  • 44
  • 87