10

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?

1 Answer 1

24

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.