How to think about Ant task classpaths

I have a class which implements org.apache.tools.ant.Task.

I am able to run it from within gradle using the ant AntBuilder reference.
In order to do this I have to set the classpath, here is a snippet of the build.gradle file:

doFirst {
    ant.taskdef(name: ''TaskWhichNeedsClasspath,
      classname: 'hello.CustomAntTask',
      classpath: (configurations.compile).asPath
	)
    ant.TaskWhichNeedsClasspath()
  }

When the actual task runs though, it only has one entry on the classpath;

public class CustomAntTask extends Task {
	public void execute() {
		String classpathStr = System.getProperty("java.class.path");
		System.out.print("classpath: " + classpathStr);
		System.out.println("---------");
	}
}

[ant:TaskWhichNeedsClasspath] classpath: /home/someuser/.gradle/wrapper/dists/gradle-5.2.1-bin/9lc4nzslqh3ep7ml2tp68fk8s/gradle-5.2.1/lib/gradle-launcher-5.2.1.jar---------

Can anyone clue me in on this?

I do not understand how the CustomAntTask itself ant the minimum is not on the classpath, and then also how to actually get the classpath to include the rest of the items specified from in the build.gradle file via the ant.taskdef.

I need to be able to load resources from inside the ant task.

Thanks

Anyone who can throw this dog a bone:

Variations of this same question:

You shouldn’t concern yourself with the “java.class.path” system property as this will list Gradle’s bootstrap classpath which isn’t relevant. You’ll be interested in the classloader which loaded your ant task

Try something like

public class CustomAntTask extends Task {
   public void execute() {
      Classloader loader = getClass().getClassloader();
      String pathInsideMyJar = "com/foo/baz.xml";
      try (InputStream in = loader.getResourceAsStream(pathInsideMyJar)) {
         doStuff(in);
      }
   } 
} 
1 Like

Thanks very much that was an immeasurable assist!