Running specified ant tasks within Maven2 outside the lifecycle


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:

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

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


12 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

  5. 5 varun

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

  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

  1. 1 WP8 Access Build Settings programmatically - Zielonka Gyan
  2. 2 Maven error with multiple executions in maven-deploy-plugin | Questions

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: