Maven内部版本号插件,如何将内部版本号保存在文件中?

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

我有一个使用 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 片段:

<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>

最佳答案

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

buildnumber-maven-plugin docs .

关于Maven内部版本号插件,如何将内部版本号保存在文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6947827/

相关文章:

android - android-maven-plugin 的问题

java - 我应该将哪个版本的 Spring Security 与 spring core 3.2.5.RELEASE 一起使用

java - 如何安装第一个 Ninja Web Framework 应用程序?

c++ - 给定一定的固定平台,sizeof 是否可以为同一类型返回不同的大小?

java - 有没有办法在构建时自动替换 .java 文件中的模式?

c++ - 为 Windows Phone 8.1 商店应用程序 (WinRT) 构建 unqlite

java - IntelliJ : Break point set in another module of maven project

java - 为生成的类添加 @XmlRootElement 注释后,我无法在源类中访问它们

java - 无法执行目标 org.apache.maven.plugins :maven-resources-plugin:2. 5:resources

Maven 包含另一个用于插件配置的 pom