40

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.

Eric B.
  • 23,425
  • 50
  • 169
  • 316

4 Answers4

32

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.

alexanderific
  • 780
  • 7
  • 16
  • 1
    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 Jul 12 '16 at 01:38
  • :) thanks for that. Took me a while of searching the page to realise – Matt Byrne Jul 12 '16 at 12:03
  • This timezone notation didn't work for me `MEZ`, I need to use `Europe/Zurich` – myborobudur Aug 27 '18 at 13:37
  • 1
    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! – alexanderific Aug 29 '18 at 16:00
  • If you are looking for code for your time zone try here, those worked for me https://en.wikipedia.org/wiki/List_of_tz_database_time_zones – hocikto Apr 25 '19 at 09:43
  • 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. – ChRoNoN Jun 27 '19 at 13:13
  • In my case, timezone only works when specified like this `GMT+04:00` – hguser Sep 05 '19 at 00:49
  • The best answer is this: It should be mark as the best one. Thanks for sharing @alexanderific – nosequeweaponer Apr 28 '20 at 23:25
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>
Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
0

There's no solution but to workaround. Do you happened to use maven buildnumber-maven-plugin plugin? If so, you can use it to generate revision for you and build timestamp. This timestamp will be with based on your local java time zone configuration.

Edit: Nevertheless question is about timestamp, as Dean Schulze pointed out, only first execution will break ${buildNumber}. To fix that you'll have to add another execution to your configuration that will create buildRevision. Updated example, below. For ex.:`

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>buildnumber-maven-plugin</artifactId>
<version>1.3</version>
<inherited>true</inherited>
<executions>
    <execution>
        <id>generate-timestamp</id>
        <phase>validate</phase>
        <goals>
            <goal>create</goal>
        </goals>
        <configuration>
            <format>{0,date,yyyy-MM-dd HH:mm:ss Z}</format>
            <items>
                <item>timestamp</item>
            </items>
            <buildNumberPropertyName>buildDateTime</buildNumberPropertyName>
            <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
        </configuration>
    </execution>
    <execution>
        <id>generate-buildnumber</id>
        <phase>validate</phase>
        <goals>
            <goal>create</goal>
        </goals>
        <configuration>
            <revisionOnScmFailure>0</revisionOnScmFailure>
            <useLastCommittedRevision>true</useLastCommittedRevision>
            <buildNumberPropertyName>buildRevision</buildNumberPropertyName>
        </configuration>
    </execution>
</executions>

Than you can use ${buildDateTime} where you want to inject your timestamp variable. Another execution with the same goal will store you revision as well.

Community
  • 1
  • 1
Augustin Ghauratto
  • 1,420
  • 1
  • 19
  • 21
0

What I did but might not apply to other people though, is that I export an environment variable in bash like

buildtimestamp=$(date +%H:%M)

and then consume it in the pom.xml when building my project.

<properties>
    <timestamp>${buildtimestamp}</timestamp>
</properties>
Ognjen Mišić
  • 1,219
  • 17
  • 37