ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Mitch Gitman <>
Subject example of correctly consuming an Ant project programmatically
Date Thu, 31 May 2012 16:33:03 GMT
Can someone point me to a good, reference example of consuming an Ant
project programmatically? The code needs to have access to the object.

I have been able to do this, but with just one catch. Below are the
relevant lines of Java code:
Project project = new Project();
project.setBaseDir(basedir); // basedir is a File object
ProjectHelper.configureProject(project, buildFile); // buildFile is a File

>From here, I can call executeTarget on the Project object as needed.

Everything works fine, but here's the catch. Whatever the code is that
instantiates the Project object, the Project will take the libraries in the
classpath for that code (typically a JUnit test) and put that in its own

So, for example, if the programmatically instantiated Ant needs to call the
javac Ant task, then I would need to add lib/tools.jar in the JDK to the
runtime classpath for the code that launches Ant. Likewise, there might be
some libraries that are needed in the test's classpath but aren't needed by
the Ant project that is launched by the test but which will then leak into
the Project's classpath and wreak havoc.

My guess is there's something about the coreLoader member of the Project
object I need to be controlling. I've also looked at the AntClassLoader
class. My fear is there's something really fundamental about the Ant
ClassLoader, or maybe just ClassLoaders in general, that I'm not quite

This is where it would be great if there's an existing, isolated example of
creating a Project that has the expected JARs in Ant lib plus tools.jar but
nothing more. This seems like the kind of thing that people have had to do
countless times before.

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message