Can someone point me to a good, reference example of consuming an Ant
project programmatically? The code needs to have access to the
org.apache.tools.ant.Project 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
project.init();
ProjectHelper.configureProject(project, buildFile); // buildFile is a File
object

>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
classpath.

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
grasping.

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.

Reply via email to