How to have Maven show local timezone in maven.build.timestamp?

25,912

Solution 1

As already mentioned, in the current versions of Maven (at least up to version 3.3.+), the maven.build.timestamp property does not allow for timezone overrides.

However, if you are okay with utilizing a different property name for your purposes, the build-helper-maven-plugin allows you to configure custom timestamps for a variety of purposes. Here is an example to configure the current timestamp in EST during a build.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.10</version>
            <executions>
                <execution>
                    <id>timestamp-property</id>
                    <goals>
                        <goal>timestamp-property</goal>
                    </goals>
                    <configuration>
                        <name>build.time</name>
                        <pattern>MM/dd/yyyy hh:mm aa</pattern>
                        <locale>en_US</locale>
                        <timeZone>America/Detroit</timeZone>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Then you can utilize the ${build.time} property instead of ${maven.build.timestamp} where you need a timestamp of the build in your preferred timezone.

Solution 2

I think there is no pure Maven solution but you can use an Ant task.

Following the instructions given in the Maven plugin developers cookbook, you can generate a filter.properties file with the <tstamp> ant task. In this element, you can customize your timestamp with the same date/time pattern as the SimpleDateFormat class and also use the Timezone class. You can then use ${build.time}, by default it will use your local timezone.

1)Use the maven-antrun-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
      <execution>
        <phase>generate-resources</phase>
        <goals>
          <goal>run</goal>
        </goals>
        <configuration>
          <tasks>
            <!-- Safety -->
            <mkdir dir="${project.build.directory}"/>
            <tstamp>
              <format property="last.updated" pattern="yyyy-MM-dd HH:mm:ss"/>
            </tstamp>
            <echo file="${basedir}/target/filter.properties" message="build.time=${last.updated}"/>
          </tasks>
        </configuration>
      </execution>
    </executions>
</plugin>

2)Activate filtering

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
<filters>
  <filter>${basedir}/target/filter.properties</filter>
</filters>
Share:
25,912
Eric B.
Author by

Eric B.

Updated on November 30, 2020

Comments

  • Eric B.
    Eric B. over 3 years

    In Maven 3.2.2+, the maven.build.timestamp has been redefined to show the time in UTC, as per MNG-5452.

    Is there any way to specify that I want the timezone info in my local timezone and not in UTC? I briefly browsed the maven sources, but do not see anyway to specify that I want the TZ to be local TZ and not UTC based.

  • Matt Byrne
    Matt Byrne almost 8 years
    You say "... the maven.build.timestamp property does allow for timezone overrides". Did you mean to say does not allow for timezone overrides?
  • Matt Byrne
    Matt Byrne almost 8 years
    :) thanks for that. Took me a while of searching the page to realise
  • Dean Schulze
    Dean Schulze over 7 years
    This overwrites the ${buildNumber}
  • Augustin Ghauratto
    Augustin Ghauratto over 7 years
    I edited, and added another execution that will create correct buildRevision
  • myborobudur
    myborobudur over 5 years
    This timezone notation didn't work for me <timeZone>MEZ</timeZone>, I need to use <timeZone>Europe/Zurich</timeZone>
  • alexanderific
    alexanderific over 5 years
    Wow, been a minute since I've been to this answer. Yeah, I later found a similar issue with EST not working how I'd like and ended up switching to America/Detroit instead too. Editing my answer to accommodate. Thanks!
  • hocikto
    hocikto about 5 years
    If you are looking for code for your time zone try here, those worked for me en.wikipedia.org/wiki/List_of_tz_database_time_zones
  • ChRoNoN
    ChRoNoN almost 5 years
    This not always solve thee problem. In my case I use timestamp as directory name when build release versions so I can keep older ones if needed.
  • hguser
    hguser over 4 years
    In my case, timezone only works when specified like this GMT+04:00
  • nosequeweaponer
    nosequeweaponer about 4 years
    The best answer is this: It should be mark as the best one. Thanks for sharing @alexanderific