Search in sources :

Example 1 with RootLoader

use of org.codehaus.groovy.tools.RootLoader in project groovy by apache.

the class Groovyc method buildClassLoaderFor.

protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) parent;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
             * Iterate over the classpath provided to groovyc, and add any missing path
             * entries to the AntClassLoader.  This is a workaround, since for some reason
             * 'directory' classpath entries were not added to the AntClassLoader' classpath.
             */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
                 * fix for GROOVY-2284
                 * seems like AntClassLoader doesn't check if the file
                 * may not exist in the classpath yet
                 */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }
    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        loader.setResourceLoader(new GroovyResourceLoader() {

            public URL loadGroovySource(String filename) throws MalformedURLException {
                return null;
            }
        });
    }
    return loader;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 2 with RootLoader

use of org.codehaus.groovy.tools.RootLoader in project groovy by apache.

the class RootLoaderRef method makeRoot.

private RootLoader makeRoot() {
    String[] list = taskClasspath.list();
    LoaderConfiguration lc = new LoaderConfiguration();
    for (int i = 0; i < list.length; i++) {
        if (list[i].matches(".*ant-[^/]*jar$")) {
            continue;
        }
        if (list[i].matches(".*commons-logging-[^/]*jar$")) {
            continue;
        }
        if (list[i].matches(".*xerces-[^/]*jar$")) {
            continue;
        }
        lc.addFile(list[i]);
    }
    return new RootLoader(lc);
}
Also used : RootLoader(org.codehaus.groovy.tools.RootLoader) LoaderConfiguration(org.codehaus.groovy.tools.LoaderConfiguration)

Example 3 with RootLoader

use of org.codehaus.groovy.tools.RootLoader in project groovy-core by groovy.

the class Groovyc method buildClassLoaderFor.

protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
        throw new IllegalArgumentException("The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent = getIncludeantruntime() ? getClass().getClassLoader() : new AntClassLoader(new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
        AntClassLoader antLoader = (AntClassLoader) parent;
        String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
        List<String> classpath = configuration.getClasspath();
        /*
             * Iterate over the classpath provided to groovyc, and add any missing path
             * entries to the AntClassLoader.  This is a workaround, since for some reason
             * 'directory' classpath entries were not added to the AntClassLoader' classpath.
             */
        for (String cpEntry : classpath) {
            boolean found = false;
            for (String path : pathElm) {
                if (cpEntry.equals(path)) {
                    found = true;
                    break;
                }
            }
            /*
                 * fix for GROOVY-2284
                 * seems like AntClassLoader doesn't check if the file
                 * may not exist in the classpath yet
                 */
            if (!found && new File(cpEntry).exists()) {
                try {
                    antLoader.addPathElement(cpEntry);
                } catch (BuildException e) {
                    log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
                }
            }
        }
    }
    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
        // in normal case we don't need to do script lookups
        loader.setResourceLoader(new GroovyResourceLoader() {

            public URL loadGroovySource(String filename) throws MalformedURLException {
                return null;
            }
        });
    }
    return loader;
}
Also used : MalformedURLException(java.net.MalformedURLException) AntClassLoader(org.apache.tools.ant.AntClassLoader) URL(java.net.URL) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovyResourceLoader(groovy.lang.GroovyResourceLoader) RootLoader(org.codehaus.groovy.tools.RootLoader) AntClassLoader(org.apache.tools.ant.AntClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) BuildException(org.apache.tools.ant.BuildException) File(java.io.File)

Example 4 with RootLoader

use of org.codehaus.groovy.tools.RootLoader in project groovy-core by groovy.

the class RootLoaderRef method makeRoot.

private RootLoader makeRoot() {
    String[] list = taskClasspath.list();
    LoaderConfiguration lc = new LoaderConfiguration();
    for (int i = 0; i < list.length; i++) {
        if (list[i].matches(".*ant-[^/]*jar$")) {
            continue;
        }
        if (list[i].matches(".*commons-logging-[^/]*jar$")) {
            continue;
        }
        if (list[i].matches(".*xerces-[^/]*jar$")) {
            continue;
        }
        lc.addFile(list[i]);
    }
    return new RootLoader(lc);
}
Also used : RootLoader(org.codehaus.groovy.tools.RootLoader) LoaderConfiguration(org.codehaus.groovy.tools.LoaderConfiguration)

Aggregations

RootLoader (org.codehaus.groovy.tools.RootLoader)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)2 GroovyResourceLoader (groovy.lang.GroovyResourceLoader)2 File (java.io.File)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 AntClassLoader (org.apache.tools.ant.AntClassLoader)2 BuildException (org.apache.tools.ant.BuildException)2 LoaderConfiguration (org.codehaus.groovy.tools.LoaderConfiguration)2