Search in sources :

Example 1 with NoExitSecurityManager

use of org.apache.tools.ant.util.optional.NoExitSecurityManager 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);
    }
}
Also used : PrintStream(java.io.PrintStream) NoExitSecurityManager(org.apache.tools.ant.util.optional.NoExitSecurityManager) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BuildException(org.apache.tools.ant.BuildException) Properties(java.util.Properties) NoExitSecurityManager(org.apache.tools.ant.util.optional.NoExitSecurityManager) ExitException(org.apache.tools.ant.ExitException)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Properties (java.util.Properties)1 BuildException (org.apache.tools.ant.BuildException)1 ExitException (org.apache.tools.ant.ExitException)1 NoExitSecurityManager (org.apache.tools.ant.util.optional.NoExitSecurityManager)1