Running specified ant tasks within Maven2 outside the lifecycle

10Mar08

I figured out a best practice to call a specific ant task within maven2 without integrating it into the lifecycle.

This might be necessary if you want to access maven2 classpath variables or properties specified in pom.xml for your ant task. The task will run outside the lifecycle which means that it only runs by explicit invocation. This makes sense for operations like database setup or something similar.

The approach is using maven2 profiles.

Specify a profile in pom.xml:

<profile>
 <id>ant-target</id>
 <build>
  <defaultGoal>antrun:run</defaultGoal>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
      
    <configuration>
     <tasks>
      <property name="compile_classpath" refid="maven.compile.classpath"/>
      <property name="runtime_classpath" refid="maven.runtime.classpath"/>
      <property name="test_classpath" refid="maven.test.classpath"/>
      <property name="plugin_classpath" refid="maven.plugin.classpath"/>
      
     <property name="db.username" value="${db.username}"/>
      
   <ant antfile="${basedir}/ant/database-setup.ant.xml" inheritRefs="true" inheritAll="true">
       <target name="${target}" />
      </ant>
     </tasks>
    </configuration>
    
    <goals>
     <goal>run</goal>
    </goals>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>ant-contrib</groupId>
    <artifactId>ant-contrib</artifactId>
    <version>1.0b2</version>
  </dependency>
 </dependencies>
</profile>

You can call the target like that:

mvn -P ant-target -D target=my-ant-target

where -P gives the name of the profile, in my example it is “ant-target”. -D specifies the target variable, in this example maven will try to execute an ant target called “my-ant-target” specified in /ant/database-setup.ant.xml.

Dependencies required by the ant task can be specified in the dependency section. This is really cool because they are only available in context of the ant task.

Notify that sometimes inheritAll=”true” will not work properly (maybe a bug), so you have to specify properties which should be available in your ant task manually (like “db.username” in this example).



14 Responses to “Running specified ant tasks within Maven2 outside the lifecycle”

  1. Thanks for writing this up. I have been screwing around with this for 6 hours trying to get it working all kinds of other different ways. Nice work!

  2. 2 Bhasker Reddy

    Thanks a bunch. Actually we were in need of something similar, this would be very useful.

  3. 3 GHiguera

    Aha, now I know TJ’s secret mojo

  4. 4 Alexwebmaster

    Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru

  5. 5 varun

    Excellent work!!.. nice interpretation of profiles.. i really rubs lots of my grey cells to get it done..
    Thnaks

  6. Sry for commenting OT … what WP template are you using? It’s looking awesome.

  7. 7 ranga

    thanks for sharing very useful post

  8. Hello mates, nice post and nice arguments commented at this place, I am really enjoying by these.

  9. I know this site offers quality depending posts
    and extra stuff, is there any other site which provides these stuff in quality?

  10. Hey that’s wierd — just on the day I added Palm Leaf to my blogroll! Click https://twitter.com/moooker1


  1. 1 WP8 Access Build Settings programmatically - Zielonka Gyan
  2. 2 Maven error with multiple executions in maven-deploy-plugin | Questions
  3. 3 java - Maven errore con più esecuzioni in maven-distribuzione-plugin
  4. 4 Dedicated Ny Game Server

Leave a comment