How to timestamp an output artifact in Maven?
-
28-09-2019 - |
Question
I am trying to find out if Maven has some built-in plug-in that can be used to time-stamp artifacts. I created an assembly file and am using the maven-assembly plugin to create a final distribution (jars,docs,scripts, etc). I want to name this distribution file as domain_year_month_day.zip. How can I append the day portion of a timestamp to the end of the final zip file that is being produced. Thanks.
Solution
You could use the maven-timestamp-plugin to set a property (e.g. timestamp
) and use it later in the final name of your assembly.
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<id>create-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<finalName>domain_${timestamp}</finalName>
<descriptors>
<descriptor>src/main/assembly/my-descriptor.xml</descriptor>
</descriptors>
<attach>true</attach>
</configuration>
</execution>
</executions>
</plugin>
As an alternative, you could put some Groovy code in your POM using the GMaven plugin:
<plugin>
<groupId>org.codehaus.gmaven</groupId>
<artifactId>gmaven-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>set-custom-property</id>
<phase>initialize</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
def timestamp = new Date().format('MM_dd_yy')
project.properties.setProperty('timestamp', timestamp)
</source>
</configuration>
</execution>
<execution><!-- for demonstration purpose -->
<id>show-custom-property</id>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<source>
println project.properties['timestamp']
</source>
</configuration>
</execution>
</executions>
</plugin>
A sample output showing the property:
$ mvn generate-resources [INFO] Scanning for projects... [INFO] ... [INFO] --- gmaven-plugin:1.3:execute (set-custom-property) @ Q4081274 --- [INFO] [INFO] --- gmaven-plugin:1.3:execute (show-custom-property) @ Q4081274 --- 11_02_10 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...
And again, use this property later in the build name of your assembly.
OTHER TIPS
You don't need the maven-timestamp-plugin with newer versions of maven. Since 2.1'ish, Maven has provide the special property maven.build.timestamp.
You set the format in the pom properties with something like this:
<maven.build.timestamp.format>yyyy-MM-dd'T'HH.mm.ss</maven.build.timestamp.format>
And then use ${maven.build.timestamp} wherever you need a timestamp property. See http://maven.apache.org/guides/introduction/introduction-to-the-pom.html for details.
As ${maven.build.timestamp}
seems buggy in maven, the workaround is as follows:
Create a new variable (I chose "build.timestamp", here) - and, optionally, specify the format :
pom.xml
<project>
...
<properties>
...
<build.timestamp>${maven.build.timestamp}</build.timestamp>
<maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format>
<!-- default is: yyyyMMdd-HHmm -->
</properties>
<build>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptors>
<descriptor>some-assembly.xml</descriptor>
</descriptors>
</configuration>
<executions>
<execution>
<id>make</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
...
Use the custom variable from anywhere:
some-assembly.xml
<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>release-${build.timestamp}</id>
<baseDirectory>/</baseDirectory>
<includeBaseDirectory>false</includeBaseDirectory>
<formats>
<format>zip</format>
</formats>
<fileSets>
<fileSet>
<directory>${project.build.directory}/${project.artifactId}-${project.version}</directory>
</fileSet>
</fileSets>
</assembly>
if you use Hudson/Jenkins you can just use the variable ${BUILD_ID} for getting sort of timestamp to any properties file u want to edit.
information to the other environment variables Hudson/Jenkins supports, take a look here: http://wiki.hudson-ci.org/display/HUDSON/Building+a+software+project