Search in sources :

Example 1 with GroovyJarFile

use of org.gradle.api.internal.plugins.GroovyJarFile in project gradle by gradle.

the class GroovyRuntime method inferGroovyClasspath.

/**
     * Searches the specified class path for Groovy Jars ({@code groovy(-indy)}, {@code groovy-all(-indy)}) and returns a corresponding class path for executing Groovy tools such as the Groovy
     * compiler and Groovydoc tool. The tool versions will match those of the Groovy Jars found. If no Groovy Jars are found on the specified class path, a class path with the contents of the {@code
     * groovy} configuration will be returned.
     *
     * <p>The returned class path may be empty, or may fail to resolve when asked for its contents.
     *
     * @param classpath a class path containing Groovy Jars
     * @return a corresponding class path for executing Groovy tools such as the Groovy compiler and Groovydoc tool
     */
public FileCollection inferGroovyClasspath(final Iterable<File> classpath) {
    // would differ in at least the following ways: 1. live 2. no autowiring
    return new LazilyInitializedFileCollection() {

        @Override
        public String getDisplayName() {
            return "Groovy runtime classpath";
        }

        @Override
        public FileCollection createDelegate() {
            GroovyJarFile groovyJar = findGroovyJarFile(classpath);
            if (groovyJar == null) {
                throw new GradleException(String.format("Cannot infer Groovy class path because no Groovy Jar was found on class path: %s", classpath));
            }
            if (groovyJar.isGroovyAll()) {
                return Cast.cast(FileCollectionInternal.class, project.files(groovyJar.getFile()));
            }
            if (project.getRepositories().isEmpty()) {
                throw new GradleException("Cannot infer Groovy class path because no repository is declared for the project.");
            }
            String notation = groovyJar.getDependencyNotation();
            List<Dependency> dependencies = Lists.newArrayList();
            // project.getDependencies().create(String) seems to be the only feasible way to create a Dependency with a classifier
            dependencies.add(project.getDependencies().create(notation));
            if (groovyJar.getVersion().getMajor() >= 2) {
                // add groovy-ant to bring in Groovydoc
                dependencies.add(project.getDependencies().create(notation.replace(":groovy:", ":groovy-ant:")));
            }
            return project.getConfigurations().detachedConfiguration(dependencies.toArray(new Dependency[0]));
        }

        // let's override this so that delegate isn't created at autowiring time (which would mean on every build)
        @Override
        public void visitDependencies(TaskDependencyResolveContext context) {
            if (classpath instanceof Buildable) {
                context.add(classpath);
            }
        }
    };
}
Also used : GroovyJarFile(org.gradle.api.internal.plugins.GroovyJarFile) TaskDependencyResolveContext(org.gradle.api.internal.tasks.TaskDependencyResolveContext) GradleException(org.gradle.api.GradleException) LazilyInitializedFileCollection(org.gradle.api.internal.file.collections.LazilyInitializedFileCollection) Dependency(org.gradle.api.artifacts.Dependency) Buildable(org.gradle.api.Buildable)

Aggregations

Buildable (org.gradle.api.Buildable)1 GradleException (org.gradle.api.GradleException)1 Dependency (org.gradle.api.artifacts.Dependency)1 LazilyInitializedFileCollection (org.gradle.api.internal.file.collections.LazilyInitializedFileCollection)1 GroovyJarFile (org.gradle.api.internal.plugins.GroovyJarFile)1 TaskDependencyResolveContext (org.gradle.api.internal.tasks.TaskDependencyResolveContext)1