Search in sources :

Example 1 with AntBuilder

use of groovy.ant.AntBuilder in project groovy by apache.

the class Groovy method main.

public static void main(String[] args) {
    final GroovyShell shell = new GroovyShell(new Binding());
    final Groovy groovy = new Groovy();
    for (int i = 1; i < args.length; i++) {
        final Commandline.Argument argument = groovy.createArg();
        argument.setValue(args[i]);
    }
    final AntBuilder builder = new AntBuilder();
    groovy.setProject(builder.getProject());
    groovy.parseAndRunScript(shell, null, null, null, new File(args[0]), builder);
}
Also used : Binding(groovy.lang.Binding) Commandline(org.apache.tools.ant.types.Commandline) AntBuilder(groovy.ant.AntBuilder) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 2 with AntBuilder

use of groovy.ant.AntBuilder in project groovy by apache.

the class Groovy method execGroovy.

/**
 * Exec the statement.
 *
 * @param txt the groovy source to exec
 * @param out not used?
 */
protected void execGroovy(final String txt, final PrintStream out) {
    log.debug("execGroovy()");
    // Check and ignore empty statements
    if (txt.trim().isEmpty()) {
        return;
    }
    log.verbose("Script: " + txt);
    if (classpath != null) {
        log.debug("Explicit Classpath: " + classpath.toString());
    }
    if (fork) {
        log.debug("Using fork mode");
        try {
            createClasspathParts();
            createNewArgs(txt);
            super.setFork(fork);
            super.setClassname(useGroovyShell ? "groovy.lang.GroovyShell" : "org.codehaus.groovy.ant.Groovy");
            configureCompiler();
            super.execute();
        } catch (Exception e) {
            Writer writer = new StringBuilderWriter();
            new ErrorReporter(e, false).write(new PrintWriter(writer));
            String message = writer.toString();
            throw new BuildException("Script Failed: " + message, e, getLocation());
        }
        return;
    }
    Object mavenPom = null;
    final Project project = getProject();
    final ClassLoader baseClassLoader;
    ClassLoader savedLoader = null;
    final Thread thread = Thread.currentThread();
    boolean maven = "org.apache.commons.grant.GrantProject".equals(project.getClass().getName());
    // treat the case Ant is run through Maven, and
    if (maven) {
        if (contextClassLoader) {
            throw new BuildException("Using setContextClassLoader not permitted when using Maven.", getLocation());
        }
        try {
            final Object propsHandler = project.getClass().getMethod("getPropsHandler").invoke(project);
            final Field contextField = propsHandler.getClass().getDeclaredField("context");
            ReflectionUtils.trySetAccessible(contextField);
            final Object context = contextField.get(propsHandler);
            mavenPom = InvokerHelper.invokeMethod(context, "getProject", EMPTY_OBJECT_ARRAY);
        } catch (Exception e) {
            throw new BuildException("Impossible to retrieve Maven's Ant project: " + e.getMessage(), getLocation());
        }
        // load groovy into "root.maven" classloader instead of "root" so that
        // groovy script can access Maven classes
        baseClassLoader = mavenPom.getClass().getClassLoader();
    } else {
        baseClassLoader = GroovyShell.class.getClassLoader();
    }
    if (contextClassLoader || maven) {
        savedLoader = thread.getContextClassLoader();
        thread.setContextClassLoader(GroovyShell.class.getClassLoader());
    }
    final String scriptName = computeScriptName();
    final GroovyClassLoader classLoader = VMPluginFactory.getPlugin().doPrivileged((PrivilegedAction<GroovyClassLoader>) () -> new GroovyClassLoader(baseClassLoader));
    addClassPathes(classLoader);
    configureCompiler();
    final GroovyShell groovy = new GroovyShell(classLoader, new Binding(), configuration);
    try {
        parseAndRunScript(groovy, txt, mavenPom, scriptName, null, new AntBuilder(this));
    } finally {
        groovy.resetLoadedClasses();
        groovy.getClassLoader().clearCache();
        if (contextClassLoader || maven)
            thread.setContextClassLoader(savedLoader);
    }
}
Also used : Binding(groovy.lang.Binding) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) AntBuilder(groovy.ant.AntBuilder) MissingMethodException(groovy.lang.MissingMethodException) BuildException(org.apache.tools.ant.BuildException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) GroovyShell(groovy.lang.GroovyShell) GroovyClassLoader(groovy.lang.GroovyClassLoader) Project(org.apache.tools.ant.Project) Field(java.lang.reflect.Field) ErrorReporter(org.codehaus.groovy.tools.ErrorReporter) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) StringBuilderWriter(org.apache.groovy.io.StringBuilderWriter) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter)

Aggregations

AntBuilder (groovy.ant.AntBuilder)2 Binding (groovy.lang.Binding)2 GroovyShell (groovy.lang.GroovyShell)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 MissingMethodException (groovy.lang.MissingMethodException)1 File (java.io.File)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 Writer (java.io.Writer)1 Field (java.lang.reflect.Field)1 StringBuilderWriter (org.apache.groovy.io.StringBuilderWriter)1 BuildException (org.apache.tools.ant.BuildException)1 Project (org.apache.tools.ant.Project)1 Commandline (org.apache.tools.ant.types.Commandline)1 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)1 ErrorReporter (org.codehaus.groovy.tools.ErrorReporter)1