如何在Maven中为输出工件添加时间戳?

Joh*_*ohn 11 maven-2 maven-3 maven maven-assembly-plugin

我试图找出Maven是否有一些可用于对工件进行时间戳的内置插件.我创建了一个程序集文件,并使用maven-assembly插件创建最终分发(jar,docs,scripts等).我想将此分发文件命名为domain_year_month_day.zip.如何将时间戳的日期部分附加到正在生成的最终zip文件的末尾.谢谢.

lre*_*der 16

你不需要maven-timestamp-plugin和更新版本的maven.从2.1'开始,Maven提供了特殊属性maven.build.timestamp.

您可以使用以下内容在pom属性中设置格式:

<maven.build.timestamp.format>yyyy-MM-dd'T'HH.mm.ss</maven.build.timestamp.format>
Run Code Online (Sandbox Code Playgroud)

然后在需要时间戳属性的地方使用$ {maven.build.timestamp}.有关详细信息,请参见 http://maven.apache.org/guides/introduction/introduction-to-the-pom.html.


Pas*_*ent 9

您可以使用maven-timestamp-plugin设置属性(例如timestamp),稍后在程序集的最终名称中使用它.

<plugin>
   <artifactId>maven-assembly-plugin</artifactId>
   <executions>
       <execution>
           <id>create-assembly</id>
           <phase>package</phase>
           <goals>
               <goal>single</goal>
           </goals>
           <configuration>
               <appendAssemblyId>false</appendAssemblyId>
               <finalName>domain_${timestamp}</finalName>
               <descriptors>
                   <descriptor>src/main/assembly/my-descriptor.xml</descriptor>
               </descriptors>
               <attach>true</attach>
           </configuration>
       </execution>
   </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您可以使用GMaven插件在您的POM中放置一些Groovy代码:

<plugin>
  <groupId>org.codehaus.gmaven</groupId>
  <artifactId>gmaven-plugin</artifactId>
  <version>1.3</version>
  <executions>
    <execution>
      <id>set-custom-property</id>
      <phase>initialize</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          def timestamp = new Date().format('MM_dd_yy')
          project.properties.setProperty('timestamp', timestamp)
        </source>
      </configuration>
    </execution>
    <execution><!-- for demonstration purpose -->
      <id>show-custom-property</id>
      <phase>generate-resources</phase>
      <goals>
        <goal>execute</goal>
      </goals>
      <configuration>
        <source>
          println project.properties['timestamp']
        </source>
      </configuration>
    </execution>
  </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

显示属性的示例输出:

$ mvn generate-resources 
[INFO] Scanning for projects...
[INFO]                                                                         
...
[INFO] --- gmaven-plugin:1.3:execute (set-custom-property) @ Q4081274 ---
[INFO] 
[INFO] --- gmaven-plugin:1.3:execute (show-custom-property) @ Q4081274 ---
11_02_10
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
...

同样,稍后在程序集的构建名称中使用此属性.


Gau*_*lio 6

由于${maven.build.timestamp}maven中似乎有bug,解决方法如下:

创建一个新变量(我在此处选择“build.timestamp”) -并且可以选择指定格式:

pom.xml

<project>
    ...
    <properties>
        ...
        <build.timestamp>${maven.build.timestamp}</build.timestamp>
        <maven.build.timestamp.format>yyyyMMdd</maven.build.timestamp.format> 
                     <!-- default is: yyyyMMdd-HHmm -->
    </properties>
    <build>
    ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <descriptors>
                    <descriptor>some-assembly.xml</descriptor>
                </descriptors>
            </configuration>
            <executions>
                <execution>
                    <id>make</id>
                    <phase>package</phase>
                    <goals>
                        <goal>assembly</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

...
Run Code Online (Sandbox Code Playgroud)

从任何地方使用自定义变量:

一些程序集.xml

<?xml version="1.0"?>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
    <id>release-${build.timestamp}</id>
    <baseDirectory>/</baseDirectory>
    <includeBaseDirectory>false</includeBaseDirectory>
    <formats>
      <format>zip</format>
    </formats>
    <fileSets>
      <fileSet>
        <directory>${project.build.directory}/${project.artifactId}-${project.version}</directory>
      </fileSet>
    </fileSets>
</assembly>
Run Code Online (Sandbox Code Playgroud)