Run a Ant task in Maven only if a property is set

半腔热情 提交于 2019-12-03 14:10:43

There is an if task in Ant-contrib that you could use:

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

You don't need the <else>, this was just for demo purpose.

In case you don't like IF syntax in Ant-contrib you can use 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>
Andrei Neshcheret

With maven-antrun-plugin:1.8 You can specify attributes in the <target/> configuration to execute or not Ant tasks depending some conditions as described in 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>

As you requested, but using <target/> instead of deprecated <tasks/>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!