【问题标题】:How to have Maven show local timezone in maven.build.timestamp?如何让 Maven 在 maven.build.timestamp 中显示本地时区?
【发布时间】:2015-04-01 15:35:47
【问题描述】:

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

有什么方法可以指定我想要本地时区而不是 UTC 的时区信息?我简要浏览了 maven 资源,但无论如何都没有看到指定我希望 TZ 是本地 TZ 而不是基于 UTC。

【问题讨论】:

    标签: java maven timezone


    【解决方案1】:

    如前所述,在当前版本的 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},您需要在您的首选时区中的构建时间戳。

    【讨论】:

    • 你说“...... maven.build.timestamp 属性确实允许时区覆盖”。您的意思是说不允许允许时区覆盖吗?
    • :) 谢谢。我花了一段时间搜索页面才意识到
    • 这个时区符号对我不起作用&lt;timeZone&gt;MEZ&lt;/timeZone&gt;,我需要使用&lt;timeZone&gt;Europe/Zurich&lt;/timeZone&gt;
    • 哇,距离我回答这个问题已经有一分钟了。是的,我后来发现了一个类似的问题,即 EST 没有按我的意愿工作,最终也改用美国/底特律。编辑我的答案以适应。谢谢!
    • 如果您正在寻找适合您所在时区的代码,请尝试这里,这些代码对我有用en.wikipedia.org/wiki/List_of_tz_database_time_zones
    【解决方案2】:

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

    按照Maven plugin developers cookbook 中给出的说明,您可以使用&lt;tstamp&gt; 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>
    

    【讨论】:

      【解决方案3】:

      没有解决办法,只能解决。你碰巧使用了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
      【解决方案4】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-26
        • 1970-01-01
        • 1970-01-01
        • 2016-12-03
        • 1970-01-01
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多