Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

My pom.xml is running an Ant task to deploy a file using FTP. However, this deployment must be only done if the -Dftp=true argument is given in the Maven command (i.e. mvn clean install -Dftp=true). Thus, I wrote the following code:

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

Using this pom.xml, the Ant task is not executed if I do not define the -Dftp property in my Maven command. However, if I give any kind of value for this property, for example -Dftp=false, the Ant task is run, which is not correct.

How configure the AntRun task to be run only if a given property has a given value?

ps: I know I can use a profile that is only active when ftp is equal to true. This solution works, but for some reason, I want to have my Antrun plugin build block.

<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)
like image 659
Romain Linsolas Avatar asked Feb 24 '10 07:02

Romain Linsolas


People also ask

How do I run Ant tasks in Maven?

Run. The maven-antrun-plugin has only one goal, run . This allows Maven to run Ant tasks. To do so, there must be an existing project and maven-antrun-plugin must have its <target> tag configured (although it would still execute without the <target> tag, it would not do anything).

Can you use Ant and Maven together?

Although most of the time we'll only use one of these, there are cases when using the two together makes sense. A common use case is when working on a legacy project that uses Ant, and we want to introduce Maven gradually while still keeping some existing Ant tasks in place.

What is Maven Ant task?

The Maven Ant Tasks allow several of Maven's artifact handling features to be used from within an Ant build. These include: Dependency management - including transitive dependencies, scope recognition and SNAPSHOT handling. Artifact deployment - deployment to a Maven repository (file integrated, other with extensions)

What is Maven-Antrun-plugin used for?

This plugin provides the ability to run Ant tasks from within Maven. You can even embed your Ant scripts in the POM! It is not the intention of this plugin to provide a means of polluting the POM, so it's encouraged to move all your Ant tasks to a build.


2 Answers

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.

like image 110
Pascal Thivent Avatar answered Oct 04 '22 23:10

Pascal Thivent


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

like image 23
Andrei Neshcheret Avatar answered Oct 04 '22 23:10

Andrei Neshcheret