How can I best share Ant targets between projects?

Posted by Rob Hruska on Stack Overflow See other posts from Stack Overflow or by Rob Hruska
Published on 2009-12-01T21:28:41Z Indexed on 2011/01/05 1:53 UTC
Read the original article Hit count: 682

Is there a well-established way to share Ant targets between projects? I have a solution currently, but it's a bit inelegant. Here's what I'm doing so far.

I've got a file called ivy-tasks.xml hosted on a server on our network. This file contains, among other targets, boilerplate tasks for managing project dependencies with Ivy. For example:

<project name="ant-ivy-tasks" default="init-ivy"
         xmlns:ivy="antlib:org.apache.ivy.ant">
  ...
  <target name="ivy-download" unless="skip.ivy.download">
    <mkdir dir="${ivy.jar.dir}"/>
    <echo message="Installing ivy..."/>
    <get src="http://repo1.maven.org/maven2/org/apache/ivy/ivy/${ivy.install.version}/ivy-${ivy.install.version}.jar"
         dest="${ivy.jar.file}" usetimestamp="true"/>
  </target>

  <target name="ivy-init" depends="ivy-download"
          description="-> Defines ivy tasks and loads global settings">
    <path id="ivy.lib.path">
      <fileset dir="${ivy.jar.dir}" includes="*.jar"/>
    </path>
    <taskdef resource="org/apache/ivy/ant/antlib.xml"
             uri="antlib:org.apache.ivy.ant"
             classpathref="ivy.lib.path"/>
    <ivy:settings url="http://myserver/ivy/settings/ivysettings-user.xml"/>
  </target>
  ...
</project>

The reason this file is hosted is because I don't want to:

  • Check the file into every project that needs it - this will result in duplication, making maintaining the targets harder.
  • Have my build.xml depend on checking out a project from source control - this will make the build have more XML at the top-level just to access the file.

What I do with this file in my projects' build.xmls is along the lines of:

<property name="download.dir" location="download"/>
<mkdir dir="${download.dir}"/>
<echo message="Downloading import files to ${download.dir}"/>

<get src="http://myserver/ivy/ivy-tasks.xml" dest="${download.dir}/ivy-tasks.xml" usetimestamp="true"/>
<import file="${download.dir}/ivy-tasks.xml"/>

The "dirty" part about this is that I have to do the above steps outside of a target, because the import task must be at the top-level. Plus, I still have to include this XML in all of the build.xml files that need it (i.e. there's still some amount of duplication).

On top of that, there might be additional situations where I might have common (non-Ivy) tasks that I'd like imported. If I were to provide these tasks using Ivy's dependency management I'd still have problems, since by the time I'd have resolved the dependencies I would have to be inside of a target in my build.xml, and unable to import (due to the constraint mentioned above).

Is there a better solution for what I'm trying to accomplish?

© Stack Overflow or respective owner

Related posts about ant

Related posts about build