## Running Ant buildfiles programmatically

The Ant support built into Eclipse allows plug-ins to programmatically run Ant buildfiles. This is done via the AntRunner class included in the org.eclipse.ant.core plug-in.

The following code snippet shows an example of how to use the AntRunner from within code of another plug-in:

    import org.eclipse.ant.core.AntRunner;
    import org.eclipse.core.runtime.IProgressMonitor;

    ...

    public void runBuild() {
    	IProgressMonitor monitor = ...
    	AntRunner runner = new AntRunner();
    	runner.setBuildFileLocation("c:/buildfiles/build.xml");
    	runner.setArguments("-Dmessage=Building -verbose");
    	runner.run(monitor);
    }

If a progress monitor is used, it is made available for the running tasks. See Progress Monitors for more details.

Note that only one Ant build can occur at any given time if the builds do not occur in separate VMs. See AntRunner.isBuildRunning();

### Special care for native libraries if build occurs within the same JRE as the workspace

Every time an Ant buildfile runs in Eclipse a new classloader is created. Since a library can only be loaded by one classloader in Java, tasks making use of native libraries could run into problems during multiple buildfile runs. If the previous classloader has not been garbage collected at the time the new classloader tries to load the native library, an exception is thrown indicating the problem and the build fails. One way of avoiding this problem is having the library load be handled by a class inside a plug-in library. The task can make use of that class for accessing native methods. This way, the library is loaded by the plug-in classloader and it does not run into the load library conflict.