Play with Maven from Ant

Maven 2.0.4 is out today, but what I’m blogging about is not particuraly a feature of today’s version.

Wants to do dirty things in your clean and rigorous maven2 build ;) ?
You can access most of Maven2 magic within ant with Maven Tasks for Ant.
Sounds to me something like a Ivy killer…

For example you can read a pom.xml file whithin ant and access all of its values with ant scripting variables :

<artifact:pom file="${pom.location}" id="pom"/>
<echo>Finding dependencies of ${pom.artifactId} version ${pom.version}</echo>

Better, you can read the dependencies and benefits from the maven dependencies mechanism (including the dreaded transitive dependencies), the following snipets downloads and gives you an Ant FileSet of the maven dependencies. (note if you’re not using maven2 pom.xml to describe your project, you can specify dependency without it)

<artifact:remoteRepository id="remote.repository" url="${remote.repository}" />
<artifact:pom file="${pom.location}" id="pom"/>
<artifact:dependencies filesetId="dependency.fileset" useScope="runtime">
<pom refid="pom"/>
<remoteRepository refid="remote.repository" />
</artifact:dependencies>

Here’s a simple example which download a set of dependencies and unjar them in a specific directory.

<project name="playWithMavenFromAnt" default="unjar" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  <property name="pom.location"      value="pom.xml" />
  <property name="dependency.scope"  value="runtime" />
  <property name="remote.repository" value="http://myPrivateRepo/repository/" />
  <property name="output.directory"  value="tmp"/>
  <target name="init">
    <artifact:remoteRepository id="remote.repository" url="${remote.repository}" />
    <artifact:pom file="${pom.location}" id="pom"/>
    <echo>Finding dependencies of ${pom.artifactId} version ${pom.version}</echo>
    <artifact:dependencies filesetId="dependency.fileset" useScope="dependency.scope">
      <pom refid="pom"/>
      <remoteRepository refid="remote.repository" />
    </artifact:dependencies>
  </target>
  <target name="copy-dependencies" depends="init">
    <mkdir dir="${output.directory}" />
    <copy todir="${output.directory}" verbose="true">
      <fileset refid="dependency.fileset" />
      <mapper type="flatten" />
    </copy>
   </target>
   <target name="unjar" depends="copy-dependencies">
     <mkdir dir="${output.directory}/lib"/>
     <unjar dest="${output.directory}/lib">
     <fileset refid="dependency.fileset"/>
   </unjar>
</target>
<target name="clean">
   <delete dir="${output.directory}"/>
</target>
</project>

Note: The above script is just an example to demonstrate the use of maven from within ant, you can do exactly the same things with a simple mvn assembly:unpack with Maven only.

update : There is an evil bug which prevents using the above with snapshots which have not been build locally, anihilating any continuous integration based build… Until this is solved, it’s difficult to use the above in a real project. MNG-1408

Links :

2 Comments

  1. Peter Thomas:

    You may be interested in this alternative approach to use Maven from Ant I blogged about here: ptrthomas.wordpress.com/2…

  2. Maven-Ant hybrid « Simbiosis:

    [...] the dependencies and put them in the lib directory for Ant to use. Using some pointers I got from here, I produced this ant file, invoked with ant -f ant-deps.xml.This will use your pom.xml to do the [...]

Leave a comment