Search in sources :

Example 11 with ClassPath

use of org.gradle.internal.classpath.ClassPath in project gradle by gradle.

the class ProcessBootstrap method runNoExit.

private void runNoExit(String mainClassName, String[] args) throws Exception {
    ClassPathRegistry classPathRegistry = new DefaultClassPathRegistry(new DefaultClassPathProvider(new DefaultModuleRegistry(CurrentGradleInstallation.get())));
    ClassLoaderFactory classLoaderFactory = new DefaultClassLoaderFactory();
    ClassPath antClasspath = classPathRegistry.getClassPath("ANT");
    ClassPath runtimeClasspath = classPathRegistry.getClassPath("GRADLE_RUNTIME");
    ClassLoader antClassLoader = classLoaderFactory.createIsolatedClassLoader(antClasspath);
    ClassLoader runtimeClassLoader = new VisitableURLClassLoader(antClassLoader, runtimeClasspath);
    ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(runtimeClassLoader);
    try {
        Class<?> mainClass = runtimeClassLoader.loadClass(mainClassName);
        Object entryPoint = mainClass.newInstance();
        Method mainMethod = mainClass.getMethod("run", String[].class);
        mainMethod.invoke(entryPoint, new Object[] { args });
    } finally {
        Thread.currentThread().setContextClassLoader(oldClassLoader);
        ClassLoaderUtils.tryClose(runtimeClassLoader);
        ClassLoaderUtils.tryClose(antClassLoader);
    }
}
Also used : DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) ClassPath(org.gradle.internal.classpath.ClassPath) ClassLoaderFactory(org.gradle.internal.classloader.ClassLoaderFactory) DefaultClassLoaderFactory(org.gradle.internal.classloader.DefaultClassLoaderFactory) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader) DefaultClassPathProvider(org.gradle.api.internal.DefaultClassPathProvider) Method(java.lang.reflect.Method) ClassPathRegistry(org.gradle.api.internal.ClassPathRegistry) DefaultClassPathRegistry(org.gradle.api.internal.DefaultClassPathRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) DefaultClassPathRegistry(org.gradle.api.internal.DefaultClassPathRegistry) VisitableURLClassLoader(org.gradle.internal.classloader.VisitableURLClassLoader)

Example 12 with ClassPath

use of org.gradle.internal.classpath.ClassPath in project gradle by gradle.

the class CachingToolingImplementationLoader method create.

public ConsumerConnection create(Distribution distribution, ProgressLoggerFactory progressLoggerFactory, InternalBuildProgressListener progressListener, ConnectionParameters connectionParameters, BuildCancellationToken cancellationToken) {
    ClassPath classpath = distribution.getToolingImplementationClasspath(progressLoggerFactory, progressListener, connectionParameters.getGradleUserHomeDir(), cancellationToken);
    ConsumerConnection connection = connections.get(classpath);
    if (connection == null) {
        connection = loader.create(distribution, progressLoggerFactory, progressListener, connectionParameters, cancellationToken);
        connections.put(classpath, connection);
    }
    return connection;
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) ConsumerConnection(org.gradle.tooling.internal.consumer.connection.ConsumerConnection)

Example 13 with ClassPath

use of org.gradle.internal.classpath.ClassPath in project gradle by gradle.

the class BuildSourceBuilder method buildAndCreateClassLoader.

public ClassLoaderScope buildAndCreateClassLoader(GradleInternal gradle, File rootDir, StartParameter containingBuildParameters) {
    File buildSrcDir = new File(rootDir, DefaultSettings.DEFAULT_BUILD_SRC_DIR);
    ClassPath classpath = createBuildSourceClasspath(gradle, buildSrcDir, containingBuildParameters);
    return classLoaderScope.createChild(buildSrcDir.getAbsolutePath()).export(classpath).lock();
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) File(java.io.File)

Example 14 with ClassPath

use of org.gradle.internal.classpath.ClassPath in project gradle by gradle.

the class BuildSourceBuilder method createBuildSourceClasspath.

private ClassPath createBuildSourceClasspath(final GradleInternal gradle, File buildSrcDir, final StartParameter containingBuildParameters) {
    if (!buildSrcDir.isDirectory()) {
        LOGGER.debug("Gradle source dir does not exist. We leave.");
        return ClassPath.EMPTY;
    }
    final StartParameter buildSrcStartParameter = containingBuildParameters.newBuild();
    buildSrcStartParameter.setCurrentDir(buildSrcDir);
    buildSrcStartParameter.setProjectProperties(containingBuildParameters.getProjectProperties());
    buildSrcStartParameter.setSearchUpwards(false);
    buildSrcStartParameter.setProfile(containingBuildParameters.isProfile());
    final BuildDefinition buildDefinition = BuildDefinition.fromStartParameterForBuild(buildSrcStartParameter, buildSrcDir, DefaultPluginRequests.EMPTY);
    assert buildSrcStartParameter.getBuildFile() == null;
    return buildOperationExecutor.call(new CallableBuildOperation<ClassPath>() {

        @Override
        public ClassPath call(BuildOperationContext context) {
            ClassPath classPath = buildBuildSrc(buildDefinition);
            context.setResult(BUILD_BUILDSRC_RESULT);
            return classPath;
        }

        @Override
        public BuildOperationDescriptor.Builder description() {
            return BuildOperationDescriptor.displayName("Build buildSrc").progressDisplayName("Building buildSrc").details(new BuildBuildSrcBuildOperationType.Details() {

                @Override
                public String getBuildPath() {
                    return gradle.getIdentityPath().toString();
                }
            });
        }
    });
}
Also used : StartParameter(org.gradle.StartParameter) ClassPath(org.gradle.internal.classpath.ClassPath) BuildOperationContext(org.gradle.internal.operations.BuildOperationContext) BuildDefinition(org.gradle.api.internal.BuildDefinition)

Example 15 with ClassPath

use of org.gradle.internal.classpath.ClassPath in project gradle by gradle.

the class ApplicationClassesInSystemClassLoaderWorkerImplementationFactory method prepareJavaCommand.

@Override
public void prepareJavaCommand(Object workerId, String displayName, DefaultWorkerProcessBuilder processBuilder, List<URL> implementationClassPath, Address serverAddress, JavaExecHandleBuilder execSpec, boolean publishProcessInfo) {
    Collection<File> applicationClasspath = processBuilder.getApplicationClasspath();
    LogLevel logLevel = processBuilder.getLogLevel();
    Set<String> sharedPackages = processBuilder.getSharedPackages();
    Object requestedSecurityManager = execSpec.getSystemProperties().get("java.security.manager");
    ClassPath workerMainClassPath = classPathRegistry.getClassPath("WORKER_MAIN");
    execSpec.setMain("worker." + GradleWorkerMain.class.getName());
    boolean useOptionsFile = shouldUseOptionsFile(execSpec);
    if (useOptionsFile) {
        // Use an options file to pass across application classpath
        File optionsFile = temporaryFileProvider.createTemporaryFile("gradle-worker-classpath", "txt");
        List<String> jvmArgs = writeOptionsFile(workerMainClassPath.getAsFiles(), applicationClasspath, optionsFile);
        execSpec.jvmArgs(jvmArgs);
    } else {
        // Use a dummy security manager, which hacks the application classpath into the system ClassLoader
        execSpec.classpath(workerMainClassPath.getAsFiles());
        execSpec.systemProperty("java.security.manager", "worker." + BootstrapSecurityManager.class.getName());
    }
    // Serialize configuration for the worker process to it stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    try {
        DataOutputStream outstr = new DataOutputStream(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
        if (!useOptionsFile) {
            // Serialize the application classpath, this is consumed by BootstrapSecurityManager
            outstr.writeInt(applicationClasspath.size());
            for (File file : applicationClasspath) {
                outstr.writeUTF(file.getAbsolutePath());
            }
            // Serialize the actual security manager type, this is consumed by BootstrapSecurityManager
            outstr.writeUTF(requestedSecurityManager == null ? "" : requestedSecurityManager.toString());
        }
        // Serialize the shared packages, this is consumed by GradleWorkerMain
        outstr.writeInt(sharedPackages.size());
        for (String str : sharedPackages) {
            outstr.writeUTF(str);
        }
        // Serialize the worker implementation classpath, this is consumed by GradleWorkerMain
        outstr.writeInt(implementationClassPath.size());
        for (URL entry : implementationClassPath) {
            outstr.writeUTF(entry.toString());
        }
        // Serialize the worker config, this is consumed by SystemApplicationClassLoaderWorker
        OutputStreamBackedEncoder encoder = new OutputStreamBackedEncoder(outstr);
        encoder.writeSmallInt(logLevel.ordinal());
        encoder.writeBoolean(publishProcessInfo);
        encoder.writeString(gradleUserHomeDir.getAbsolutePath());
        new MultiChoiceAddressSerializer().write(encoder, (MultiChoiceAddress) serverAddress);
        // Serialize the worker, this is consumed by SystemApplicationClassLoaderWorker
        ActionExecutionWorker worker = new ActionExecutionWorker(processBuilder.getWorker(), workerId, displayName, gradleUserHomeDir);
        byte[] serializedWorker = GUtil.serialize(worker);
        encoder.writeBinary(serializedWorker);
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    execSpec.setStandardInput(buffer.getInputStream());
}
Also used : ClassPath(org.gradle.internal.classpath.ClassPath) EncodedStream(org.gradle.process.internal.streams.EncodedStream) DataOutputStream(java.io.DataOutputStream) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) LogLevel(org.gradle.api.logging.LogLevel) URL(java.net.URL) OutputStreamBackedEncoder(org.gradle.internal.serialize.OutputStreamBackedEncoder) File(java.io.File) MultiChoiceAddressSerializer(org.gradle.internal.remote.internal.inet.MultiChoiceAddressSerializer)

Aggregations

ClassPath (org.gradle.internal.classpath.ClassPath)18 DefaultClassPath (org.gradle.internal.classpath.DefaultClassPath)6 File (java.io.File)5 IOException (java.io.IOException)5 Module (org.gradle.api.internal.classpath.Module)4 Method (java.lang.reflect.Method)3 UncheckedIOException (org.gradle.api.UncheckedIOException)3 DefaultModuleRegistry (org.gradle.api.internal.classpath.DefaultModuleRegistry)3 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)3 EncodedStream (org.gradle.process.internal.streams.EncodedStream)3 InputStream (java.io.InputStream)2 InvocationHandler (java.lang.reflect.InvocationHandler)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 URLClassLoader (java.net.URLClassLoader)2 ArrayList (java.util.ArrayList)2 ModuleRegistry (org.gradle.api.internal.classpath.ModuleRegistry)2 ClassLoaderScope (org.gradle.api.internal.initialization.ClassLoaderScope)2 UncheckedException (org.gradle.internal.UncheckedException)2 VisitableURLClassLoader (org.gradle.internal.classloader.VisitableURLClassLoader)2 CurrentGradleInstallation (org.gradle.internal.installation.CurrentGradleInstallation)2