Search in sources :

Example 46 with Project

use of org.apache.tools.ant.Project in project gradle by gradle.

the class AntUtil method createProject.

/**
     * @return Factory method to create new Project instances
     */
public static Project createProject() {
    final Project project = new Project();
    final ProjectHelper helper = ProjectHelper.getProjectHelper();
    project.addReference(ProjectHelper.PROJECTHELPER_REFERENCE, helper);
    // import checks that stack is not empty
    helper.getImportStack().addElement("AntBuilder");
    project.addBuildListener(new AntLoggingAdapter());
    project.init();
    project.getBaseDir();
    return project;
}
Also used : Project(org.apache.tools.ant.Project) ProjectHelper(org.apache.tools.ant.ProjectHelper) AntLoggingAdapter(org.gradle.api.internal.project.ant.AntLoggingAdapter)

Example 47 with Project

use of org.apache.tools.ant.Project in project groovy-core by groovy.

the class Groovy method parseAndRunScript.

private void parseAndRunScript(GroovyShell shell, String txt, Object mavenPom, String scriptName, File scriptFile, AntBuilder builder) {
    try {
        final Script script;
        if (scriptFile != null) {
            script = shell.parse(scriptFile);
        } else {
            script = shell.parse(txt, scriptName);
        }
        final Project project = getProject();
        script.setProperty("ant", builder);
        script.setProperty("project", project);
        script.setProperty("properties", new AntProjectPropertiesDelegate(project));
        script.setProperty("target", getOwningTarget());
        script.setProperty("task", this);
        script.setProperty("args", cmdline.getCommandline());
        if (mavenPom != null) {
            script.setProperty("pom", mavenPom);
        }
        script.run();
    } catch (final MissingMethodException mme) {
        // not a script, try running through run method but properties will not be available
        if (scriptFile != null) {
            try {
                shell.run(scriptFile, cmdline.getCommandline());
            } catch (IOException e) {
                processError(e);
            }
        } else {
            shell.run(txt, scriptName, cmdline.getCommandline());
        }
    } catch (final CompilationFailedException e) {
        processError(e);
    } catch (IOException e) {
        processError(e);
    }
}
Also used : Script(groovy.lang.Script) Project(org.apache.tools.ant.Project) MissingMethodException(groovy.lang.MissingMethodException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 48 with Project

use of org.apache.tools.ant.Project in project groovy-core by groovy.

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 ("".equals(txt.trim())) {
        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) {
            StringWriter writer = new StringWriter();
            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");
            contextField.setAccessible(true);
            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 = 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) AntBuilder(groovy.util.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) StringWriter(java.io.StringWriter) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) PrintWriter(java.io.PrintWriter)

Example 49 with Project

use of org.apache.tools.ant.Project in project groovy-core by groovy.

the class GroovyTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    project = new Project();
    project.init();
    ProjectHelper.getProjectHelper().parse(project, antFile);
    FLAG = null;
}
Also used : Project(org.apache.tools.ant.Project)

Example 50 with Project

use of org.apache.tools.ant.Project in project hudson-2.x by hudson.

the class Util method createFileSet.

/**
     * Creates Ant {@link FileSet} with the base dir and include pattern.
     *
     * <p>
     * The difference with this and using {@link FileSet#setIncludes(String)}
     * is that this method doesn't treat whitespace as a pattern separator,
     * which makes it impossible to use space in the file path.
     *
     * @param includes
     *      String like "foo/bar/*.xml" Multiple patterns can be separated
     *      by ',', and whitespace can surround ',' (so that you can write
     *      "abc, def" and "abc,def" to mean the same thing.
     * @param excludes
     *      Exclusion pattern. Follows the same format as the 'includes' parameter.
     *      Can be null.
     * @since 1.172
     */
public static FileSet createFileSet(File baseDir, String includes, String excludes) {
    FileSet fs = new FileSet();
    fs.setDir(baseDir);
    fs.setProject(new Project());
    StringTokenizer tokens;
    tokens = new StringTokenizer(includes, ",");
    while (tokens.hasMoreTokens()) {
        String token = tokens.nextToken().trim();
        fs.createInclude().setName(token);
    }
    if (excludes != null) {
        tokens = new StringTokenizer(excludes, ",");
        while (tokens.hasMoreTokens()) {
            String token = tokens.nextToken().trim();
            fs.createExclude().setName(token);
        }
    }
    return fs;
}
Also used : Project(org.apache.tools.ant.Project) StringTokenizer(java.util.StringTokenizer) QuotedStringTokenizer(hudson.util.QuotedStringTokenizer) FileSet(org.apache.tools.ant.types.FileSet)

Aggregations

Project (org.apache.tools.ant.Project)76 File (java.io.File)32 BuildException (org.apache.tools.ant.BuildException)23 IOException (java.io.IOException)17 Test (org.junit.Test)12 FileSet (org.apache.tools.ant.types.FileSet)11 ZipFile (java.util.zip.ZipFile)10 IgniteDeploymentGarAntTask (org.apache.ignite.util.antgar.IgniteDeploymentGarAntTask)8 DefaultLogger (org.apache.tools.ant.DefaultLogger)5 ProjectHelper (org.apache.tools.ant.ProjectHelper)5 Path (org.apache.tools.ant.types.Path)5 MissingMethodException (groovy.lang.MissingMethodException)4 DirectoryScanner (org.apache.tools.ant.DirectoryScanner)4 Task (org.apache.tools.ant.Task)4 AbstractProject (hudson.model.AbstractProject)3 Target (org.apache.tools.ant.Target)3 Zip (org.apache.tools.ant.taskdefs.Zip)3 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)3 Binding (groovy.lang.Binding)2 GroovyClassLoader (groovy.lang.GroovyClassLoader)2