51

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)

Rich Seller
  • 83,208
  • 23
  • 172
  • 177
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273

8 Answers8

83

Feature does not work with maven 2.2.1 resource filtering.

See: https://issues.apache.org/jira/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.

turbanoff
  • 2,439
  • 6
  • 42
  • 99
jj.
  • 1
  • 1
  • 2
  • 2
  • 3
    +1 much better than http://maven.apache.org/plugin-developers/cookbook/add-build-time-to-manifest.html BTW, you code can also be inside the same pom.xml in ... . – Cojones Jul 08 '11 at 23:22
  • 3
    Note that in a bug in jenkins is blocking you from using this feature ( only for jenkins case only ) https://issues.jenkins-ci.org/browse/JENKINS-9693 – Rudy Dec 24 '12 at 05:54
  • after 3 years this maven issue is still open! – Karussell Mar 22 '13 at 23:59
  • 2
    @Karussell, you are free to contribute a fix to the project, which would be more productive than complaining that people who donate their time to a project are too slow to fix an issue important to you. – Paul Mar 25 '13 at 22:02
  • 4
    Lovely open source argument. But I did not complain. It is a fact ;) ! – Karussell Mar 26 '13 at 10:37
49

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

Ernesto Campohermoso
  • 7,213
  • 1
  • 40
  • 51
Thomas Marti
  • 940
  • 9
  • 12
  • and the current date in the property : ${timestamp} More over I had to add a dummy – jpprade Oct 01 '12 at 08:58
  • Sorry for my edit: it looks like `` is used by this goal to format `${buildNumber}`... Very confusing. For those who want build number *and* build date, you need to have two executions for `buildnumber-maven-plugin`: one with `create` goal (and optional configuration for it), and another one for `create-timestamp` goal with customized `` in it's configuration. Mixing configurations does not work. – dma_k Dec 10 '13 at 17:48
  • Ok, I feel a bit a dummy, and without searching online, could someone tell me why I have to use the syntax `{0, date, yyyy-MM-dd HH:mm:ss}` for just saying `yyyy-MM-dd HH:mm:ss`? What does it mean? – reallynice Jul 22 '14 at 11:02
  • 2
    I get an error: org.codehaus.mojo:buildnumber-maven-plugin:1.3:create failed: The scm url cannot be null – DD. Jun 09 '15 at 10:29
25

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
  • 12,815
  • 8
  • 45
  • 68
  • 7
    Note: this doesn't work (as of yet) in filtering resource files – matt b Sep 03 '09 at 17:31
  • 1
    I don't think (nowadays) one *needs* to provide ``. (I myself was fooled into thinking a bare `${maven.build.timestamp}` did not work, but then I was only testing using Eclipse/m2e/WTP. As soon as I run something like `mvn compile`, `${maven.build.timestamp}` is updated just fine.) – Arjan Jan 10 '13 at 11:03
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>
                                <goals>
                                    <goal>create-timestamp</goal>
                                </goals>
                            </pluginExecutionFilter>
                            <action>
                                <execute>
                                    <runOnConfiguration>true</runOnConfiguration>
                                    <runOnIncremental>true</runOnIncremental>
                                </execute>
                            </action>
                        </pluginExecution>
                    </pluginExecutions>
                </lifecycleMappingMetadata>
            </configuration>
        </plugin>
    </plugins>
</pluginManagement>

This tells m2e that you want it to go ahead and run the plugin when building within Eclipse.

Now when you build inside or outside of Eclipse, the timestamp is correctly generated and works with resource filtering!

It's a shame that functionality so simple has to be so hard...

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • This is the only solution that I've ever found to work in RAD/Eclipse. Thank you. – Chris Harris Nov 12 '13 at 19:05
  • Initially I ignored your answer because of all the faffing with the eclipse config that wasn't necessary for me (probably because m2e improved since you wrote it). I posted exactly what worked for me as a separate answer in the hope that it will help people like me :). – AmanicA May 22 '14 at 20:20
11

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}
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
  • 1
    Thanks for this! The BuildNumber plugin appears to be pretty broken for me but this worked like a charm. – JavadocMD Nov 03 '10 at 07:39
9

This worked for me. All I wanted was the timestamp.

In the pom...

<properties>
    <maven.build.timestamp.format>yyyyMMdd-HHmm</maven.build.timestamp.format>
    <dev.build.timestamp>${maven.build.timestamp}</dev.build.timestamp>
</properties>
...
<overlay>
   <groupId>mystuff</groupId>
   <artifactId>mystuff.web</artifactId>
   <filtered>true</filtered>
</overlay>

And in a JSP file...

<div>Built: ${dev.build.timestamp}</div>

Example result is...

<div>Built: 20130419-0835</div>
boz jennings
  • 327
  • 2
  • 6
7

Stick ${build.time} in a properties file and the following in pom.xml:

<build>
   <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>buildnumber-maven-plugin</artifactId>
        <version>1.3</version>
        <configuration>
          <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
          <timestampPropertyName>build.time</timestampPropertyName>
        </configuration>
        <executions>
          <execution>
            <phase>initialize</phase>
            <goals>
              <goal>create-timestamp</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
   </plugins>
</build>

Also see the buildnumber-maven-plugin documentation.


(Other answers got me close especially Garret Wilson but his eclipse config wasn't necessary for me and it caused me to ignore his answer, so I'm posting exactly what worked for me.)

As a bonus if you want to get two properties one for date and one for time, this is how you do it:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>buildnumber-maven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>build.date</id>
      <phase>initialize</phase>
      <goals>
        <goal>create-timestamp</goal>
      </goals>
      <configuration>
        <timestampFormat>yyyy-MM-dd</timestampFormat>
        <timestampPropertyName>build.date</timestampPropertyName>
      </configuration>
    </execution>
    <execution>
      <id>build.time</id>
      <phase>initialize</phase>
      <goals>
        <goal>create-timestamp</goal>
      </goals>
      <configuration>
        <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
        <timestampPropertyName>build.time</timestampPropertyName>
      </configuration>
    </execution>
  </executions>
</plugin>
AmanicA
  • 4,659
  • 1
  • 34
  • 49
  • I had to use dummy scm, and it saved my day I am using this for my CI tools, now jenkins gives daily and nightly build and we can install to nexus and we dont have to enable ovrride. – Manoj Jan 18 '18 at 21:56
5

it's work for me at maven 2.1.0

${maven.build.timestamp}

elciospy
  • 260
  • 4
  • 11