ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nicolas Lalevée <>
Subject Re: example of correctly consuming an Ant project programmatically
Date Thu, 31 May 2012 21:17:31 GMT

Le 31 mai 2012 à 18:33, Mitch Gitman a écrit :

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

You have indeed to properly setup the classloader used by the Project. If you want to setup
a classloader like the one which is used at the boostrap of Ant, when it is picking up jars
in the .ant/lib folder for instance, and also the tools.jar (see when Locator.getToolsJar()
is called), you should look into the code of[]).

I have never played with an AntClassLoader, but probably a classical URLClassloader will work
just fine.


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message