2013-08-08 32 views
15

Tôi cố gắng để vượt qua tính maven (được định nghĩa thông qua hồ sơ) một thi antrun:Maven antrun: pass tính maven để ant

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.7</version> 
    <dependencies> 
     <!-- ... these are ok --> 
    </dependencies> 
    <executions> 
     <execution> 
      <configuration> 
       <target> 
        <property name="ant_destDir" value="${destDir}" /> 
        <property name="ant_serverDeploy" value="${serverDeploy}" /> 
        <property name="ant_deployDir" value="${deployDir}" /> 
        <property name="ant_userDeploy" value="${userDeploy}" /> 
        <property name="ant_passwordDeploy" value="${passwordDeploy}" /> 
        <!-- correct task definitions for sshexec and scp --> 
        <sshexec host="${serverDeploy}" username="${userDeploy}" 
          password="${passwordDeploy}" trust="yes" 
          command="some command" /> 
        <scp remoteTodir="${userDeploy}@${serverDeploy}:${destDir}" 
          password="${passwordDeploy}" trust="yes" sftp="true"> 
         <fileset dir="${deployDir}" includes="*.jar" /> 
        </scp> 
        <sshexec host="${serverDeploy}" username="${userDeploy}" 
          password="${passwordDeploy}" trust="yes" 
          command="some command" /> 
       </target> 
      </configuration> 
      <phase>install</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

Các thuộc tính được định nghĩa trong các cấu hình để cho phép triển khai trong các máy chủ khác nhau (tôi biết nó không phải là phương pháp tốt nhất có thể, nhưng đây là cách mọi thứ được thực hiện ở đây), như thế này:

<profile> 
    <id>aprofile</id> 
    <properties> 
     <property name="serverDeploy" value="somevalue" /> 
     <property name="userDeploy" value="someuser" /> 
     <property name="passwordDeploy" value="somepassword" /> 
     <!-- and so on --> 
    </properties> 
</profile> 

vấn đề của tôi là tôi không thể có được tài sản maven để làm việc trong plugin kiến; Tôi đã cố gắng để thêm một nhiệm vụ <echoproperties> trong kiến ​​để xem những thuộc tính tôi có và không có dấu vết của tài sản maven. Có thể sử dụng các đặc tính được xác định của maven hay tôi nên sử dụng cách tiếp cận khác? Bất kỳ đề nghị được chào đón.

Edit: Tôi đã sửa đổi kịch bản như mỗi câu trả lời đầu tiên, nó vẫn không hoạt động

Trả lời

17

Bạn có thể vượt qua các thuộc tính bằng cách định nghĩa các thuộc tính Ant mới (sử dụng thẻ property trong target của bạn trong configuration). Vì vậy, ví dụ:

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
          http://maven.apache.org/maven-v4_0_0.xsd"> 

    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.test</groupId> 
    <artifactId>test-module</artifactId> 
    <name>test-module</name> 
    <version>SNAPSHOT</version> 

    <properties> 
     <my.custom.property>false</my.custom.property> 
    </properties> 

    <profiles> 
     <profile> 
      <id>customProfile</id> 
      <properties> 
       <my.custom.property>true</my.custom.property> 
      </properties> 
     </profile> 
    </profiles> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-antrun-plugin</artifactId> 
       <version>1.7</version> 
       <executions> 
        <execution> 
         <id>compile</id> 
         <phase>compile</phase> 
         <configuration> 
          <target> 
           <property name="antProperty" value="${my.custom.property}"/> 
           <echo message="Custom Ant Property is: ${antProperty}"/> 
           <echoproperties /> 
          </target> 
         </configuration> 
         <goals> 
          <goal>run</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

Khi tôi thực hiện mvn compile trên pom này đầu ra là:

main: 
[echo] Custom Ant Property is: false 
[echoproperties] #Ant properties 
[echoproperties] #Thu Aug 08 17:17:30 CEST 2013 
[echoproperties] ant.project.name=maven-antrun- 
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010 
[echoproperties] antProperty=false 

và khi lệnh là mvn -PcustomProfile compile sau đó đầu ra là:

main: 
[echo] Custom Ant Property is: true 
[echoproperties] #Ant properties 
[echoproperties] #Thu Aug 08 17:18:30 CEST 2013 
[echoproperties] ant.project.name=maven-antrun- 
[echoproperties] ant.version=Apache Ant(TM) version 1.8.2 compiled on December 20 2010 
[echoproperties] antProperty=true 

này hoạt động sử dụng maven 3.0.5.

+0

@RiccardoCossu Tôi đã tạo một dự án thử nghiệm chỉ chứa một pom (Tôi đã chỉnh sửa câu trả lời của mình để bao gồm pom hoàn chỉnh). Như được mô tả trong câu trả lời khi tôi thực thi maven, thuộc tính được thay thế chính xác trong cuộc gọi antrun. Bạn đang sử dụng phiên bản nào của maven? – DB5

+0

Tôi đã thử dự án thử nghiệm của bạn và nó hoạt động; Tôi vẫn phải hiểu tại sao nó không hoạt động trong trường hợp cụ thể của tôi ... :-(Tôi đang sử dụng 3.0.4 –

+2

nhận được nó! Tôi rơi vào một vòng lặp của sự nhầm lẫn giữa kiến ​​và maven ... Tôi đã tuyên bố tài sản cách "kiến" trong phần maven (với thẻ có tên là thuộc tính, thay vì tên tùy chỉnh) –

3

Trên các phiên bản mới hơn của maven bạn chỉ có thể sử dụng:

<taskdef resource="net/sf/antcontrib/antcontrib.properties" 
        classpathref="maven.plugin.classpath" /> 

dụ:

<build> 
....  
     <plugins> 
     .... 
    <plugin> 
     <artifactId>maven-antrun-plugin</artifactId> 
     <executions> 
      <execution> 
      <phase>install</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
      <tasks> 
       <taskdef resource="net/sf/antcontrib/antcontrib.properties" 
             classpathref="maven.plugin.classpath" /> 

       <echo message="Project name from Maven: ${project.name}" /> 

      </tasks> 
     </configuration> 
     </execution> 
     </executions> 
    </plugin> 
     .... 
     </plugins> 
     .... 
    </build> 

<dependencies> 
<dependency> 
    <groupId>ant-contrib</groupId> 
    <artifactId>ant-contrib</artifactId> 
    <version>1.0b3</version> 
    <exclusions> 
     <exclusion> 
      <groupId>ant</groupId> 
      <artifactId>ant</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 
<dependency> 
    <groupId>ant</groupId> 
    <artifactId>ant-nodeps</artifactId> 
    <version>1.6.5</version> 
</dependency> 
</dependencies> 
3

Hầu hết các thuộc tính sẽ được tự động chuyển tới những kiến, tại ít nhất là nếu bạn đang chạy một tập lệnh kiến. Một số thuộc tính được đổi tên. Tôi đề nghị chạy "mvn -X" và plugin antrun in danh sách tất cả các ánh xạ biến thành kiến ​​(những thứ như basedir trở thành project.basedir, v.v.)

Các vấn đề liên quan