如何在构建服务器上让MSBuild自动递增ClickOnce发布版本号?

8
我们有一个NAnt脚本,它从CVS检出,然后运行MSBuild来发布应用程序。问题是我们必须记得在Visual Studio中始终增加版本号。
我们有自动递增版本号的选项,但这会在下一次检出时被清除,我不想让构建脚本检入项目文件。
有没有简单的方法来解决这个问题?

这是一个关于自定义MSBuild任务的好教程:http://weblogs.asp.net/bradleyb/archive/2005/12/02/432150.aspx - Jeremy Thompson
3个回答

10

自动更新MinimumRequiredVersion

项目编辑器简介

  1. 在解决方案资源管理器中,右键单击项目并选择卸载项目。

    Screenshot of Unloading

  2. 一旦项目变为不可用状态,再次右键单击并选择编辑<project_name>.<lang> proj。

    Screenshot of Opening Editor

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.

实现

  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.


非常感谢Kev提供的他们的答案,我在这里基本上重新整理了一下,为初学者添加了一些解释。这是我写的一个博客文章,关于这个问题进行了更深入的探讨。

6
MinimumRequiredVersion不是应用程序版本或修订版。可以将更新部署到具有更高修订号的应用程序中,客户端可以通过可选的更新提示来使用它。同步MinimumRequiredVersion仅消除了可选的更新提示,并强制更新在没有提示的情况下进行。我认为这个答案并不完整。没有发布版本递增。 - Jon Davis

2
最终我使用NAnt xmlpoke实现了这个功能,因此版本号变成了20.0.dayofyear.hourminute - 这在大多数构建中是唯一的。
不需要自定义任务 - 新版的MSBuild也有pokexml,所以它可能也可以使用。
<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>

嗨,Adem,这个解决方案是否也会更新 VS 内部的主要、次要和构建版本,还是只用于发布?你知道有没有一种方法可以先读取“ApplicationVersion”的版本,然后仅替换修订版本? - Herdo
这些是用于ClickOnce发布版本的代码 - 对于DLL内部的版本,我在AssemblyInfo.cs文件中有一行代码,如下所示:[assembly: AssemblyVersion("2.1.*")] - 抱歉回复晚了,希望对你有帮助。 - Adam Butler

1

你有几个选项,以下是两个示例:

  1. 指定星号代替构建版本号以自动增加。
    http://msdn.microsoft.com/en-us/library/system.reflection.assemblyversionattribute.aspx

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

  2. 使用 MSBuild 扩展包中的 AsssemblyInfo msbuild 任务。
    http://msbuildextensionpack.codeplex.com
    示例:
    http://www.msbuildextensionpack.com/help/4.0.4.0/html/d6c3b5e8-00d4-c826-1a73-3cfe637f3827.htm

编辑
抱歉,我误读了你的问题。

在这里可以查看Jason Stangroome所接受的答案:
如何让ClickOnce发布版本匹配AssemblyInfo.cs文件版本?


谢谢,但我在寻找发布版本而不是程序集版本。我相信这两者是不同的。 - Adam Butler
抱歉,我误读了你的问题。我已经更新了我的答案。希望对你有所帮助。 - Rami A.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接