Allain Lalonde
Allain Lalonde

Reputation: 93348

Using Maven from 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)

Upvotes: 19

Views: 37534

Answers (9)

Randyaa
Randyaa

Reputation: 2945

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>

Upvotes: 1

cabaji99
cabaji99

Reputation: 1445

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>

Upvotes: 2

Johannes
Johannes

Reputation: 138

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>

Upvotes: 0

Dhaval Patel
Dhaval Patel

Reputation: 648

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

Upvotes: 2

Peter Thomas
Peter Thomas

Reputation: 58058

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.

Upvotes: 4

whaley
whaley

Reputation: 16265

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

Upvotes: 5

Dominic Mitchell
Dominic Mitchell

Reputation: 12299

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.

Upvotes: 11

Tim O&#39;Brien
Tim O&#39;Brien

Reputation: 9851

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)

Upvotes: 13

Vladimir
Vladimir

Reputation: 6871

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.

Upvotes: 10

Related Questions