Maven内部版本号插件,如何将内部版本号保存在文件中? [英] Maven build number plugin, how to save the build number in a file?
问题描述
我有一个使用Spring Framework和Git的Java项目,我想显示一个内部版本号.我找到了内部版本号Maven插件.对于Git,内部版本号是一个Git哈希.我不喜欢这样,我认为约会更具表现力.
I've a Java project using Spring Framework and Git and I wanted to display a build number. I found the Build Number Maven plugin. With Git the build number is a Git hash. I dislike that and I thought a date was much more expressive.
我找到了这个精彩的博客文章,介绍了如何将SVN和Git的内部版本号插件与其他配置文件一起使用.由于我只使用Git,因此没有创建新的配置文件,而是将插件部分复制到了构建标记中.
I found this excellent blog article explaining how to use build number plugin with a different profile for SVN and Git. Since I just use Git, instead of creating a new profile, I just copied the plugin part in my build tag.
当我运行"mvn软件包"时,它告诉我:
When I run "mvn package" it tells me:
[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中).
Which looks ok, but I wonder, where is it stored? "git status" doesn't detect any new file and it seems it's not in target/ too (target/ is in my .gitignore).
也许我必须更改配置以将内部版本号存储在文件中?如何使用内部版本号值?
Maybe I've to change the configuration to store the build number in a file? How can I use the build number value?
由于Michael-O的提示,我阅读了有关 《 Maven入门指南》中的如何过滤资源文件.我已经在src/main/resources/properties/application.properties中创建了文件application.properties,内容如下:
Thanks to the hint of Michael-O, I read the chapter about how to filter resource files in Maven Getting Started Guide. I've created a file application.properties in src/main/resources/properties/application.properties with the following contents:
# application properties
application.name=${pom.name}
application.version=${pom.version}
application.build=${buildNumber}
我在构建部分中添加了以下XML代码段:
I've added the following XML snippet within my build section:
<resources>
<resource>
<directory>src/main/resources/properties</directory>
<filtering>true</filtering>
</resource>
</resources>
现在,当我从命令行"mvn package"调用时,此属性文件将保存在target/classes/properties/application.properties中,例如,具有以下内容:
Now when I call from command line "mvn package", this property file gets saved in target/classes/properties/application.properties, for instance with the following contents:
# application properties
application.name=Sherd Control Panel
application.version=1.0.1-SNAPSHOT
application.build=2011-08-05_05-55_stivlo
在命令行中一切正常,但是,m2eclipse会产生Build错误:
Everything works fine from command line, but, sigh, m2eclipse gives Build errors:
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.
For some reason m2eclipse tries to connect to my repository, but it can't because it's a Git repository accessed with SSH and a private key. I wonder if I can tell m2eclipse to not connect to Git.
在进一步挖掘之后,我发现有关versionOnScmFailure选项,将其设置为true,现在m2eclipse也可以使用.作为参考,这是我使用的buildnumber maven插件的完整配置(在build/plugins部分的pom.xml中).
After more digging I found about revisionOnScmFailure option, set it to true and now also m2eclipse works. For reference, here is the full configuration of buildnumber maven plugin that I used (it goes in pom.xml in the build / plugins section).
<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将版本号输出到文本文件
Store it in a filtered properties file. See Using maven to output the version number to a text file
这篇关于Maven内部版本号插件,如何将内部版本号保存在文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!