Search in sources :

Example 1 with SimpleFileCollection

use of org.gradle.api.internal.file.collections.SimpleFileCollection in project spring-boot by spring-projects.

the class BootRunTask method addResourcesIfNecessary.

private void addResourcesIfNecessary() {
    if (this.addResources) {
        SourceSet mainSourceSet = SourceSets.findMainSourceSet(getProject());
        final File outputDir = (mainSourceSet == null ? null : mainSourceSet.getOutput().getResourcesDir());
        final Set<File> resources = new LinkedHashSet<>();
        if (mainSourceSet != null) {
            resources.addAll(mainSourceSet.getResources().getSrcDirs());
        }
        List<File> classPath = new ArrayList<>(getClasspath().getFiles());
        classPath.addAll(0, resources);
        getLogger().info("Adding classpath: " + resources);
        setClasspath(new SimpleFileCollection(classPath));
        if (outputDir != null) {
            for (File directory : resources) {
                FileUtils.removeDuplicatesFromOutputDirectory(outputDir, directory);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SourceSet(org.gradle.api.tasks.SourceSet) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) ArrayList(java.util.ArrayList) File(java.io.File)

Example 2 with SimpleFileCollection

use of org.gradle.api.internal.file.collections.SimpleFileCollection in project gradle by gradle.

the class JavaInstallationProbe method getMetadataInternal.

private EnumMap<SysProp, String> getMetadataInternal(File jdkPath) {
    JavaExecAction exec = factory.newJavaExecAction();
    exec.executable(javaExe(jdkPath, "java"));
    File workingDir = Files.createTempDir();
    exec.setWorkingDir(workingDir);
    exec.setClasspath(new SimpleFileCollection(workingDir));
    try {
        writeProbe(workingDir);
        exec.setMain(JavaProbe.CLASSNAME);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        exec.setStandardOutput(baos);
        ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
        exec.setErrorOutput(errorOutput);
        exec.setIgnoreExitValue(true);
        ExecResult result = exec.execute();
        int exitValue = result.getExitValue();
        if (exitValue == 0) {
            return parseExecOutput(baos.toString());
        }
        return error("Command returned unexpected result code: " + exitValue + "\nError output:\n" + errorOutput);
    } catch (ExecException ex) {
        return error(ex.getMessage());
    } finally {
        try {
            FileUtils.deleteDirectory(workingDir);
        } catch (IOException e) {
            throw new GradleException("Unable to delete temp directory", e);
        }
    }
}
Also used : JavaExecAction(org.gradle.process.internal.JavaExecAction) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) ExecException(org.gradle.process.internal.ExecException) GradleException(org.gradle.api.GradleException) ExecResult(org.gradle.process.ExecResult)

Example 3 with SimpleFileCollection

use of org.gradle.api.internal.file.collections.SimpleFileCollection in project gradle by gradle.

the class DefaultClasspathHasher method hash.

@Override
public HashCode hash(ClassPath classpath) {
    FileCollectionSnapshot snapshot = snapshotter.snapshot(new SimpleFileCollection(classpath.getAsFiles()), TaskFilePropertyCompareStrategy.ORDERED, ClasspathSnapshotNormalizationStrategy.INSTANCE);
    BuildCacheHasher hasher = new DefaultBuildCacheHasher();
    snapshot.appendToHasher(hasher);
    return hasher.hash();
}
Also used : SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) FileCollectionSnapshot(org.gradle.api.internal.changedetection.state.FileCollectionSnapshot) DefaultBuildCacheHasher(org.gradle.caching.internal.DefaultBuildCacheHasher) BuildCacheHasher(org.gradle.caching.internal.BuildCacheHasher) DefaultBuildCacheHasher(org.gradle.caching.internal.DefaultBuildCacheHasher)

Example 4 with SimpleFileCollection

use of org.gradle.api.internal.file.collections.SimpleFileCollection in project gradle by gradle.

the class DefaultVariantTransformRegistration method transform.

@Override
public List<File> transform(File input) {
    try {
        File absoluteFile = input.getAbsoluteFile();
        // Collect up hash of the input files, and of the transform's configuration params and implementation
        FileCollectionSnapshot snapshot = fileCollectionSnapshotter.snapshot(new SimpleFileCollection(absoluteFile), TaskFilePropertyCompareStrategy.UNORDERED, TaskFilePropertySnapshotNormalizationStrategy.ABSOLUTE);
        DefaultBuildCacheHasher hasher = new DefaultBuildCacheHasher();
        hasher.putBytes(inputsHash.asBytes());
        snapshot.appendToHasher(hasher);
        HashCode resultHash = hasher.hash();
        return transformedFileCache.getResult(absoluteFile, resultHash, transformer);
    } catch (Exception e) {
        throw new ArtifactTransformException(input, to, implementation, e);
    }
}
Also used : HashCode(com.google.common.hash.HashCode) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) FileCollectionSnapshot(org.gradle.api.internal.changedetection.state.FileCollectionSnapshot) DefaultBuildCacheHasher(org.gradle.caching.internal.DefaultBuildCacheHasher) ArtifactTransformException(org.gradle.api.artifacts.transform.ArtifactTransformException) File(java.io.File) ArtifactTransformException(org.gradle.api.artifacts.transform.ArtifactTransformException) VariantTransformConfigurationException(org.gradle.api.artifacts.transform.VariantTransformConfigurationException)

Example 5 with SimpleFileCollection

use of org.gradle.api.internal.file.collections.SimpleFileCollection in project gradle by gradle.

the class ApiGroovyCompiler method execute.

@Override
public WorkResult execute(final GroovyJavaJointCompileSpec spec) {
    GroovySystemLoaderFactory groovySystemLoaderFactory = new GroovySystemLoaderFactory();
    ClassLoader compilerClassLoader = this.getClass().getClassLoader();
    GroovySystemLoader compilerGroovyLoader = groovySystemLoaderFactory.forClassLoader(compilerClassLoader);
    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.setVerbose(spec.getGroovyCompileOptions().isVerbose());
    configuration.setSourceEncoding(spec.getGroovyCompileOptions().getEncoding());
    configuration.setTargetBytecode(spec.getTargetCompatibility());
    configuration.setTargetDirectory(spec.getDestinationDir());
    canonicalizeValues(spec.getGroovyCompileOptions().getOptimizationOptions());
    if (spec.getGroovyCompileOptions().getConfigurationScript() != null) {
        applyConfigurationScript(spec.getGroovyCompileOptions().getConfigurationScript(), configuration);
    }
    try {
        configuration.setOptimizationOptions(spec.getGroovyCompileOptions().getOptimizationOptions());
    } catch (NoSuchMethodError ignored) {
    /* method was only introduced in Groovy 1.8 */
    }
    Map<String, Object> jointCompilationOptions = new HashMap<String, Object>();
    final File stubDir = spec.getGroovyCompileOptions().getStubDir();
    stubDir.mkdirs();
    jointCompilationOptions.put("stubDir", stubDir);
    jointCompilationOptions.put("keepStubs", spec.getGroovyCompileOptions().isKeepStubs());
    configuration.setJointCompilationOptions(jointCompilationOptions);
    ClassLoader classPathLoader;
    VersionNumber version = parseGroovyVersion();
    if (version.compareTo(VersionNumber.parse("2.0")) < 0) {
        // using a transforming classloader is only required for older buggy Groovy versions
        classPathLoader = new GroovyCompileTransformingClassLoader(getExtClassLoader(), new DefaultClassPath(spec.getCompileClasspath()));
    } else {
        classPathLoader = new DefaultClassLoaderFactory().createIsolatedClassLoader(new DefaultClassPath(spec.getCompileClasspath()));
    }
    GroovyClassLoader compileClasspathClassLoader = new GroovyClassLoader(classPathLoader, null);
    GroovySystemLoader compileClasspathLoader = groovySystemLoaderFactory.forClassLoader(classPathLoader);
    FilteringClassLoader.Spec groovyCompilerClassLoaderSpec = new FilteringClassLoader.Spec();
    groovyCompilerClassLoaderSpec.allowPackage("org.codehaus.groovy");
    groovyCompilerClassLoaderSpec.allowPackage("groovy");
    // Disallow classes from Groovy Jar that reference external classes. Such classes must be loaded from astTransformClassLoader,
    // or a NoClassDefFoundError will occur. Essentially this is drawing a line between the Groovy compiler and the Groovy
    // library, albeit only for selected classes that run a high risk of being statically referenced from a transform.
    groovyCompilerClassLoaderSpec.disallowClass("groovy.util.GroovyTestCase");
    groovyCompilerClassLoaderSpec.disallowPackage("groovy.servlet");
    FilteringClassLoader groovyCompilerClassLoader = new FilteringClassLoader(GroovyClassLoader.class.getClassLoader(), groovyCompilerClassLoaderSpec);
    // AST transforms need their own class loader that shares compiler classes with the compiler itself
    final GroovyClassLoader astTransformClassLoader = new GroovyClassLoader(groovyCompilerClassLoader, null);
    // where the transform class is loaded from)
    for (File file : spec.getCompileClasspath()) {
        astTransformClassLoader.addClasspath(file.getPath());
    }
    JavaAwareCompilationUnit unit = new JavaAwareCompilationUnit(configuration, compileClasspathClassLoader) {

        @Override
        public GroovyClassLoader getTransformLoader() {
            return astTransformClassLoader;
        }
    };
    final boolean shouldProcessAnnotations = shouldProcessAnnotations(spec);
    if (shouldProcessAnnotations) {
        // If an annotation processor is detected, we need to force Java stub generation, so the we can process annotations on Groovy classes
        // We are forcing stub generation by tricking the groovy compiler into thinking there are java files to compile.
        // All java files are just passed to the compile method of the JavaCompiler and aren't processed internally by the Groovy Compiler.
        // Since we're maintaining our own list of Java files independent of what's passed by the Groovy compiler, adding a non-existent java file
        // to the sources won't cause any issues.
        unit.addSources(new File[] { new File("ForceStubGeneration.java") });
    }
    // Sort source files to work around https://issues.apache.org/jira/browse/GROOVY-7966
    File[] sortedSourceFiles = Iterables.toArray(spec.getSource(), File.class);
    Arrays.sort(sortedSourceFiles);
    unit.addSources(sortedSourceFiles);
    unit.setCompilerFactory(new JavaCompilerFactory() {

        public JavaCompiler createCompiler(final CompilerConfiguration config) {
            return new JavaCompiler() {

                public void compile(List<String> files, CompilationUnit cu) {
                    if (shouldProcessAnnotations) {
                        // In order for the Groovy stubs to have annotation processors invoked against them, they must be compiled as source.
                        // Classes compiled as a result of being on the -sourcepath do not have the annotation processor run against them
                        spec.setSource(spec.getSource().plus(new SimpleFileCollection(stubDir).getAsFileTree()));
                    } else {
                        // When annotation processing isn't required, it's better to add the Groovy stubs as part of the source path.
                        // This allows compilations to complete faster, because only the Groovy stubs that are needed by the java source are compiled.
                        FileCollection sourcepath = new SimpleFileCollection(stubDir);
                        if (spec.getCompileOptions().getSourcepath() != null) {
                            sourcepath = spec.getCompileOptions().getSourcepath().plus(sourcepath);
                        }
                        spec.getCompileOptions().setSourcepath(sourcepath);
                    }
                    spec.setSource(spec.getSource().filter(new Spec<File>() {

                        public boolean isSatisfiedBy(File file) {
                            return hasExtension(file, ".java");
                        }
                    }));
                    try {
                        javaCompiler.execute(spec);
                    } catch (CompilationFailedException e) {
                        cu.getErrorCollector().addFatalError(new SimpleMessage(e.getMessage(), cu));
                    }
                }
            };
        }
    });
    try {
        unit.compile();
    } catch (org.codehaus.groovy.control.CompilationFailedException e) {
        System.err.println(e.getMessage());
        // Explicit flush, System.err is an auto-flushing PrintWriter unless it is replaced.
        System.err.flush();
        throw new CompilationFailedException();
    } finally {
        // Remove compile and AST types from the Groovy loader
        compilerGroovyLoader.discardTypesFrom(classPathLoader);
        compilerGroovyLoader.discardTypesFrom(astTransformClassLoader);
        //Discard the compile loader
        compileClasspathLoader.shutdown();
    }
    return new SimpleWorkResult(true);
}
Also used : HashMap(java.util.HashMap) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) FileCollection(org.gradle.api.file.FileCollection) SimpleWorkResult(org.gradle.api.internal.tasks.SimpleWorkResult) GroovyClassLoader(groovy.lang.GroovyClassLoader) SimpleFileCollection(org.gradle.api.internal.file.collections.SimpleFileCollection) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) GroovyClassLoader(groovy.lang.GroovyClassLoader) GroovySystemLoader(org.gradle.api.internal.classloading.GroovySystemLoader) GroovySystemLoaderFactory(org.gradle.api.internal.classloading.GroovySystemLoaderFactory) DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) JavaAwareCompilationUnit(org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) JavaCompiler(org.codehaus.groovy.tools.javac.JavaCompiler) JavaCompilerFactory(org.codehaus.groovy.tools.javac.JavaCompilerFactory) VersionNumber(org.gradle.util.VersionNumber) FilteringClassLoader(org.gradle.internal.classloader.FilteringClassLoader) Spec(org.gradle.api.specs.Spec) File(java.io.File) DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath)

Aggregations

SimpleFileCollection (org.gradle.api.internal.file.collections.SimpleFileCollection)9 File (java.io.File)7 FileCollection (org.gradle.api.file.FileCollection)4 FileCollectionSnapshot (org.gradle.api.internal.changedetection.state.FileCollectionSnapshot)2 PatternSet (org.gradle.api.tasks.util.PatternSet)2 DefaultBuildCacheHasher (org.gradle.caching.internal.DefaultBuildCacheHasher)2 HashCode (com.google.common.hash.HashCode)1 GroovyClassLoader (groovy.lang.GroovyClassLoader)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)1 JavaAwareCompilationUnit (org.codehaus.groovy.tools.javac.JavaAwareCompilationUnit)1 JavaCompiler (org.codehaus.groovy.tools.javac.JavaCompiler)1 JavaCompilerFactory (org.codehaus.groovy.tools.javac.JavaCompilerFactory)1 GradleException (org.gradle.api.GradleException)1 ArtifactTransformException (org.gradle.api.artifacts.transform.ArtifactTransformException)1 VariantTransformConfigurationException (org.gradle.api.artifacts.transform.VariantTransformConfigurationException)1