For my last couple of Java projects, I decided to use Maven. It was a long due  experiment. I’m not gonna talk about my experience with it, maybe in a future post, suffice to say that I’m quite satisfied. The first project went to production a couple of weeks ago, and all went fine, except for one thing. Reading the specs of the project, I overlooked the tiny little print that stated: “The final product must be built with Ant“.
Cr@p!
, was the first thing I thought. Obviously, the client refused the proposal to install Maven. As a programmer, I usually try to solve my problem the lazy way. I really don’t want to commit all the project dependencies to the Subversion repository. That sort of freaks me out. Here enters the Ant mvn task. For the sake of simplicity, the mvn task can be seen as, more or less, a wrapper that allows to invoke a Maven build from ant. I felt I was on the right track, but I thought I had one more problem to solve: how to install Maven on any possible build machine ? Luckily, the answer lied in the documentation.

<target name="build">
    <artifact:mvn mavenVersion="3.0.3" pom="./pom.xml">
        <arg value="package"/>
    </artifact:mvn>
</target>

This snippet shows how to invoke a Maven goal, specifying the pom location and the Maven version we would like to use. The first time you run the build, the mvn task will take care of downloading mvn with all the necessary dependencies. That was easy.
There is one last thing to take care of though, dependencies. My project depends on a few libraries, and in particular on the JMS jar, which, due to license restrictions, cannot be distributed through a Maven repository. So I have to install it in my local repo, and the following snippet shows how:

<artifact:pom id="jms-dep" file="deps/jms-1.1.pom"/>
<artifact:install file="deps/jms-1.1.jar">
    <pom refid="jms-dep"/>
</artifact:install>

This snippet should go in a target executed before the build one. So, let’s have a look at the complete build.xml:

<project name="gestoreAlert" default="build" basedir="." xmlns:artifact="antlib:org.apache.maven.artifact.ant">
    <description>
        Invoke mvn package goal.
    </description>

    <path id="maven-ant-tasks.classpath" path="maven-ant-tasks-2.1.3.jar" />
    <typedef resource="org/apache/maven/artifact/ant/antlib.xml"
        uri="antlib:org.apache.maven.artifact.ant"
        classpathref="maven-ant-tasks.classpath" />

   <artifact:localRepository id="local.repository" path="~/.m2/repository" layout="default" />

   <target name="deps">
       <artifact:pom id="jms-dep" file="deps/jms-1.1.pom"/>
       <artifact:install file="deps/jms-1.1.jar">
           <pom refid="jms-dep"/>
       </artifact:install>

       <artifact:pom id="jboss-javaee-dep" file="deps/jboss-javaee-5.1.0.GA.pom"/>
       <artifact:install file="deps/jboss-javaee-5.1.0.GA.jar">
          <pom refid="jboss-javaee-dep"/>
       </artifact:install>

       <artifact:pom id="kryo-dep" file="deps/kryo-2.0.5.pom"/>
       <artifact:install file="deps/kryo2-2.05-all.jar">
          <pom refid="kryo-dep"/>
       </artifact:install>
   </target>

   <target name="build" depends="deps">
       <artifact:mvn mavenVersion="3.0.3" pom="./pom.xml">
           <arg value="package"/>
       </artifact:mvn>
   </target>
</project>

Advertisements