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

2022-09-01 03:19:54

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

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


答案 1

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

但是,如果您愿意使用不同的属性名称来实现目的,则构建-帮助器-maven-插件允许您为各种目的配置自定义时间戳。下面是在生成期间在 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}


答案 2

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

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

推荐