Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven property for current year

Tags:

maven

I have a maven project that generates a properties file during a build. One of the properties I would like to include is the copyright dates for the project.

I have the property defined as such:

<properties>
    <copyright-years>${project.inceptionDate}-2016</copyright-years>
</properties>

I'm using the build.timestamp property elsewhere.

How can I replace the 2016 in the property with the current year, ideally by changing the timestamp format in just this one instance?

like image 614
Randall Avatar asked Sep 03 '16 12:09

Randall


People also ask

What is Maven build timestamp?

maven.build.timestamp. the UTC timestamp of build start, in yyyy-MM-dd'T'HH:mm:ss'Z' default format, which can be overridden with maven.build.timestamp.format POM property.

What are Maven properties?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.

What is Basedir in POM xml?

basedir : The directory that the current project resides in. This means this points to where your Maven projects resides on your system. It corresponds to the location of the pom. xml file.

Where is Maven properties located?

You can also reference any properties in the Maven Local Settings file which is usually stored in ~/. m2/settings. xml. This file contains user-specific configuration such as the location of the local repository and any servers, profiles, and mirrors configured by a specific user.


1 Answers

You can use the build-helper-maven-plugin for this task. It has a timestamp-property goal that can be used to store a timestamp with a given format into a Maven property:

Sets a property based on the current date and time.

The advantage is that it doesn't force you to define a specific maven.build.timestamp.format, which would not be convenient if you intend to format the current date in a second way elsewhere. A sample configuration would be:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>build-helper-maven-plugin</artifactId>
  <version>1.12</version>
  <executions>
    <execution>
      <id>timestamp-property</id>
      <goals>
        <goal>timestamp-property</goal>
      </goals>
      <phase>validate</phase>
      <configuration>
        <name>current.year</name>
        <pattern>yyyy</pattern>
      </configuration>
    </execution>
  </executions>
</plugin>

This will store the current year in the current.year Maven property. This is done at the validate phase, which is the first phase executed, so that all of the rest of the build can use it with ${current.year}.

like image 66
Tunaki Avatar answered Sep 29 '22 14:09

Tunaki