Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Maven from Ant

Tags:

maven-2

ant

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)

like image 242
Allain Lalonde Avatar asked Mar 31 '09 18:03

Allain Lalonde


People also ask

Can we use Maven and Ant together?

You can use the maven-antrun-plugin to invoke the ant build. Then use the build-helper-maven-plugin to attach the jar produced by ant to the project. The attached artifact will be installed/deployed alongside the pom. If you specify your project with packaging pom , Maven will not conflict with the ant build.

Is Maven based on Ant?

Apache Ant is the predecessor of Apache Maven. First released in 2000, Ant was developed as a replacement for a build tool Make, which was used widely in software development in the past.


2 Answers

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)

like image 185
Tim O'Brien Avatar answered Sep 22 '22 08:09

Tim O'Brien


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.

like image 20
Dominic Mitchell Avatar answered Sep 21 '22 08:09

Dominic Mitchell