I am upgrading a large build-system to use Maven2 instead of Ant, and we have two related requirements that I'm stuck on:
We need to generate a time-stamped artifact, so a part of the package phase (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.properties file inside the generated jar.
This information is included in the generated pom.properties when you run,
e.g., mvn package
but is commented out:
#Generated by Maven
#Mon Aug 03 12:57:17 PDT 2009
Any ideas on where to start would be helpful! Thanks!
If you use a version of Maven >= 2.1.0-M1, then you can use the ${maven.build.timestamp} property.
For more info, see: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
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.
We need a newer answer :) It is build in now: http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Available_Variables
use
${maven.build.timestamp}
If you need the time in a timezone other than UTC (the default when you use
${maven.build.timestamp}
) you could use thebuild-helper-maven-plugin
. See more in Brief examples on how to use the Build Helper Maven Plugin's goals.Anyway, this is how I've got the timestamp in GMT-5 and put it in the final name of my artifact:
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
and the package name is
MyProject_1.0_2015_03_02_13_46.war
Maven versions 2.1.0-M1 or newer have built in special variable
maven.build.timestamp
.See Maven documentation for more details.
For older Maven versions a look at maven-timestamp-plugin or buildnumber-maven-plugin.
If you use maven-timestamp-plugin, you can use something like this to manipulate resulting artifact name.
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.properties file directly with this plugin.
These three sites are also worth checking out.