Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Passing your Maven classpath to Ant:

In your pom.xml, configure something like this:

Code Block
   <build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <id>build-config-files</id>
            <phase>compile</phase>
            <goals>
              <goal>run</goal>
            </goals>
            <configuration>
              <tasks>
                <property name="runtime-classpath" refid="maven.runtime.classpath"/>
                <ant target="build" inheritRefs="true"/>
              </tasks>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

In your build.xml, have something like:

Code Block
     <path id="project.class.path">
        <path path="${runtime-classpath}"/>
    </path>

The path that maven is using should now be assigned to "runtime-classpath".

This is very useful for calling a custom ant task, when the definition is in your maven classpath. 

Using ant-contrib tasks:

To use ant-contrib tasks with the maven-antrun-plugin you'll need to do a couple of things:

1. Add a taskdef.

Code Block
<taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

2. Add some dependencies for the plugin:

Code Block
<dependencies>
  <dependency>
    <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>1.0b3</version>
    <exclusions>
      <exclusion>
        <groupId>ant</groupId>
        <artifactId>ant</artifactId>
      </exclusion>
    </exclusions>
  </dependency>
  <dependency>
    <groupId>ant</groupId>
    <artifactId>ant-nodeps</artifactId>
    <version>1.6.5</version>
  </dependency>
</dependencies>

Notice that the transcient dependency on ant:ant is execluded for ant-contrib. This is due to ant-contrib having a dependency on ant-1.5. I was getting the following error before replacing ant with ant-nodeps-1.6.5:

Code Block
Error configuring: org.apache.maven.plugins:maven-antrun-plugin. Reason: java.lang.NoSuchMethodError: org.apache.tools.ant.util.FileUtils.close(Ljava/io/InputStream;)

Here's full example of using the antrun plugin to move a war file to a jboss server deploy directory once it is built. You can also define the unpacked property (ie. mvn install -Dunpacked) to have the app moved exploded (or unpacked).

Code Block
<plugin>
  <artifactId>maven-antrun-plugin</artifactId>
  <executions>
    <execution>
      <phase>install</phase>
      <configuration>
        <tasks>
          <taskdef resource="net/sf/antcontrib/antcontrib.properties" classpathref="maven.plugin.classpath"/>

          <delete file="${my.jboss.server}/deploy/${build.finalName}.war"/>
          <delete dir="${my.jboss.server}/deploy/${build.finalName}.war"/>

          <if>
            <isset property="unpacked"/>
            <then>
              <mkdir dir="${my.jboss.server}/deploy/${build.finalName}.war"/>
              <unzip src="${project.build.directory}/${build.finalName}.war"
                     dest="${wsm.jboss.server}/deploy/${build.finalName}.war"/>
            </then>
            <else>
              <copy todir="${my.jboss.server}/deploy">
                <fileset dir="${project.build.directory}" includes="${build.finalName}.war"/>
              </copy>
            </else>
          </if>
       </tasks>

      </configuration>
      <goals>
        <goal>run</goal>
      </goals>
     </execution>
   </executions>
   <dependencies>
   <dependency>
     <groupId>ant-contrib</groupId>
     <artifactId>ant-contrib</artifactId>
     <version>1.0b3</version>
     <exclusions>
       <exclusion>
         <groupId>ant</groupId>
         <artifactId>ant</artifactId>
       </exclusion>
     </exclusions>
   </dependency>
   <dependency>
     <groupId>ant</groupId>
     <artifactId>ant-nodeps</artifactId>
     <version>1.6.5</version>
   </dependency>
  </dependencies>
</plugin>

Maven plugin classpath bug.

Using the Maven Antrun Plugin in multiple projects in the Reactor

In Maven 2.x, there is a significant bug that can lead to very perplexing failures with the antrun plugin. There are two conditions that you have to meet to get into this problem.

1) You have to use the plugin in two projects that are loaded into the reactor via <modules/> .
2) You have to require additional plugin dependencies on the plugin.

In Maven 2.x, the dependencies of the first project into the reactor win, and all the others are entirely ignored.

Concretely, if your first project uses the plugin with no special dependencies, and your second one tries to use ant-trax, the second antrun execution will fail, and complain that it can't find the classes in ant-trax.

The only solution to this is to ensure that, in all of the places where you use the plugin, you list the same dependencies.

This problem has been fixed in Maven 3.