2010-11-02 49 views
10

我想知道Maven是否有一些內置的插件可用於時間戳工件。我創建了一個程序集文件,並使用maven-assembly插件創建最終分發(jar,docs,scripts等)。我想將此分發文件命名爲domain_year_month_day.zip。如何將時間戳的日期部分附加到正在生成的最終壓縮文件的末尾。謝謝。如何在Maven中輸出工件時間戳?

回答

8

您可以使用maven-timestamp-plugin來設置屬性(例如timestamp),並稍後在組件的最終名稱中使用它。顯示性能

<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> 

輸出示例:

<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> 

作爲替代方案,你可以使用GMaven plugin把一些Groovy代碼在你的POM

 
$ 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] ------------------------------------------------------------------------ 
... 

再次,使用此屬性稍後在您的程序集的構建名稱中。

+0

這是偉大的。再次感謝您的回答和時間帕斯卡爾。 – John 2010-11-03 11:38:24

+1

然後接受它我的朋友:D – 2010-11-17 14:19:44

16

你不需要Maven的時間戳的插件與行家的新版本。自2.1版本以來,Maven提供了特殊的屬性maven.build.timestamp。

您設置的格式在像這樣的POM屬性:

<maven.build.timestamp.format>yyyy-MM-dd'T'HH.mm.ss</maven.build.timestamp.format> 

然後用$ {} maven.build.timestamp無論你需要一個timestamp屬性。有關詳細信息,請參閱http://maven.apache.org/guides/introduction/introduction-to-the-pom.html

4

由於${maven.build.timestamp}似乎Buggy在Maven的,解決方法如下:

創建一個新的變量(我選擇了 「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> 

... 

使用自定義從任何地方變量:

一些-assembly.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>