use of org.apache.tools.ant.ExitException in project maven-plugins by apache.
the class AntWrapper method invoke.
/**
* Invoke <code>Ant</code> for the default target of a given build file.
*
* @param antBuild an <code>Ant build</code> file
* @throws IllegalArgumentException if any
* @throws BuildException if any
*/
public static void invoke(File antBuild) throws BuildException, IllegalArgumentException {
if (!antBuild.exists()) {
throw new IllegalArgumentException("antBuild should exist");
}
if (!antBuild.isFile()) {
throw new IllegalArgumentException("antBuild should be a file");
}
// ----------------------------------------------------------------------
// NB: By using org.apache.tools.ant.launch.Launcher, we have:
// java.lang.ClassCastException
// at org.apache.tools.ant.launch.Launcher.run(Launcher.java:245)
// So, using org.apache.tools.ant.Main#main()
// ----------------------------------------------------------------------
Properties oldSystemProperties = System.getProperties();
System.setProperty("basedir", antBuild.getParentFile().getAbsolutePath());
SecurityManager oldSm = System.getSecurityManager();
System.setSecurityManager(new NoExitSecurityManager());
PrintStream oldErr = System.err;
OutputStream errOS = new ByteArrayOutputStream();
PrintStream err = new PrintStream(errOS);
System.setErr(err);
PrintStream oldOut = System.out;
OutputStream outOS = new ByteArrayOutputStream();
PrintStream out = new PrintStream(outOS);
System.setOut(out);
// ----------------------------------------------------------------------
// To prevent Javac exception i.e. "Unable to find a javac compiler"
// Ant can use the same command line arguments as the javac of the current VM
// ----------------------------------------------------------------------
System.setProperty("build.compiler", "extJavac");
try {
Main.main(new String[] { "-f", antBuild.getAbsolutePath() });
} catch (ExitException e) {
if (StringUtils.isNotEmpty(errOS.toString())) {
throw new BuildException("Error in the Ant build file. \n= Ant output =\n" + outOS.toString() + "\n" + errOS.toString());
}
} finally {
System.setSecurityManager(oldSm);
System.setErr(oldErr);
System.setOut(oldOut);
System.setProperties(oldSystemProperties);
}
}
Aggregations