2010-04-18 46 views
8

Tôi muốn trích xuất tất cả các thuộc tính từ tệp pom.xml của tôi thành tệp thuộc tính. Đây là những thuộc tính phổ biến như phiên bản phụ thuộc, phiên bản plugin và thư mục. Tôi đang sử dụng plugin thuộc tính-maven, nhưng nó không hoạt động như tôi muốn.properties-maven-plugin: Lỗi khi tải thuộc tính-file

Các phần thiết yếu của pom.xml của tôi:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0-alpha-1</version> 
    <executions> 
    <execution> 
     <phase>initialize</phase> 
     <goals> 
     <goal>read-project-properties</goal> 
     </goals> 
     <configuration> 
     <files> 
      <file>${basedir}/pom.properties</file> 
     </files> 
     </configuration> 
    </execution> 
    </executions> 
</plugin> 

Bây giờ khi tôi chạy "tài sản mvn: đọc-dự án bất động sản" tôi nhận được lỗi sau:

[INFO] One or more required plugin parameters are invalid/missing for 'properties:read-project-properties' 

[0] Inside the definition for plugin 'properties-maven-plugin' specify the following: 

<configuration> 
    ... 
    <files>VALUE</files> 
</configuration>. 

Các pom. thuộc tính-tập tin nằm trong cùng một thư mục như pom.xml. Tôi có thể làm gì để cho plugin thuộc tính-maven đọc thuộc tính của tôi?

EDIT

tôi nộp một vấn đề tại http://jira.codehaus.org/browse/MOJO-1523. Nó đã bị đóng cửa như "không phải là một lỗi", lý do là:

It's by design. The project definition has to be self-contained, otherwise it is no longer complete if it is refered from elsewhere as part of the transitive deps.

Trả lời

0

EDIT2

Xem here cho một workaround sử dụng Nhiệm vụ Ant, mà làm cho trường hợp sử dụng này có thể

5

yếu tố configuration của bạn được định nghĩa bên trong một execution và do đó chỉ áp dụng cho execution này.

Vì vậy, hãy gọi mvn initialize (hoặc sau một giai đoạn sau initialize) để sử dụng configuration của ràng buộc execution hiện tại của bạn.

Hoặc sử dụng một toàn cầu configuration:

<plugin> 
    <groupId>org.codehaus.mojo</groupId> 
    <artifactId>properties-maven-plugin</artifactId> 
    <version>1.0-alpha-2</version> 
    <configuration> 
    <files> 
     <file>etc/config/dev.properties</file> 
     </files> 
    </configuration> 
    ... 
    </plugin> 

Và sau đó gọi

mvn properties:read-project-properties 

Nhưng điều đó sẽ không có ý nghĩa nhiều trong trường hợp cụ thể của plugin này (bạn muốn các thuộc tính có sẵn trong quá trình xây dựng) vì vậy điều này khiến bạn với giải pháp đầu tiên.


Cập nhật: Tôi đã làm một thử nghiệm đứng về phía tôi và, quả thật vậy, với POM sau:

<project> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.stackoverflow</groupId> 
    <artifactId>Q2664362</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0-alpha-2</version> 
     <executions> 
      <execution> 
      <phase>initialize</phase> 
      <goals> 
       <goal>read-project-properties</goal> 
      </goals> 
      <configuration> 
       <files> 
       <file>etc/config/dev.properties</file> 
       </files> 
      </configuration> 
      </execution> 
     </executions> 
     </plugin> 
    </plugins> 
    </build> 
    <dependencies> 
    <dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>${junit.version}</version> 
     <scope>test</scope> 
    </dependency> 
    </dependencies> 
</project> 

Chạy mvn test sẽ không làm việc: maven sẽ cố gắng để tải về junit:jar:${junit.version} (tức là nó không sử dụng giá trị của thuộc tính) và điều này rõ ràng sẽ không thành công.

 
$ mvn test 
[INFO] Scanning for projects... 
[INFO] ------------------------------------------------------------------------ 
[INFO] Building SO - Q2664362 - maven-properties-plugin demo 
[INFO] task-segment: [test] 
[INFO] ------------------------------------------------------------------------ 
[INFO] [properties:read-project-properties {execution: default}] 
[INFO] [resources:resources {execution: default-resources}] 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/main/resources 
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.pom 
[INFO] Unable to find resource 'junit:junit:pom:${junit.version}' in repository central (http://repo1.maven.org/maven2) 
[INFO] [compiler:compile {execution: default-compile}] 
[INFO] Nothing to compile - all classes are up to date 
[INFO] [resources:testResources {execution: default-testResources}] 
[INFO] Using 'UTF-8' encoding to copy filtered resources. 
[INFO] skip non existing resourceDirectory /home/pascal/Projects/stackoverflow/Q2664362/src/test/resources 
Downloading: http://repo1.maven.org/maven2/junit/junit/${junit.version}/junit-${junit.version}.jar 
[INFO] Unable to find resource 'junit:junit:jar:${junit.version}' in repository central (http://repo1.maven.org/maven2) 
[INFO] ------------------------------------------------------------------------ 
[ERROR] BUILD ERROR 
[INFO] ------------------------------------------------------------------------ 
[INFO] Failed to resolve artifact. 
... 

Phần lẻ là việc tải xuống phụ thuộc xảy ra sau properties:read-project-properties. Tôi không chắc chắn nhưng điều này nghe có vẻ giống như một lỗi, bạn nên mở an issue.

+0

Cảm ơn bạn rất nhiều, gợi ý tốt! Lỗi đã biến mất. Nhưng những gì tôi muốn làm vẫn không hoạt động: các thuộc tính trong tệp pom.xml của tôi không được thay thế bằng các thuộc tính trong tệp prop của tôi. Ví dụ. khi tôi gọi "thuộc tính mvn: kiểm tra thuộc tính đọc-dự án", tôi nhận được lỗi như "Thiếu: ---------- 1) junit: junit: jar: $ {junit.version}" Tôi có phải chạy bất kỳ mục tiêu cụ thể nào khác để maven chèn chính xác các thuộc tính tại "thời gian chạy" không? – ifischer

+1

Cảm ơn lời khuyên của bạn. Chỉ cần gửi một vấn đề. Có vẻ như, tôi là người duy nhất cần một "tính năng" như vậy;) – ifischer

3

Hãy thử sử dụng xác nhận giai đoạn thay vì khởi tạo cho maven 3.x (link).

-1

Tôi gặp phải câu hỏi của bạn, nhưng tôi đã cố gắng thêm tài nguyên này tại đây, nó hoạt động tốt.

<build> 
    <resources> 
     <resource> 
      <directory>src/config</directory> //your config folder 
      <filtering>true</filtering> 
     </resource> 
    </resources> 

    <plugin> 
      <groupId>org.codehaus.mojo</groupId> 
      <artifactId>properties-maven-plugin</artifactId> 
      <version>1.0-alpha-2</version> 
      <executions> 
       <execution> 
       <phase>initialize</phase> 
       <goals> 
        <goal>read-project-properties</goal> 
       </goals> 
       <configuration> 
        <files> 
        <file>src/config/config.properties</file> //your config file 
        </files> 
       </configuration> 
       </execution> 
      </executions> 
      </plugin> 
    </build> 
Hope you resolve this as above 
Các vấn đề liên quan