Search code examples
maven-2filtering

Adding the current date with Maven2 filtering


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)


Solution

  • 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}.