【发布时间】:2015-04-01 15:35:47
【问题描述】:
在 Maven 3.2.2+ 中,maven.build.timestamp 已重新定义为以 UTC 显示时间,与 MNG-5452 一致。
有什么方法可以指定我想要本地时区而不是 UTC 的时区信息?我简要浏览了 maven 资源,但无论如何都没有看到指定我希望 TZ 是本地 TZ 而不是基于 UTC。
【问题讨论】:
在 Maven 3.2.2+ 中,maven.build.timestamp 已重新定义为以 UTC 显示时间,与 MNG-5452 一致。
有什么方法可以指定我想要本地时区而不是 UTC 的时区信息?我简要浏览了 maven 资源,但无论如何都没有看到指定我希望 TZ 是本地 TZ 而不是基于 UTC。
【问题讨论】:
如前所述,在当前版本的 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},您需要在您的首选时区中的构建时间戳。
【讨论】:
<timeZone>MEZ</timeZone>,我需要使用<timeZone>Europe/Zurich</timeZone>
我认为没有纯 Maven 解决方案,但您可以使用 Ant 任务。
按照Maven plugin developers cookbook 中给出的说明,您可以使用<tstamp> ant 任务生成filter.properties 文件。在此元素中,您可以使用与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>
【讨论】:
没有解决办法,只能解决。你碰巧使用了mavenbuildnumber-maven-plugin插件吗? 如果是这样,您可以使用它为您生成修订并构建时间戳。此时间戳将基于您的本地 java 时区配置。
编辑:不过问题是关于timestamp,正如Dean Schulze 指出的那样,只有第一个execution 会破坏${buildNumber}。要解决这个问题,您必须将另一个 execution 添加到您的配置中,这将创建 buildRevision。更新示例,如下。
例如:`
<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>
您可以在要注入时间戳变量的地方使用${buildDateTime}。另一个具有相同目标的执行也会存储您的修订版本。
【讨论】:
${buildNumber}
buildRevision
我所做的但可能不适用于其他人的是,我在 bash 中导出了一个环境变量,例如
buildtimestamp=$(date +%H:%M)
然后在构建我的项目时在 pom.xml 中使用它。
<properties>
<timestamp>${buildtimestamp}</timestamp>
</properties>
【讨论】: