仅在设置了属性时才在Maven中运行Ant任务

Rom*_*las 6 maven-2 maven-antrun-plugin

pom.xml正在运行Ant任务来使用FTP部署文件.但是,只有-Dftp=true在Maven命令(即mvn clean install -Dftp=true)中给出参数时,才必须执行此部署.因此,我写了以下代码:

<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>
Run Code Online (Sandbox Code Playgroud)

使用它pom.xml,如果我没有-Dftp在Maven命令中定义属性,则不会执行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)
Run Code Online (Sandbox Code Playgroud)

Pas*_*ent 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>
Run Code Online (Sandbox Code Playgroud)

你不需要<else>,这只是为了演示目的.


小智 5

使用 maven-antrun-plugin:1.8 您可以在 <target/> 配置中指定属性来执行或不执行 Ant 任务,具体取决于Maven antrun 插件文档中所述的某些条件

<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>
Run Code Online (Sandbox Code Playgroud)

按照您的要求,但使用 <target/> 而不是已弃用的 <tasks/>