2010-11-02 31 views
10

Tôi đang cố gắng tìm hiểu xem Maven có một số trình cắm thêm có sẵn có thể được sử dụng cho các tạo phẩm thời gian đóng dấu hay không. Tôi tạo ra một tập tin lắp ráp và đang sử dụng các plugin maven-lắp ráp để tạo ra một phân phối cuối cùng (lọ, tài liệu, kịch bản, vv). Tôi muốn đặt tên tệp phân phối này là domain_year_month_day.zip. Làm thế nào tôi có thể nối thêm phần ngày của dấu thời gian vào cuối tệp zip cuối cùng đang được tạo. Cảm ơn.Làm cách nào để tạo dấu thời gian cho một tạo phẩm đầu ra trong Maven?

Trả lời

8

Bạn có thể sử dụng maven-timestamp-plugin để đặt thuộc tính (ví dụ: timestamp) và sử dụng sau này trong tên cuối cùng của hội đồng của bạn.

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

Là một thay thế, bạn có thể đặt một số mã Groovy trong POM của bạn bằng cách sử dụng GMaven plugin:

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

Một đầu ra mẫu cho thấy tài sản:

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

Và một lần nữa, sử dụng này tài sản sau này trong tên xây dựng của hội đồng của bạn.

+0

này là rất tốt. Cảm ơn một lần nữa cho câu trả lời và thời gian của bạn Pascal. – John

+1

Sau đó chấp nhận nó bạn của tôi: D –

0

nếu bạn sử dụng Hudson/Jenkins, bạn chỉ có thể sử dụng biến $ {BUILD_ID} để nhận loại dấu thời gian cho bất kỳ tệp thuộc tính nào bạn muốn chỉnh sửa.

thông tin cho các biến môi trường khác Hudson/Jenkins hỗ trợ, hãy xem ở đây: http://wiki.hudson-ci.org/display/HUDSON/Building+a+software+project

16

Bạn không cần maven-timestamp-plugin với các phiên bản mới hơn của maven. Kể từ 2.1'ish, Maven đã cung cấp thuộc tính đặc biệt maven.build.timestamp.

Bạn đặt định dạng trong các thuộc tính pom với một cái gì đó như thế này:

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

Và sau đó sử dụng $ {} maven.build.timestamp bất cứ nơi nào bạn cần một tài sản timestamp. Xem http://maven.apache.org/guides/introduction/introduction-to-the-pom.html để biết chi tiết.

4

Như ${maven.build.timestamp} dường như lỗi trong maven, thực hiện giải pháp như sau:

Tạo biến mới (tôi đã chọn "build.timestamp", tại đây) - và, tùy chọn, chỉ định định dạng:

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> 

... 

Sử dụng biến tùy chỉnh từ bất cứ đâu:

0.123.

số-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> 
Các vấn đề liên quan