Alex Rivera | Logout

Adding the current date with Maven2 filtering

Asked 2009-04-29T14:41:18.263
52

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)

Edit
Report

1 Answer

12

Thomas Marti's answer is a step in the right direction, but there's a simpler approach that does not require a dummy <scm> declaration in the POM. Use the buildnumber-maven-plugin, but use the create-timestamp goal. The documentation isn't clear; here's what it looks like to get a date in YYYY-MM-DD format and put it in the build.date property:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.2</version>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <timestampFormat>yyyy-MM-dd</timestampFormat>
        <timestampPropertyName>build.date</timestampPropertyName>
    </configuration>
</plugin>

Out of the box this won't work in Eclipse with m2e, so you'll have to add the following inside the POM <build> section:

<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.eclipse.m2e</groupId>
            <artifactId>lifecycle-mapping</artifactId>
            <version>1.0.0</version>
            <configuration>
                <lifecycleMappingMetadata>
                    <pluginExecutions>
                        <pluginExecution>
                            <pluginExecutionFilter>
                                <groupId>org.codehaus.mojo</groupId>
                                <artifactId>buildnumber-maven-plugin</artifactId>
                                <versionRange>[1.2,)</versionRange>
             
answered 2013-05-21T16:57:24.403

Your Answer