2010-02-24 86 views
6

我的pom.xml正在運行Ant任務以使用FTP部署文件。但是,只有在Maven命令(即mvn clean install -Dftp=true)中給出-Dftp=true參數時才能執行此部署。因此,我寫了下面的代碼:只有在設置屬性時纔在Maven中運行Ant任務

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

使用該pom.xml,如果我不定義在我的Maven命令-Dftp財產不執行Ant任務。但是,如果我爲此屬性提供任何類型的值,例如-Dftp=false,則Ant任務將運行,這是不正確的。

如何配置AntRun任務僅在給定屬性具有給定值時才運行?

ps:我知道我可以使用profile,只有當ftp等於true時纔有效。此解決方案有效,但由於某種原因,我想要我的Antrun插件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) 

回答

9

有一個在Ant-contrib一個if的任務,你可以使用:

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

你不需要<else>,這只是爲了演示的目的。

+0

這個'if'任務的可怕(和冗長)語法是什麼。但它做的工作,這是更重要的; o)謝謝! – romaintaz 2010-02-24 09:04:33

2

如果您不喜歡Ant-contrib中的IF語法,則可以使用antelopetasks。

<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

與Maven-antrun-插件:1.8作爲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> 

根據你的要求描述你可以指定<目標/ >配置屬性執行與否取決於一些條件Ant任務,但使用<目標/ >而不是棄用<任務/ >

+0

謝謝!適用於我(使用Maven 3.0.5和3.3.9),這是2017年的方式:) – xav 2017-11-03 12:01:29