0
votes

I want to use an Ant task that is defined in an artifact. The artifact exists in the main Maven repositories and has some dependencies.

I want to use Ivy and Ant to:

  1. Declare the dependency on that artifact and its transitive dependencies, so that they are resolved when the script is run.
  2. Retrieve all the jar files as an Ant path, that I can feed into Ant's taskdef.
  3. Refer to that set of resolved jar files in some other part of the build script.

So far, the documentation I have found does not optimize for this use case. Instead, it suggests to write the files ivy.xml, ivysettings.xml; I don't like that, the dependencies are small enough that I would like to fit everything in a single build script.

Any ideas?

1

1 Answers

1
votes

The ivy cachepath task is a resolve task that can be used to create an ANT path. What is perhaps not well known is that this resolving task can also be used inline, in other words, you can specify the dependencies directly without an ivy file.

<ivy:cachepath pathid="tasks.path">
    <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
</ivy:cachepath>

For a related answer that utilizes an ivy file to manage multiple classpaths:

Example

The following example is a little contrived by demonstrates ivy downloading the jar associated with the groovy task. I have also included a utility target that I use to install the ivy jar as well.

build.xml

<project name="demo" default="build" xmlns:ivy="antlib:org.apache.ivy.ant">

    <available classname="org.apache.ivy.Main" property="ivy.installed"/> 

    <!--
    ============
    Main targets 
    ============
    -->
    <target name="resolve" depends="install-ivy">
        <ivy:cachepath pathid="tasks.path">
            <dependency org="org.codehaus.groovy" name="groovy-all" rev="2.4.7" conf="default"/>
        </ivy:cachepath>
    </target>

    <target name="build" depends="resolve">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy" classpathref="tasks.path"/>

        <groovy>
          ant.echo "Hello world"
        </groovy>
    </target>

    <!--
    ==================
    Supporting targets
    ==================
    -->
    <target name="install-ivy" description="Install ivy" unless="ivy.installed">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/ivy.jar" src="http://search.maven.org/remotecontent?filepath=org/apache/ivy/ivy/2.4.0/ivy-2.4.0.jar"/>
        <fail message="Ivy has been installed. Run the build again"/>
    </target>

    <target name="clean" description="Cleanup build files">
        <delete dir="${build.dir}"/>
    </target>

    <target name="clean-all" depends="clean" description="Additionally purge ivy cache">
        <ivy:cleancache/>
    </target>

</project>