How to have Maven show local timezone in maven.bui

2019-03-12 09:12发布

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.

4条回答
淡お忘
2楼-- · 2019-03-12 09:26

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.

查看更多
女痞
3楼-- · 2019-03-12 09:31

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>
查看更多
女痞
4楼-- · 2019-03-12 09:34

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>
查看更多
疯言疯语
5楼-- · 2019-03-12 09:35

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.

查看更多
登录 后发表回答