19

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)

9 Answers 9

13

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)

2
  • 1
    As of today it still isn't updated and version is a SNAPSHOT of an alpha 1.
    – bric3
    Jan 1, 2012 at 14:24
  • This is okay, but you can't do a mvn clean install I don't think.
    – Ring
    Feb 8, 2013 at 1:16
11

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.

1
  • You're right about the maven-ant-task.jar file placement. You only need those two lines if you have it inside you project and not in your ant's lib folder. That's how I once solved a problem , stackoverflow.com/a/32950670/1793718
    – Lucky
    Jan 19, 2016 at 8:57
10

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.

1
  • After some hard won configuration, I got this running like a charm. Thanks a bunch. Apr 2, 2009 at 18:59
5

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

1
  • 5
    +1 - it's 2012 and the maven-ant-tasks are the best way I've found t o use dependency management via ant. I tried in earnest to use ivy a few years ago, but eventually gave up on it and just went with maven-ant-tasks for ant-only projects, and maven when possible. Ivy is (was) just a re-implementation of the basic maven idea with no additional benefit, except a different name (which was enough of a selling point for many). Today, though, more projects simply (yes, simply) use maven and only resort to ant for corner cases (imo). If the code will be (re-)used, mavenize the poroject asap.
    – michael
    Jul 31, 2012 at 14:26
4

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.

1
  • 1
    The arguments against Ivy are bogus. If you really want to use Maven then sure don't use Ivy. Saying that Maven is better than Ivy because Ivy isn't like Maven is interesting. I'm sure nobody buys into that. Nov 14, 2011 at 16:20
2

It is very simple to run Maven goal from Ant

<target name="buildProject" description="Builds the individual project">
    <exec dir="${source.dir}\${projectName}" executable="cmd">
        <arg value="/c"/>
        <arg value="${env.MAVEN_HOME}\bin\mvn.bat"/>
        <arg line="clean install" />
    </exec>
</target>

Using this you are allow to run any kind of Maven goal from Ant...

Enjoy....

2
  • It's not cross-platform and Maven Ant tasks are better for this purpose.
    – gouessej
    Nov 22, 2014 at 12:48
  • @dhawal I tried this but I get [exec] The system cannot find the path specified. [exec] Result: 1
    – pankaj
    Jul 5, 2016 at 8:50
2

In my case i just want an ejb jar to be at the repository so i could use it on another project with maven as dependency so:

<target name="runMaven" depends="deploy" description="LLama al maven.">
    <exec executable="cmd">
        <arg value="/c"/>
        <arg value="mvn.bat install:install-file -DgroupId=com.advance.fisa.prototipo.camel -DartifactId=batch-process -Dversion=1.0 -Dpackaging=jar  -Dfile=${jarDirectory}\batch-process.jar"/>
    </exec>
</target>
1

Download Maven Ant Tasks then use this:

<target name="getDependencies">
        <path id="maven-ant-tasks.classpath" path="${basedir}${file.separator}maven${file.separator}lib${file.separator}maven-ant-tasks.jar" />
        <typedef resource="org/apache/maven/artifact/ant/antlib.xml" uri="antlib:org.apache.maven.artifact.ant" classpathref="maven-ant-tasks.classpath" />

        <artifact:dependencies filesetId="dependency.fileset" type="jar">
            <pom file="pom.xml" />
        </artifact:dependencies>

        <!--TODO take care of existing duplicates in the case of changed/upgraded dependencies-->
        <copy todir="lib">
            <fileset refid="dependency.fileset" />
            <mapper type="flatten" from="${dependency.versions}" />
        </copy>
    </target>
0

I am working on the same problem right now. I installed all necessary libs in my local Maven repo and from there I put it into our Company Maven Repo. It is not working quite right yet. Some of the tests fail that work nicely in my Maven test run, but since the outcome of the test is not important for the coverage data, I am quite satisfied.

Here is my Maven snippet. I hope that helps.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.1</version>
    <inherited>false</inherited>
    <executions>
        <execution>
            <phase>site</phase>
            <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="CRAP4J_HOME" value="${user.home}/Projects/crap4j"/>
                    <taskdef name="crap4j" classname="org.crap4j.anttask.Crap4jAntTask">
                        <classpath>
                            <fileset dir="${CRAP4J_HOME}/lib">
                                <include name="**/*.jar"/>
                            </fileset>
                        </classpath>
                    </taskdef>
                    <crap4j projectdir="${project.basedir}/alm-jar-server"
                            outputDir="${project.basedir}/crap4jReports"
                            dontTest="false"
                            debug="true">
                        <classes>
                            <pathElement location="${project.basedir}/target/classes"/>
                        </classes>
                        <srces>
                            <pathElement location="${project.basedir}/src/main/java"/>
                        </srces>
                        <testClasses>
                            <pathElement location="${project.basedir}/target/test-classes"/>
                        </testClasses>
                        <libClasspath>
                            <fileset dir="${user.home}/.m2/repository">
                                <include name="**/*.jar"/>
                            </fileset>
                        </libClasspath>
                    </crap4j>


                   </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>org.crap4j</groupId>
            <artifactId>crap4j</artifactId>
            <version>1.1.6</version>
        </dependency>
        <dependency>
            <groupId>org.kohsuke</groupId>
            <artifactId>args4j</artifactId>
            <version>2.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.agitar</groupId>
            <artifactId>asmlib</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.0</version>
        </dependency>
        <dependency>
            <groupId>com.agitar</groupId>
            <artifactId>coverage</artifactId>
            <version>1.0</version>
        </dependency>
    </dependencies>
</plugin>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.