use of org.gradle.internal.classpath.DefaultClassPath in project gradle by gradle.
the class BuildSrcUpdateFactory method create.
public DefaultClassPath create() {
File markerFile = new File(cache.getBaseDir(), "built.bin");
final boolean rebuild = !markerFile.exists();
Collection<File> classpath = build(rebuild);
LOGGER.debug("Gradle source classpath is: {}", classpath);
try {
markerFile.createNewFile();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return new DefaultClassPath(classpath);
}
use of org.gradle.internal.classpath.DefaultClassPath in project gradle by gradle.
the class PluginUnderTestMetadata method generate.
@TaskAction
public void generate() {
Properties properties = new Properties();
if (!getPluginClasspath().isEmpty()) {
List<String> paths = CollectionUtils.collect(getPluginClasspath(), new Transformer<String, File>() {
@Override
public String transform(File file) {
return file.getAbsolutePath().replaceAll("\\\\", "/");
}
});
StringBuilder implementationClasspath = new StringBuilder();
Joiner.on(File.pathSeparator).appendTo(implementationClasspath, paths);
properties.setProperty(IMPLEMENTATION_CLASSPATH_PROP_KEY, implementationClasspath.toString());
// As these files are inputs into this task, they have just been snapshotted by the task up-to-date checking.
// We should be reusing those persistent snapshots to avoid reading into memory again.
ClasspathHasher classpathHasher = getServices().get(ClasspathHasher.class);
String hash = classpathHasher.hash(new DefaultClassPath(getPluginClasspath())).toString();
properties.setProperty(IMPLEMENTATION_CLASSPATH_HASH_PROP_KEY, hash);
}
File outputFile = new File(getOutputDirectory(), METADATA_FILE_NAME);
try {
OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
GUtil.savePropertiesNoDateComment(properties, outputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
use of org.gradle.internal.classpath.DefaultClassPath in project gradle by gradle.
the class DefaultJdkToolsInitializer method initializeJdkTools.
public void initializeJdkTools() {
// Add in tools.jar to the systemClassloader parent
File toolsJar = Jvm.current().getToolsJar();
if (toolsJar != null) {
final ClassLoader systemClassLoaderParent = classLoaderFactory.getIsolatedSystemClassLoader();
ClasspathUtil.addUrl((URLClassLoader) systemClassLoaderParent, new DefaultClassPath(toolsJar).getAsURLs());
}
}
use of org.gradle.internal.classpath.DefaultClassPath 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);
}
use of org.gradle.internal.classpath.DefaultClassPath in project gradle by gradle.
the class ValidateTaskProperties method validateTaskClasses.
@TaskAction
public void validateTaskClasses() throws IOException {
ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
ClassPath classPath = new DefaultClassPath(Iterables.concat(Collections.singleton(getClassesDir()), getClasspath()));
ClassLoader classLoader = getClassLoaderFactory().createIsolatedClassLoader(classPath);
Thread.currentThread().setContextClassLoader(classLoader);
try {
validateTaskClasses(classLoader);
} finally {
Thread.currentThread().setContextClassLoader(previousContextClassLoader);
ClassLoaderUtils.tryClose(classLoader);
}
}
Aggregations