2010-02-24 17 views
6

My pom.xml đang chạy tác vụ Ant để triển khai tệp bằng FTP. Tuy nhiên, việc triển khai này chỉ được thực hiện nếu đối số -Dftp=true được đưa ra trong lệnh Maven (ví dụ: mvn clean install -Dftp=true). Do đó, tôi đã viết đoạn mã sau:Chỉ chạy tác vụ Ant trong Maven nếu thuộc tính được đặt

<build> 
    <plugins> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-antrun-plugin</artifactId> 
      <version>1.2</version> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>run</goal> 
        </goals> 
        <configuration> 
         <tasks if="ftp"> 
          <echo message="Deploying files through FTP..."/> 
          ... 
         </tasks> 
        </configuration> 
       </execution> 
      </executions> 

Sử dụng pom.xml này, nhiệm vụ Ant không được thực hiện nếu tôi không xác định -Dftp tài sản trong lệnh Maven tôi. Tuy nhiên, nếu tôi cung cấp bất kỳ loại giá trị nào cho thuộc tính này, ví dụ: -Dftp=false, tác vụ Ant sẽ chạy, điều đó không chính xác.

Cách định cấu hình tác vụ AntRun sẽ chỉ chạy nếu thuộc tính đã cho có giá trị đã cho?

ps: Tôi biết tôi có thể sử dụng profile chỉ hoạt động khi ftp bằng true. Giải pháp này hoạt động, nhưng vì lý do nào đó, tôi muốn có plugin Antrun của tôi build.

<profiles> 
    <profile> 
     <id>deployment</id> 
     <activation> 
      <activeByDefault>false</activeByDefault> 
      <property> 
       <name>ftp</name> 
       <value>true</value> 
      </property> 
     </activation> 
     <build> 
      <plugins> 
       <plugin> 
        ... (define the Ant task here) 

Trả lời

9

Có một nhiệm vụ if trong Ant-contrib mà bạn có thể sử dụng:

<plugin> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.3</version> 
    <executions> 
     <execution> 
     <id>ftp</id> 
     <phase>package</phase> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     <configuration> 
      <tasks> 
      <taskdef resource="net/sf/antcontrib/antcontrib.properties" 
       classpathref="maven.plugin.classpath" /> 
      <if> 
       <equals arg1="${ftp}" arg2="true" /> 
       <then> 
       <echo message="The value of property ftp is true" /> 
       </then> 
       <else> 
       <echo message="The value of property ftp is not true" /> 
       </else> 
      </if> 
      </tasks> 
     </configuration> 
     </execution> 
    </executions> 
    <dependencies> 
     <dependency> 
     <groupId>ant-contrib</groupId> 
     <artifactId>ant-contrib</artifactId> 
     <version>20020829</version> 
     </dependency> 
    </dependencies> 
    </plugin> 

Bạn không cần <else>, đây là chỉ cho mục đích demo.

+0

Cú pháp khủng khiếp (và tiết) cho tác vụ 'if' này là gì. Nhưng nó hoạt động, điều đó quan trọng hơn; o) Cảm ơn! – romaintaz

2

Trong trường hợp bạn không thích cú pháp IF trong Ant-contrib, bạn có thể sử dụng antelopetask.

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.7</version> 
    <inherited>false</inherited> 
    <configuration> 
     <target> 
      <taskdef name="if" classname="ise.antelope.tasks.IfTask"/> 

      <if name="maven.ant.target"> 
       <ant target="${maven.ant.target}"/> 
       <else> 
        <fail message="Please specify a target to execute in 'maven.ant.target' property" /> 
       </else> 
      </if> 
     </target> 
    </configuration> 
    <dependencies> 
     <!-- http://antelope.tigris.org/nonav/docs/manual/bk03.html --> 
     <dependency> 
      <groupId>org.tigris.antelope</groupId> 
      <artifactId>antelopetasks</artifactId> 
      <version>3.2.10</version> 
     </dependency> 
    </dependencies> 
</plugin> 
1

Với maven-antrun-plugin: 1.8 Bạn có thể chỉ định các thuộc tính trong < mục tiêu/> cấu hình để thực hiện hay không nhiệm vụ Ant tùy thuộc một số điều kiện như mô tả trong Maven antrun plugin documentation

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.8</version> 
    <executions> 
     <execution> 
     <phase>package</phase> 
     <goals> 
      <goal>run</goal> 
     </goals> 
     <configuration> 
      <target if="ftp"> 
      <echo message="To run, just call mvn package -Dftp=true"/> 
      </target> 
     </configuration> 
     </execution> 
    </executions> 
    </plugin> 

Như bạn yêu cầu, nhưng sử dụng mục tiêu </> thay vì không dùng nữa < nhiệm vụ/>

+0

Cảm ơn! Làm việc cho tôi (với Maven 3.0.5 và 3.3.9), đó là cách để đi vào năm 2017 :) – xav