Java 如何向 Maven 工件添加时间戳信息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1224359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I add time-stamp information to Maven artifacts?
提问by Ryan
I am upgrading a large build-system to use Maven2 instead of Ant, and we have two related requirements that I'm stuck on:
我正在升级一个大型构建系统以使用 Maven2 而不是 Ant,我们有两个相关的需求,我坚持:
We need to generate a time-stamped artifact, so a part of the packagephase (or wherever), instead of building
project-1.0-SNAPSHOT.jar
we should be building
project-1.0-20090803125803.jar
(where the
20090803125803
is just aYYYYMMDDHHMMSS
time-stamp of when the jar is built).The only real requirement is that the time-stamp be a part of the generated file's filename.
The same time-stamp has to be included within a version.propertiesfile inside the generated jar.
我们需要生成一个带时间戳的工件,因此是打包阶段(或其他任何地方)的一部分,而不是构建
project-1.0-SNAPSHOT.jar
我们应该建造
project-1.0-20090803125803.jar
(其中
20090803125803
只是YYYYMMDDHHMMSS
构建罐子的时间戳)。唯一真正的要求是时间戳是生成文件的文件名的一部分。
相同的时间戳必须包含在生成的 jar内的version.properties文件中。
This information is included in the generated pom.propertieswhen you run,
e.g., mvn package
but is commented out:
例如,当您运行时,此信息包含在生成的pom.properties 中,mvn package
但被注释掉:
#Generated by Maven
#Mon Aug 03 12:57:17 PDT 2009
Any ideas on where to start would be helpful! Thanks!
关于从哪里开始的任何想法都会有所帮助!谢谢!
采纳答案by Juha Syrj?l?
Maven versions 2.1.0-M1 or newer have built in special variable maven.build.timestamp
.
Maven 版本 2.1.0-M1 或更新版本内置了特殊变量maven.build.timestamp
。
<build>
<finalName>${project.artifactId}-${project.version}-${maven.build.timestamp}</finalName>
</build>
See Maven documentationfor more details.
有关更多详细信息,请参阅 Maven文档。
For older Maven versions a look at maven-timestamp-pluginor buildnumber-maven-plugin.
对于较旧的 Maven 版本,请查看maven-timestamp-plugin或buildnumber-maven-plugin。
If you use maven-timestamp-plugin, you can use something like this to manipulate resulting artifact name.
如果您使用 maven-timestamp-plugin,您可以使用类似这样的东西来操作生成的工件名称。
<build>
<finalName>${project.artifactId}-${project.version}-${timestamp}</finalName>
</build>
And this configuration for buildnumber-maven-plugin should create a ${timestamp} property which contains the timestamp value. There doesn't seem to be a way to create the version.propertiesfile directly with this plugin.
而 buildnumber-maven-plugin 的这个配置应该创建一个包含时间戳值的 ${timestamp} 属性。似乎没有办法直接使用此插件创建version.properties文件。
<configuration>
<format>{0,date,yyyyMMddHHmmss}</format>
<items>
<item>timestamp</item>
</items>
</configuration>
回答by Rich Seller
When a SNAPSHOT project is deployed, by default a timestamp is used unless you override it in the deploy plugin. If you're not getting unique timestamps, it is probably down to a configuration of your Maven repository. As the other answer says though, use the timestamp or buildnumber plugin for releases.
部署 SNAPSHOT 项目时,默认情况下使用时间戳,除非您在部署插件中覆盖它。如果您没有获得唯一的时间戳,则可能归结为 Maven 存储库的配置。正如另一个答案所说,请使用时间戳或 buildnumber 插件进行发布。
回答by Horia Chiorean
If you use a version of Maven >= 2.1.0-M1, then you can use the ${maven.build.timestamp} property.
如果您使用 Maven >= 2.1.0-M1 的版本,那么您可以使用 ${maven.build.timestamp} 属性。
For more info, see: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
有关更多信息,请参阅:http: //maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
回答by lmiguelmh
If you need the time in a timezone other than UTC (the default when you use ${maven.build.timestamp}
) you could use the build-helper-maven-plugin
. See more in Brief examples on how to use the Build Helper Maven Plugin's goals.
如果您需要 UTC 以外的时区中的时间(使用时的默认值${maven.build.timestamp}
),您可以使用build-helper-maven-plugin
. 在关于如何使用 Build Helper Maven Plugin's目标的简要示例中查看更多信息。
Anyway, this is how I've got the timestamp in GMT-5and put it in the final name of my artifact:
无论如何,这就是我如何在 GMT-5 中获得时间戳并将其放入我的工件的最终名称中:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.9.1</version>
<executions>
<execution>
<id>timestamp-property</id>
<goals>
<goal>timestamp-property</goal>
</goals>
<configuration>
<name>current.time</name>
<pattern>yyyyMMdd-HHmmss</pattern>
<timeZone>GMT-5</timeZone>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<finalName>${project.name}-${current.time}</finalName>
</configuration>
</plugin>
</plugins>
</build>
回答by mcvkr
This post (especially the below part) is also very useful and practical for this issue.
这篇文章(尤其是下面的部分)对于这个问题也非常有用和实用。
Stamping Version Number and Build Time in a Properties File with Maven
The pom will look like this
pom 看起来像这样
...
<properties>
....
<!-- Timestamp of build -->
<timestamp>${maven.build.timestamp}</timestamp>
<maven.build.timestamp.format>yyyy_MM_dd_HH_mm</maven.build.timestamp.format>
</properties>
...
<build>
<finalName>${project.artifactId}-${project.version}-${timestamp}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
....
</plugin>
</plugins>
</build>
....
and the package name is MyProject-1.0. 0-2015_03_02_13_46.war
包名是 MyProject-1.0. 0-2015_03_02_13_46.war
回答by Jan
We need a newer answer :) It is build in now: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
我们需要一个更新的答案 :) 它现在是内置的:http: //maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
use ${maven.build.timestamp}
用 ${maven.build.timestamp}