How to access maven.build.timestamp for resource filtering

MavenTimestampMaven 3

Maven Problem Overview


I am using maven 3.0.4 and would like to make the build timestamp accessible to my application. For this, I'm putting a placeholder in a .properties file and let maven filter on build. While this is working fine for ${project.version}, ${maven.build.timestamp} is not substituted on filtering.

The property seems to be available on build - I can use it to modify the artifact name:

<finalName>${project.artifactId}-${maven.build.timestamp}</finalName>

So why is it not available for resource filtering? And, more importantly, how do I make it accessible?

Maven Solutions


Solution 1 - Maven

I have discovered this article, explaining that due to a bug in maven, the build timestamp does not get propagated to the filtering. The workaround is to wrap the timestamp in another property:

<properties>
   <timestamp>${maven.build.timestamp}</timestamp>
   <maven.build.timestamp.format>yyyy-MM-dd HH:mm</maven.build.timestamp.format>
</properties>

Filtering then works as expected for

buildTimestamp=${timestamp}

Solution 2 - Maven

I can confirm as of Maven 3.x {maven.build.timestamp} is "working" now. They work arounded the problem, apparently. No additional properties workaround needed anymore.

However, be careful your "filtering" plugin (maven-resources-plugin) is up to date. It needs to be relatively new, so if mvn help:effective-pom shows an old version (ex: 2.6), bump it to something newer, fixed it for me, 3.x ex:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-resources-plugin</artifactId>
  <version>3.1.0</version>
</plugin>

<properties><timestamp>... workaround is no longer required...

This also cleared up, kind of, why it was working in IntelliJ but not the command line. IntelliJ probably uses their own "modified/internal" maven constants, so it was working there, but not from maven command line.

Also note if you add a filtering resource directory to you pom, you may need to also "re-add" the default directory, it gets lost, ex:

  <resource>
    <directory>src/main/resources-filtered</directory> <!-- to get "maven.build.timestamp" into resource properties file -->
    <filtering>true</filtering>
  </resource>
  <resource>
    <directory>src/main/resources</directory> <!-- apparently have to add this is you have the other... -->
  </resource>

NB if you're using spring boot as your parent, you have to use @maven.build.timestamp@ instead. Also note if you're using spring boot there's a file META-INF/build-info.properties that is optionally created by the spring-boot-maven-plugin that you can read (spring provides a BuildProperties bean for convenience reading it).

Solution 3 - Maven

In order to enrich the Stackoverflow content for others, that like me, found this post as a way to solve the "problem" of ${maven.build.timestamp}. This is not a maven bug, but an expected behavior of m2e, as can be seen in this post.

Therefore, I believe that we can not expect the solution to be "corrected", since, from what I understand, the correction involves conceptual issues.

In my case, what I did was use the plugin (buildnumber-maven-plugin) as described in this other post.

Solution 4 - Maven

Adding Maven properties at the pom project level doesn't take into account correct local Timezone, so timestamp may appear wrong :

<properties><timestamp>${maven.build.timestamp}</timestamp></properties>

Using the build-helper-maven-plugin applies the correct timezone and current daylight saving to the timestamp :

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9.1</version>
            <executions>
                <execution>
                    <id>timestamp-property</id>
                    <goals>
                        <goal>timestamp-property</goal>
                    </goals>
                    <configuration>
                        <name>timestamp</name>
                        <pattern>yyyy-MM-dd HH:mm:ss</pattern>
                        <timeZone>Europe/Zurich</timeZone>
                    </configuration>
                </execution>
            </executions>
        </plugin>
     </plugins>
     <resources>
         <resource>
             <directory>src/main/resources</directory>
             <filtering>true</filtering>
         </resource>
     </resources>
 </build>

When packaging, Maven will replace any token timestamp in /resources folder, e.g. resources/version.properties :

build.timestamp=${timestamp}

You can then load this properties file in your Application.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionkostjaView Question on Stackoverflow
Solution 1 - MavenkostjaView Answer on Stackoverflow
Solution 2 - MavenrogerdpackView Answer on Stackoverflow
Solution 3 - MavenBob RiversView Answer on Stackoverflow
Solution 4 - MavenskayView Answer on Stackoverflow