Using Maven from Ant

36,463

Solution 1

If you think that Maven is overkill in your project, you could/should try Apache Ivy: It's a very powerful dependency management library similar to the Maven one.

If you're hosting a project on the web, take also a look at Ivy Roundup, it's a repository of Ivy definitions for various libraries.

Solution 2

There is a new set of Ant tasks that use Mercury. Mercury is the refactored code that will be the basis of way that Maven 3 interacts with Maven (and OSGi) repositories that is being implemented by Oleg Gusakov. Mercury is well tested, and you can start using it in Ant projects today. Take a look at some of the How-to documents that Oleg has written:

http://people.apache.org/~ogusakov/sites/mercury-ant/mercury-ant-tasks/howto.html

Here's a simple example of using Mercury in an Ant build.xml file. The following build file creates a classpath that depends on verion 3.0 of the asm artifact:

<javac srcdir="src/main/java"
       destdir="target/classes">
  <classpath> 
    <deps>
      <dependency name="asm:asm:3.0"/>
    </deps>
  </classpath>
</javac>

There are a lot of advanced features such as support for verifying PGP signatures or MD5 digests. You can also start to define different repositories that Mercury depends on. This XML allows you to define a reference to a repository such as Nexus in addition to using a local directory as a repository:

<repo id="myCentral" 
 url="http://localhost:8081/nexus/contengs/groups/public"/>
<repository dir="/my/local/repo"/>

<javac srcdir="src/main/java"
       destdir="target/classes">
  <classpath> 
    <deps>
      <dependency name="asm:asm:3.0"/>
    </deps>
  </classpath>
</javac>

If you need to reference a repository that requires authentication Mercury has support for storing a username and password:

<repo id="myCentral" 
 url="http://localhost:8081/nexus/contengs/groups/public">
  <auth name="foo" pass="bar"/>
</repo>

<javac srcdir="src/main/java"
       destdir="target/classes">
  <classpath> 
    <deps>
      <dependency name="asm:asm:3.0"/>
    </deps>
  </classpath>
</javac>

Most compelling is the ability to publish an artifact to a repository from an Ant build file. If you work in an organization of any scale, you'll want to start thinking about deploying artifacts to a repository manager like Nexus. With Mercury, you can start deploying artifacts to a repository manager without having to adopt Maven. Here's a build file that defines an authenticated repository and writes an artifact:

<repo id="myCentral" 
 url="http://localhost:8081/nexus/contengs/groups/public">
  <auth name="foo" pass="bar"/>
</repo>

<write repoid="myCentral"
       name="t:t:1.0"
       file="${basedir}/target/t.jar"/>

Mercury is ready to use, and you can expect a lot of developments from Oleg going forward. If you want to start using it, the best place to look is at Oleg's How-to Page. (Note: This information will soon be integrated into the Definitive Guide)

Solution 3

Whilst the mercury tasks work, I haven't used them. I have had good success with their predecessors, the maven-ant-tasks. They're fairly simple to get going, if you already have a POM handy.

<project name="blah" xmlns:artifact="antlib:org.apache.maven.artifact.ant">
  <!-- If you drop the maven-ant-tasks in ~/.ant/lib, you don't need these two bits. -->
  <taskdef uri="antlib:org.apache.maven.artifact.ant"
           resource="org/apache/maven/artifact/ant/antlib.xml"
           classpathref="ant.classpath" />
  <path id="ant.classpath">
    <fileset dir="${ant.tasks.dir}">
      <include name="*.jar" />
    </fileset>
  </path>
  <target name="resolve" description="--> retrieve dependencies with maven">
      <!-- Resolve dependencies -->
      <artifact:dependencies filesetId="dependency.fileset">
          <pom file="pom.xml" />
      </artifact:dependencies>
      <!-- Copy all dependencies to the correct location. -->
      <copy todir="${web.dir}/WEB-INF/lib">
          <fileset refid="dependency.fileset" />
          <!-- This mapper strips off all leading directory information -->
          <mapper type="flatten" />
      </copy>
  </target>
</project>

I like to keep my ant task jars inside the project, so I've added the taskdef and path. But if you want to put maven-ant-tasks-2.0.9.jar in ~/.ant/lib, then you don't need to declare this stuff. I think.

Solution 4

Just use the Maven Ant Tasks. They can be downloaded at the normal maven download page.

Solution 5

Refer this: Why you should use the Maven Ant Tasks instead of Maven or Ivy

I wouldn't really recommend Ivy for reasons given in the link above.

Share:
36,463
Allain Lalonde
Author by

Allain Lalonde

I'm a Software Developer working on Alternative User Interfaces for business projects. I love working on things that make you "Why?".

Updated on July 09, 2022

Comments

  • Allain Lalonde
    Allain Lalonde almost 2 years

    Are there ant plugins that wrap maven so that I can make use of its dependency management features to download jars for me and place them in my ant build's lib folder?

    My specific problem is that I'm using the Crap4j plugin for Hudson, but it doesn't, as of yet, support Maven. Since it's a small project, maven is overkill, but I don't want to go without mvn dependency:copy-dependcies if I don't have to.

    Any suggestions? (other than suck it up)