Search code examples
maven-2mavenmaven-assembly-pluginmaven-3

How to timestamp an output artifact in Maven?


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.