Ant Integration: "cannot find symbol" when using an Ant task to compile a custom task

Hello, I just started to learn about Gradle and I’m currently playing around a little. The first thing I’m interested in is the Ant integration, so I wrote an Ant script that compiles a custom Ant task and then uses this task. Then I wanted to use the Ant tasks from Gradle. Unfortunately, while this works perfectly in Ant, it does not work when I want to use these Ant tasks with Gradle. Instead, I get “package org.apache.tools.ant does not exist” and “cannot find symbol: class Task”. Since Gradle fully integrates Ant, I thought that this should work out of the box but it seems like Gradle does not include the Ant Jar file from its lib directory. Can anyone explain to me, why there is a difference between Ant and Gradle here and how I can get this to work? Here is my code:

The custom Ant task:

package example.ant.tasks;
  import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
  public class MyAntTask extends Task
{
  public MyAntTask()
 {
  }
    public void execute() throws BuildException
 {
   log("*******MyAntTask*********");
 }
}

The Ant build script:

<project name="ant-example" default="useTask" basedir=".">
    <target name="compileTask">
  <mkdir dir="../src/MyAntTask/bin"/>
              <javac destdir="../src/MyAntTask/bin">
   <src path="../src/MyAntTask/src"/>
  </javac>
 </target>
   <target name="useTask" depends="compileTask">
  <taskdef name = "myAntTask" classname = "example.ant.tasks.MyAntTask" classpath= "../src/MyAntTask/bin"/>
  <myAntTask/>
 </target>
    <target name="clean">
  <delete dir="../src/MyAntTask/bin"/>
 </target>
  </project>

The Gradle build script

ant.importBuild 'build.xml'

There are a few subtle differences/limitations when importing an Ant build into Gradle, and you may have hit one of them.

If it’s OK to tweak the Ant build, one way out would be to configure the javac Ant task with ‘includeAntRuntime=false’ (as recommended in the task’s documentation) and add an explicit dependency on the Ant Jar. This also lets you control the Ant version to compile against. (Note that Gradle always executes Ant tasks with Ant 1.8.2.)

Hello Peter,

thank you for your answer, I did what you suggested and it worked perfectly. Another question out of curiosity: Is it possible to add the dependency on the Ant Jar in the Gradle build file?I tried:

ant.importBuild 'build.xml'
ant.references.classpath = ant.path(location: '/Dev/gradle-1.0-milestone-7/lib/ant-1.8.2.jar')
}

and

ant.importBuild 'build.xml'
compileTask.doFirst {
 ant.references.classpath = ant.path(location: '/Dev/gradle-1.0-milestone-7/lib/ant-1.8.2.jar')
}

but none of it worked.

Hello, I’m trying to migrate our ant build process to gradle and ran into the same problem.

The point is, when I am compiling my custom ant task I want them to depend on the build system. So when switching to gradle I’m loosing that. This means I have to adjust my ant build process for compiling the custom ant tasks.