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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28281988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:55:09  来源:igfitidea点击:

How to have Maven show local timezone in maven.build.timestamp?

javamaventimezone

提问by Eric B.

In Maven 3.2.2+, the maven.build.timestamphas been redefined to show the time in UTC, as per MNG-5452.

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

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.

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

回答by Ortomala Lokni

I think there is no pure Maven solution but you can use an Ant task.

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

Following the instructions given in the Maven plugin developers cookbook, you can generate a filter.propertiesfile with the <tstamp>ant task. In this element, you can customize your timestamp with the same date/time pattern as the SimpleDateFormat classand also use the Timezone class. You can then use ${build.time}, by default it will use your local timezone.

按照Maven 插件开发人员手册 中给出的说明,您可以生成一个filter.properties包含<tstamp>ant 任务的文件。在此元素中,您可以使用与SimpleDateFormat 类相同的日期/时间模式自定义时间戳,还可以使用Timezone 类。然后您可以使用${build.time},默认情况下它将使用您的本地时区。

1)Use the maven-antrun-plugin

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)Activate filtering

2)激活过滤

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

回答by Augustin Ghauratto

There's no solution but to workaround. Do you happened to use maven buildnumber-maven-pluginplugin? 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.

没有解决办法,只能解决。你碰巧用过 maven buildnumber-maven-plugin插件吗?如果是这样,您可以使用它为您生成修订并构建时间戳。此时间戳将基于您的本地 java 时区配置。

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

编辑:不过timestamp,正如迪恩舒尔茨指出的那样,问题是关于,只有 firstexecution会打破${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>

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.

比您可以使用${buildDateTime}要注入时间戳变量的位置。具有相同目标的另一个执行也将存储您的修订。

回答by alexanderific

As already mentioned, in the current versions of Maven (at least up to version 3.3.+), the maven.build.timestampproperty does not allow for timezone overrides.

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

However, if you are okay with utilizing a different property name for your purposes, the build-helper-maven-pluginallows you to configure custom timestamps for a variety of purposes. Here is an example to configure the current timestamp in EST during a build.

但是,如果您可以为您的目的使用不同的属性名称,则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>

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.

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

回答by Ognjen Mi?i?

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

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

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

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

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

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