Search code examples
antresourcesincludeexternaltask

Include external resources in Ant build task


I have an Ant build script to generate some classes. The process requires an external library. Since I need this task for a lot of different modules I don't want to copy the lib everytime and I don't want to reference it localy as that would require all developers to download the lib first. How do I reference and include an external resource? This is my setup so far

<project name="generate" basedir=".">

    <property name="src" location="src/main/java"/>
    <property name="generated" location="target/classes"/>
    <property name="build" location="src/main/java"/>

    <path id="cp">
        <fileset dir="path/to/lib" includes="**/querydsl-jpa-2.2.3-apt-one-jar.jar"/>
    <fileset dir="path/to/.m2" includes="**/*.jar"/>
    </path>


  <target name="compile" >
    <!-- Compile the java code from ${src} into ${build} -->
    <javac srcdir="${src}" classpathref="cp" includeantruntime="false">
      <compilerarg value="-proc:only"/>      
      <compilerarg value="-processor"/>
      <compilerarg value="com.mysema.query.apt.QuerydslAnnotationProcessor"/>
      <compilerarg value="-s"/>
      <compilerarg value="${generated}"/>
    </javac>

    <!-- compilation -->
    <javac classpathref="cp" destdir="${build}" includeantruntime="false">      
      <src path="${src}"/>
      <src path="${generated}"/>
    </javac>  
  </target>

</project>

Now I have the querydsl-jpa-2.2.3-apt-one-jar.jar at some remote location and I also want to reference our internal maven repository instead of the local .m2 directory, so I need to reference remote locations and include the libraries from there.


Solution

  • I would suggest you use the Apache Ivy Ant plug-in. It can help you in two ways:

    1. Ivy can download libraries in the same manner as Maven
    2. Your library is already available from Maven Central (No need to store it on your own website)

    Developers using your project will only require the Ivy jar installed into one of the following locations:

    • $HOME/.ant/lib
    • $ANT_HOME/lib

    build.xml

    The Ivy resolve task downloads (and caches) dependencies (found in the ivy.xml file). The cachepath task automatically populates the classpath:

    <project name="generate" basedir="." xmlns:ivy="antlib:org.apache.ivy.ant">
    
        <property name="src" location="src/main/java"/>
        <property name="generated" location="target/classes"/>
        <property name="build" location="src/main/java"/>
    
        <target name="resolve" >
            <ivy:resolve/>
            <ivy:cachepath pathid="cp" conf="compile"/>
        </target>
    
        <target name="compile" depends="resolve">
        ..
    

    ivy.xml

    Dependencies are declared here:

    <ivy-module version="2.0" xmlns:e="http://ant.apache.org/ivy/extra">
    
        <info organisation="com.myspotontheweb" module="demo"/>
    
        <configurations defaultconfmapping="compile->default"/>
        
        <dependencies>
            <!-- Your jar -->
            <dependency org="com.mysema.querydsl" name="querydsl-jpa" rev="2.2.3" >
                <artifact name="querydsl-jpa" type="jar" m:classifier="apt-one-jar"/>
            </dependency>
    
            <!-- Other Maven dependencies -->
            <dependency org="commons-lang" name="commons-lang" rev="2.6"/>
            ..
    
        </dependencies>
    
    </ivy-module>
    

    The querydsl-jpa-2.2.3-apt-one-jar.jar jar is special needs an additional classifier specification. If it was a Maven dependency it would be declared as follows:

    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>2.2.3</version>
        <classifier>apt-one-jar</classifier>
    </dependency>
    

    ivysettings.xml

    Ivy can be configured to use your Maven repository (Just like a Maven client):

    <ivysettings>
        <settings defaultResolver="maven-repo"/>
        <resolvers>
            <ibiblio name="maven-repo" m2compatible="true" root="http://myrepo.mycompany.com/maven-central-proxy"/>
        </resolvers>
    </ivysettings>