Log the Ant output to the Eclipse console
Have you ever written an Eclipse Plug-In that has to call an Ant-Script? OK, I’m sure you get this done. Maybe then you’ve ask yourself (or someone else
): “How do I get the Ant output into the console of Eclipse?” So, I have and also was asked.
My spontaneous and innocent answer: “Just call ‘addBuildLogger’ on your AntRunner.”
The Logger has to look like that:
public class EclipseAntLogger extends DefaultLogger {
private IOConsole console = new IOConsole("Ant", null);
private IOConsoleOutputStream outputStream = console.newOutputStream();
public EclipseAntLogger() {
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
}
@Override
protected void printMessage(String message, PrintStream stream, int priority) {
super.printMessage(message, stream, priority);
try {
outputStream.write(message + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}
The problem I’ve got with that was a ClassNotFound exception of my logger class. Of course! My AntRunner uses a different VM and with that a totally different ClassPath than the one of Eclipse.
How to put your logger class onto the AntRunner ClassPath?
-> Setup the custom classpath of the AntRunner.
// get the default custom classpath from the preferences AntCorePreferences corePreferences = AntCorePlugin.getPlugin().getPreferences(); URL[] urls = corePreferences.getURLs(); // get the location of the plugin jar File bundleFile = FileLocator.getBundleFile(myPlugin.getBundle()); URL url = bundleFile.toURI().toURL(); // bond urls to complete classpath List<URL> classpath = new ArrayList<URL>(); classpath.addAll(Arrays.asList(urls)); classpath.add(url); // set custom classpath antRunner.setCustomClasspath(classpath.toArray(new URL[classpath.size()]));
Not too nice in my opinion. Another possibility is to use an extension.
<extension point="org.eclipse.ant.core.extraClasspathEntries"> <extraClasspathEntry library="ant/logger.jar"> </extraClasspathEntry> </extension>
You have to put the logger class into another jar and add it to the extra classpath of the eclipse ant classpath.
Better! But I wasn’t almost happy with this solution.
After some days of work there was the flash of genius – or something like that
You know the laucher from the Eclipse-UI where you can create a new ‘External Tools Configurations…’?

In your code you have to use ‘ILaunchConfigurationType’ to create a new configuration. My solution looks like this:
ILaunchManager launchManager = DebugPlugin.getDefault().getLaunchManager(); ILaunchConfigurationType lcType = launchManager.getLaunchConfigurationType(IAntLaunchConstants.ID_ANT_LAUNCH_CONFIGURATION_TYPE); String name = launchManager.generateLaunchConfigurationName("Run Ant"); ILaunchConfigurationWorkingCopy wc = lcType.newInstance(null, name); wc.setAttribute(ILaunchManager.ATTR_PRIVATE, true); wc.setAttribute(IExternalToolConstants.ATTR_LOCATION, buildFile.toString()); wc.setAttribute(IAntLaunchConstants.ATTR_ANT_TARGETS, "run"); wc.launch(ILaunchManager.RUN_MODE, null);
It’s based on the new API that will be introduced with Eclipse 3.6 and requires at least these bundles:
- org.eclipse.debug.core
- org.eclipse.ant.launching
- org.eclipse.core.externaltools
It works beautiful I think? So what do you?
Do you have another suggestion how to solve the problem? Let me know.
be happy until cancelled,
Your Codescale’s
Hello,
nice Tutorial but I have one problem,
I added the Logger Class manually to the classpath but now I get the problem that the logger Class can’t be loaded because the class IOConsole is unknown..
Also If the Logger runs in a different VM how can the Logger logs to the current VM if in the other VM no ConsolePlugin is initialized?
Greets,
Gerald.
Hi Gerald,
First of all, is it necessary to use this way where you add the logger onto the Ant-Classpath? Or can you use my last suggested method, using the Launcher-API? This is way more easier…
If you need the Logger on the Ant-Classpath then I suggest you to check the classpath in the eclipse you try to run your code. Go to Preferences -> Ant -> Runtime -> Classpath there you see if all necessary Plug-Ins and jars are available. (How do you try to get the Logger onto the Classpath. In your Code or by an Extension?)
Regards, Matthias
P.S.: At the moment I have to travel a lot, so maybe I’m a bit lazy in my replies…
At the moment I used the last method (ILaunchConfigurationWorkingCopy) so there is no problem the output appears in the message console. The only problem is that the output goes to a different console window, so I have to switch the different console names.
Before I added the logger to the custom classpath with the required ant librarys. The Ant librarys I added with the help of AntCorePlugin.getPlugin().getPreferences();
But in these librarys no ui librarys are included such as IOConsole.
So the logger class cannot be loaded because of the missing classpath entrys of e.g. IOConsole.
Thanks for your reply.
Regards,
Gerald.