views:

2548

answers:

4

I have a Maven2 project, and I need to add, in a properties file, the current version and the current date.

For the current version, I've used ${project.version}, which works correctly.

My question is how can I set the current date (i.e. the date when the build is done by Maven2) in my properties file:

client.version=Version ${project.version}
client.build=???

(in addition, if I can specify the format for the date, it will be really great)

+6  A: 

Hi

You can use the Maven Buildnumber Plugin for this:

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>buildnumber-maven-plugin</artifactId>
      <executions>
        <execution>
          <phase>initialize</phase>
          <goals>
            <goal>create</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <doCheck>false</doCheck>
        <doUpdate>false</doUpdate>
        <timestampFormat>{0, date, yyyy-MM-dd HH:mm:ss}</timestampFormat>
      </configuration>
    </plugin>
  </plugins>
</build>

The date is then available in the property ${buildNumber}.

Thomas Marti
+2  A: 

Another solution is to use Groovy inside the pom.xml (maybe not as proper as the solution proposed by Thomas Marti):

   <build>
      <resources>
         <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
         </resource>
      </resources>
      <plugins>
         <plugin>
            <groupId>org.codehaus.groovy.maven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <executions>
               <execution>
                  <phase>validate</phase>
                  <goals>
                     <goal>execute</goal>
                  </goals>
                  <configuration>
                     <source>
                     import java.util.Date 
                     import java.text.MessageFormat 
                     def vartimestamp = MessageFormat.format("{0,date,yyyyMMdd-HH:mm:ss}", new Date()) 
                     project.properties['buildtimestamp'] = vartimestamp
                     </source>
                  </configuration>
               </execution>
            </executions>
         </plugin>
      </plugins>
   </build>

and then use the buildtimestamp property:

client.version=${pom.version}
client.build=${buildtimestamp}
romaintaz
+3  A: 

Since Maven 2.1 M1, you can now do ${maven.build.timestamp} provided you also define ${maven.build.timestamp.format}

<properties>
    ...
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
    ...
</properties>
aberrant80
Note: this doesn't work (as of yet) in filtering resource files
matt b
+4  A: 

Feature does not work with maven 2.2.1 resource filtering.

See: http://jira.codehaus.org/browse/MRESOURCES-99

But you could create a custom property in the parent pom:

<properties>
    <maven.build.timestamp.format>yyMMdd_HHmm</maven.build.timestamp.format>
    <buildNumber>${maven.build.timestamp}</buildNumber>
</properties>

Where buildNumber is the new property that can be filtered into the resources.

jj