How to access maven.build.timestamp to filter resources

I am using maven 3.0.4 and want to make the build timestamp available for my application. To do this, I put the placeholder in the .properties file and enable the maven filter during assembly. Although this works fine for ${project.version} , ${maven.build.timestamp} not substituted when filtering.

The property seems to be buildable - I can use it to change the name of the artifact:

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

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

+97
timestamp maven maven-3
Nov 05 '12 at 8:35
source share
4 answers

I found this article explaining that due to an error in maven , timestamp assemblies do not apply to 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 works as expected for

 buildTimestamp=${timestamp} 
+200
Nov 05
source share

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

Therefore, I believe that we cannot expect the solution to be β€œfixed”, because, as I understand it, the correction is related to conceptual problems.

In my case, I used the plugin ( buildnumber-maven-plugin ) as described in this other post .

+2
Feb 06 '15 at 12:06
source share

I can confirm that with Maven 3.x {maven.build.timestamp} now "works." Apparently, they are working on this problem. No additional properties are required.

However, be careful, your maven-resources-plugin has been updated. It should be relatively new, so if mvn help:effective-pom shows the old version (for example, 2.6), increase it to something newer, fix 3.x ex for me:

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

It also made it clear, for some reason, why it worked in IntelliJ and not on the command line. IntelliJ probably uses its own "modified / internal" maven constants, so it worked there, but not from the maven command line.

Also note that if you add a filtering resources directory to your pom, you may also need to β€œre-add” the default directory, it is lost, for example:

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

Note: if you use spring loading as a parent, you should use @ maven.build.timestamp @ instead . Also note that if you use spring boot, there is a META-INF/build-info.properties which, optionally, creates a spring-boot-maven-plugin that you can read (for readability, BuildProperties provides the BuildProperties component of the BuildProperties ).

+2
Oct 23 '18 at 0:07
source share

Adding Maven properties at the project pom level does not take into account the correct local time zone, so the timestamp may not display correctly:

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

Using the build-helper-maven-plugin module applies the correct time zone and current daylight saving time 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 timestamp in the / resources folder, for example resources / version.properties:

build.timestamp = $ {} Timestamp

Then you can load this properties file in your application.

0
Jan 28 '19 at 8:13
source share



All Articles