How do I create the equivalent of an Ant PATH?

I am working on a proof of concept to convert an inhouse Ant build over to Gradle. The existing build uses PATHs and references extensively and I am getting a null when a path defined in an imported Ant file is tried to be used by the Javac Ant task.

I have a primary build and through the magic of the settings.gradle file I am calling sub-builds.

In the sub-build I am importing a build file that defines lots of useful paths so that I can change them in only one place and re-use them everywhere else.

ant.importBuild '../build-imports.xml'

I’m then making a Groovy variable out of it.

String basePath = ant.properties['base.path']

Here is my Javac line from the build:

ant.javac(srcDir: "${compileSourcecode}", destDir: "WebContent/WEB-INF/classes", source: "${compileSourceversion}", target: "${compileTargetversion}", verbose: "${compileVerbose}", debug: "${compileDebug}", deprecation: "${compileDeprecation}", optimize: "${compileOptimize}", classpathref: "${basePath}", includeAntRuntime: "${compileIncludeAntRuntime}")

The basePath variable is null instead of containing my nicely defined path.

The path in question looks like this:

<path id="base.path">
  <pathelement location="ApacheCommons/commons-lang/commons-lang.jar" />
  <pathelement location="log4j/log4j.jar" />
  <pathelement location="Acme/acme.jar" />
  <fileset dir="." erroronmissingdir="false">
   <include name="**/*.jar"/>
  </fileset>
  <fileset dir="../../${project.name}Model" erroronmissingdir="false">
   <include name="**/${project.name}Model.jar"/>
  </fileset>
  <fileset dir="../../${project.name}Persistance" erroronmissingdir="false">
   <include name="**/${project.name}Persistance.jar"/>
  </fileset>
    <fileset dir="../../${project.name}Service" erroronmissingdir="false">
   <include name="**/${project.name}Service.jar"/>
  </fileset>
  <path refid="server.path" />
 </path>

So how do I make this Gradle’ish? I haven’t found anything in either the Gradle userguide or the Oreilly Gradle book. Perhaps I’m just not using the right keywords?

Ant types with an id attribute are treated (by ant) as a reference, rather than a property. So, you should use this, instead:

def basePath = ant.references['base.path']

This will be an object of type ‘org.apache.tools.ant.types.Path’, which you can pass directly into the javac task, or convert to a String using something like:

def pathAsString = basePath.list().join(File.pathSeparator)

Adam, Thanks. I tried this and now it claims that the reference is not found. A step forward, but the reference absolutely does exist.

ant.references is a Map - you might iterate over the keys to see what’s there.

The proof of concept is now over. Thank you for your help. Unfortunately, but understandably in the circumstances, the decision was made to stick with Ant builds for now.