When running ant, prefer Main class over Launcher because IDEA builds all the necessary classpath on its own. Otherwise, with Launcher, weird problems possible because of screwed up class-loading (IDEA-105828 Ant javac fails with org.apache.tools.ant.taskdefs.Javac was not found with JAXB 2.2.7, works outside of IDEA)

This commit is contained in:
Eugene Zhuravlev
2013-04-21 13:00:18 +04:00
parent d2692ccc65
commit e6137deae8
@@ -21,20 +21,25 @@ public final class AntMain2 {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
IdeaAntLogger2.guardStreams();
// first try to use the new way of launching ant
// as we build classpath ourselves, and ensure all libraries are added to classpath,
// preferred way for us to run ant will be using the traditional ant entry point, via the "Main" class
try {
final Class antLauncher = Class.forName("org.apache.tools.ant.launch.Launcher");
final Class antMain = Class.forName("org.apache.tools.ant.Main");
//noinspection HardCodedStringLiteral
antLauncher.getMethod("main", new Class[]{args.getClass()}).invoke(null, new Object[]{args});
antMain.getMethod("main", new Class[]{args.getClass()}).invoke(null, new Object[]{args});
return;
}
catch (ClassNotFoundException e) {
// ignore and try older variant
// ignore
}
final Class antMain = Class.forName("org.apache.tools.ant.Main");
// fallback: try the newer approach, launcher
// This approach is less preferred in our case, but still...
// From the ant documentation: "You should start the launcher with the most minimal classpath possible, generally just the ant-launcher.jar."
final Class antLauncher = Class.forName("org.apache.tools.ant.launch.Launcher");
//noinspection HardCodedStringLiteral
antMain.getMethod("main", new Class[]{args.getClass()}).invoke(null, new Object[]{args});
antLauncher.getMethod("main", new Class[]{args.getClass()}).invoke(null, new Object[]{args});
}
}