2011-08-22 33 views
29

Tôi có tập tin với một số cấu hình và trong các plugin phần Maven POM, tôi có tomcat maven cắm với một số cấu hình như thế này:tính Reading nộp từ Maven POM nộp

<configuration> 
    <url>http://localhost:8080/manager/html</url> 
    <server>tomcat</server> 
</configuration> 

Tôi muốn xuất thiết lập url vào một số tệp thuộc tính ví dụ: tomcat.properties với khóa đó:

url=http://localhost:8080/manager/html 

Và làm cách nào tôi có thể đọc lại khóa này trong tệp POM của mình?

Trả lời

32

Maven cho phép bạn xác định các thuộc tính trong POM của dự án. Bạn có thể làm điều này bằng cách sử dụng tập tin POM tương tự như sau:

<project> 
    ... 
    <properties> 
     <server.url>http://localhost:8080/manager/html</server.url> 
    </properties> 
    ... 
    <build> 
     <plugins> 
      <plugin> 
      ... 
       <configuration> 
        <url>${server.url}</url> 
        <server>tomcat</server> 
       </configuration> 
      ... 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Bạn có thể tránh xác định tài sản trong thẻ properties, và vượt qua giá trị từ dòng lệnh như:

mvn -Dserver.url=http://localhost:8080/manager/html some_maven_goal 

Bây giờ, nếu bạn không muốn chỉ định chúng từ dòng lệnh và nếu bạn cần tách biệt thêm các thuộc tính này từ dự án POM, thành tệp thuộc tính, thì bạn sẽ cần sử dụng Properties Maven plugin và chạy mục tiêu read-project-properties trong initialize phase of the Maven lifecycle . Ví dụ từ trang plugin được sao chép ở đây:

<project> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0-alpha-2</version> 
     <executions> 
      <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. --> 
      <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> 
</project> 
+0

Công trình này !! .. Cảm ơn bạn!! –

4

Nó không phải là thực sự có thể tải các thuộc tính từ một tập tin bằng cách sử dụng hướng dẫn trong câu trả lời chấp nhận như là các đặc tính này không có sẵn trong các tập tin pom mặc dù chúng có thể được sử dụng để lọc. Minimal quầy dụ:

Trong pom.xml:

<build> 
    <plugins> 
    <plugin> 
     <groupId>org.codehaus.mojo</groupId> 
     <artifactId>properties-maven-plugin</artifactId> 
     <version>1.0-alpha-2</version> 
     <executions> 
     <!-- Associate the read-project-properties goal with the initialize phase, to read the properties file. --> 
     <execution> 
      <!-- Apart from this test, the phase must be initialize --> 
      <phase>validate</phase> 
      <goals> 
      <goal>read-project-properties</goal> 
      </goals> 
      <configuration> 
      <files> 
       <file>dev.properties</file> 
      </files> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    <plugin> 
     <groupId>org.apache.maven.plugins</groupId> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <version>1.6</version> 
     <executions> 
     <execution> 
      <phase>validate</phase> 
      <goals> 
      <goal>run</goal> 
      </goals> 
      <configuration> 
      <target> 
       <echo>Displaying value of properties</echo> 
       <echo>[foo] ${foo}</echo> 
      </target> 
      </configuration> 
     </execution> 
     </executions> 
    </plugin> 
    </plugins> 
</build> 

Có trong dev.properties:

foo=bar 

Sau đó runnin lệnh mvn validate sản xuất đầu ra:

[echo] Displaying value of properties 
[echo] [foo] bar 
+0

Bạn không thành công trong chuỗi ** [echo] [foo] $ {foo} **. Grrr .... – gavenkoa

+0

Đó là vì ** xác thực ** được thực thi trước ** khởi tạo **. – gavenkoa

+0

@gavenkoa bạn có thể giải thích làm thế nào tôi thất bại trong chuỗi đó? –

8

Toàn bộ ví dụ làm việc có sẵn tại: http://hg.defun.work/exp/file/tip/maven/properties

phần Ở đây thiết yếu của pom.xml:

<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> 
     </execution> 
    </executions> 
    <configuration> 
     <files> 
     <file>dev.properties</file> 
     </files> 
    </configuration> 
    </plugin> 

    <plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.6</version> 
    <executions> 
     <execution> 
     <phase>compile</phase> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     <configuration> 
      <target> 
      <echo>project.build.sourceEncoding is "${project.build.sourceEncoding}"</echo> 
      <echo>foo is "${foo}"</echo> 
      <echo>with-spaces is "${with-spaces}"</echo> 
      <echo>existent.property is "${existent.property}"</echo> 
      <echo>nonexistent.property is "${nonexistent.property}"</echo> 
      </target> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 
</plugins> 

Như bạn thấy tính-maven-plugin vẫn ở alpha sân khấu, đó là lý do tại sao tôi ghét Maven như xây dựng các công cụ. ..