如何让Maven在maven.build.timestamp中显示本地时区?

时间:2015-02-02 16:16:04

标签: java maven timezone

在Maven 3.2.2 +中,maven.build.timestamp已重新定义,以MNG-5452显示UTC时间。

有没有办法在我的本地时区指定我想要时区信息而不是UTC?我简要地浏览了maven源代码,但是无论如何都没有看到我想要TZ是本地TZ而不是基于UTC。

3 个答案:

答案 0 :(得分:17)

如前所述,在当前版本的Maven中(至少版本为3.3。+),maven.build.timestamp属性不允许时区覆盖。

但是,如果您可以为您的目的使用不同的属性名称,build-helper-maven-plugin允许您为各种目的配置自定义时间戳。以下是在构建期间配置EST中当前时间戳的示例。

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

然后,您可以使用${build.time}属性而不是${maven.build.timestamp},而您需要在首选时区中构建时间戳。

答案 1 :(得分:2)

我认为没有纯Maven解决方案,但您可以使用Ant任务。

按照Maven plugin developers cookbook中的说明,您可以使用filter.properties ant任务生成<tstamp>文件。在此元素中,您可以使用与SimpleDateFormat class相同的日期/时间模式自定义时间戳,并使用Timezone class。然后,您可以使用${build.time},默认情况下,它将使用您当地的时区。

1)使用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)激活过滤

<resources>
  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>
</resources>
<filters>
  <filter>${basedir}/target/filter.properties</filter>
</filters>

答案 2 :(得分:0)

我所做的但可能不适用于其他人的是,我以bash格式导出了一个环境变量,例如

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

,然后在构建我的项目时在pom.xml中使用它。

<properties>
    <timestamp>${buildtimestamp}</timestamp>
</properties>