【问题标题】:Maven build number plugin, how to save the build number in a file?Maven内部版本号插件,如何将内部版本号保存在文件中?
【发布时间】:2011-10-20 08:12:06
【问题描述】:

我有一个使用 Spring Framework 和 Git 的 Java 项目,我想显示一个内部版本号。我找到了Build Number Maven plugin。对于 Git,内部版本号是 Git 哈希。我不喜欢这样,我认为约会更具表现力。

我发现这个excellent blog article 解释了如何将内部版本号插件与 SVN 和 Git 的不同配置文件一起使用。由于我只是使用 Git,而不是创建新配置文件,我只是将插件部分复制到我的构建标签中。

当我运行“mvn package”时它告诉我:

[INFO] --- buildnumber-maven-plugin:1.0:create (default) @ sherd ---
[INFO] Storing buildNumber: 2011-08-04_21-48_stivlo at timestamp: 1312487296631

看起来不错,但我想知道,它存储在哪里? “git status” 没有检测到任何新文件,而且它似乎也不在 target/ 中(target/ 在我的 .gitignore 中)。

也许我必须更改配置以将内部版本号存储在文件中?如何使用内部版本号值?


感谢 Michael-O 的提示,我阅读了关于 how to filter resource files in Maven Getting Started Guide 的章节。我在 src/main/resources/properties/application.properties 中创建了一个文件 application.properties,内容如下:

# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}

我在构建部分中添加了以下 XML sn-p:

<resources>
    <resource>
        <directory>src/main/resources/properties</directory>
        <filtering>true</filtering>
    </resource>
</resources>

现在,当我从命令行“mvn package”调用时,此属性文件会保存在 target/classes/properties/application.properties 中,例如具有以下内容:

# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo

从命令行一切正常,但是,叹息,m2eclipse 给出了构建错误:

05/08/11 6.05.03 CEST: Build errors for obliquid-cp; 
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal
org.codehaus.mojo:buildnumber-maven-plugin:1.0:create (default) on project 
sherd: Cannot get the branch information from the scm repository : 
Exception while executing SCM command.

由于某种原因,m2eclipse 尝试连接到我的存储库,但它不能,因为它是一个使用 SSH 和私钥访问的 Git 存储库。我想知道我是否可以告诉 m2eclipse 不连接到 Git。


经过更多挖掘后,我发现了关于 revisionOnScmFailure 选项,将其设置为 true,现在 m2eclipse 也可以工作了。作为参考,这里是我使用的 buildnumber maven 插件的完整配置(它在 build / plugins 部分的 pom.xml 中)。

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <goals>
                <goal>create</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
         <revisionOnScmFailure>true</revisionOnScmFailure>
        <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
        <items>
            <item>timestamp</item>
            <item>${user.name}</item>
        </items>
    </configuration>
</plugin>

【问题讨论】:

    标签: maven build maven-plugin buildnumber-maven-plugin


    【解决方案1】:

    将其存储在过滤的属性文件中。见Using maven to output the version number to a text file

    【讨论】:

      【解决方案2】:

      documentation 页面表示属性文件存储在${basedir}/buildNumber.properties,这是在运行buildnumber:create 阶段时创建的。

      【讨论】:

      • 但是它不会创建该文件,至少在我的配置中是这样。我通过过滤属性文件取得了部分成功,但在更新的问题中遇到了另一个问题。
      • 您是否尝试过手动创建文件,然后先在其中添加起始版本号?这样做,做你的事,然后检查数字是否增加。确保您满足了它要求的要求。
      • 感谢您的建议。我尝试创建一个空的 buildNumber.properties 文件,然后执行 buildnumber:create 并没有任何反应,然后我尝试放入 1,执行 buildnumber:create 并没有任何反应。与此同时,我刚刚找到了使用过滤属性的最后一个问题的解决方案。我将用完整的解决方案更新我的问题。
      • @stivlo 当您使用过滤属性时,您的属性文件包含字符串foo=${buildNumber},并且仅为构建的工件更改变量。如果我处于开发模式并想从属性文件中获取内部版本号而不是 ${buildNumber} 字符串怎么办?
      【解决方案3】:

      您不应该将revisionOnScmFailure 选项设置为true,它不需要布尔值。将其设置为 SCM 不可用时要使用的修订字符串,例如 na 或类似的。这对您的情况无关紧要,因为您覆盖了内部版本号格式,但它会更正确。

      buildnumber-maven-plugin docs

      【讨论】:

      • 谢谢,在 Mac OS X 10.10 上通过 Netbeans 构建时,我无法让该插件工作,但通过命令行它构建得很好。设置此属性让我可以继续通过 IDE 工作,而 Jenkins 服务器显然也可以工作。
      【解决方案4】:

      我无法重现 OP 报告的问题。在我的情况下,命令行和 m2eclipse 都可以正常工作,并且文件在 target/classes 文件夹中正确生成。 @KasunBG 提供的答案不正确。 buildNumber.properties 仅在您使用以下内容时生成:

              <format>{0,number}</format>
              <items>
                  <item>buildNumber</item>
              </items>
      

      buildNumber.properties 用于存储可以递增的数字。出于这个原因(我认为),如果您使用时间戳/scmVersion 等,插件不会生成此文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 2017-05-05
        • 1970-01-01
        • 2013-04-10
        • 2011-07-09
        • 1970-01-01
        相关资源
        最近更新 更多