Maven – Add ant-contrib Ant Tasks in maven-antrun-plugin

Ant Tasks, such as <if>, belongs to ant-contrib.jar. So in order to use them in maven-antrun-plugin, you have to add this dependency explicitly in the pom.xml.

<!-- Properties -->
<properties>
  <greeting>true</greeting>
</properties>

<!-- Build Configuration -->
<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-antrun-plugin</artifactId>
      <version>1.7</version>
      <dependencies>
        <dependency>
          <groupId>ant-contrib</groupId>
          <artifactId>ant-contrib</artifactId>
          <version>20020829</version>
        </dependency>
      </dependencies>
      <executions>
        <execution>
          <id>initialize</id>
          <phase>initialize</phase>
          <configuration>
            <tasks>
              <!-- add the ant tasks from ant-contrib -->
              <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
              
              <!-- show greeting if ${greeting} is set to true-->
              <if>
                <equals arg1="${greeting}" arg2="true" />
                <then>
                  <echo>Hello World</echo>
                </then>
              </if>
            </tasks>
          </configuration>
          <goals>
            <goal>run</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>

Please remember to add the tasks definition of ant-contrib.jar using the <taskdef> inside the <tasks> node.

Run mvn initialize, the console will echo the greeting message only if the ${greeting} property is set to true.

Done =)

Advertisements

4 thoughts on “Maven – Add ant-contrib Ant Tasks in maven-antrun-plugin”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s