Search in sources :

Example 1 with DefaultModuleRegistry

use of org.gradle.api.internal.classpath.DefaultModuleRegistry in project gradle by gradle.

the class DefaultDaemonStarter method startDaemon.

public DaemonStartupInfo startDaemon() {
    String daemonUid = UUID.randomUUID().toString();
    GradleInstallation gradleInstallation = CurrentGradleInstallation.get();
    ModuleRegistry registry = new DefaultModuleRegistry(gradleInstallation);
    ClassPath classpath;
    List<File> searchClassPath;
    if (gradleInstallation == null) {
        // When not running from a Gradle distro, need runtime impl for launcher plus the search path to look for other modules
        classpath = new DefaultClassPath();
        for (Module module : registry.getModule("gradle-launcher").getAllRequiredModules()) {
            classpath = classpath.plus(module.getClasspath());
        }
        searchClassPath = registry.getAdditionalClassPath().getAsFiles();
    } else {
        // When running from a Gradle distro, only need launcher jar. The daemon can find everything from there.
        classpath = registry.getModule("gradle-launcher").getImplementationClasspath();
        searchClassPath = Collections.emptyList();
    }
    if (classpath.isEmpty()) {
        throw new IllegalStateException("Unable to construct a bootstrap classpath when starting the daemon");
    }
    versionValidator.validate(daemonParameters);
    List<String> daemonArgs = new ArrayList<String>();
    daemonArgs.add(daemonParameters.getEffectiveJvm().getJavaExecutable().getAbsolutePath());
    List<String> daemonOpts = daemonParameters.getEffectiveJvmArgs();
    daemonArgs.addAll(daemonOpts);
    daemonArgs.add("-cp");
    daemonArgs.add(CollectionUtils.join(File.pathSeparator, classpath.getAsFiles()));
    if (Boolean.getBoolean("org.gradle.daemon.debug")) {
        daemonArgs.add("-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005");
    }
    LOGGER.debug("Using daemon args: {}", daemonArgs);
    daemonArgs.add(GradleDaemon.class.getName());
    // Version isn't used, except by a human looking at the output of jps.
    daemonArgs.add(GradleVersion.current().getVersion());
    // Serialize configuration to daemon via the process' stdin
    StreamByteBuffer buffer = new StreamByteBuffer();
    FlushableEncoder encoder = new KryoBackedEncoder(new EncodedStream.EncodedOutput(buffer.getOutputStream()));
    try {
        encoder.writeString(daemonParameters.getGradleUserHomeDir().getAbsolutePath());
        encoder.writeString(daemonDir.getBaseDir().getAbsolutePath());
        encoder.writeSmallInt(daemonParameters.getIdleTimeout());
        encoder.writeSmallInt(daemonParameters.getPeriodicCheckInterval());
        encoder.writeString(daemonUid);
        encoder.writeSmallInt(daemonOpts.size());
        for (String daemonOpt : daemonOpts) {
            encoder.writeString(daemonOpt);
        }
        encoder.writeSmallInt(searchClassPath.size());
        for (File file : searchClassPath) {
            encoder.writeString(file.getAbsolutePath());
        }
        encoder.flush();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
    InputStream stdInput = buffer.getInputStream();
    return startProcess(daemonArgs, daemonDir.getVersionedDir(), stdInput);
}
Also used : DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath) ClassPath(org.gradle.internal.classpath.ClassPath) FlushableEncoder(org.gradle.internal.serialize.FlushableEncoder) EncodedStream(org.gradle.process.internal.streams.EncodedStream) InputStream(java.io.InputStream) ModuleRegistry(org.gradle.api.internal.classpath.ModuleRegistry) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) ArrayList(java.util.ArrayList) StreamByteBuffer(org.gradle.internal.io.StreamByteBuffer) UncheckedIOException(org.gradle.api.UncheckedIOException) KryoBackedEncoder(org.gradle.internal.serialize.kryo.KryoBackedEncoder) UncheckedIOException(org.gradle.api.UncheckedIOException) IOException(java.io.IOException) CurrentGradleInstallation(org.gradle.internal.installation.CurrentGradleInstallation) GradleInstallation(org.gradle.internal.installation.GradleInstallation) DefaultModuleRegistry(org.gradle.api.internal.classpath.DefaultModuleRegistry) GradleDaemon(org.gradle.launcher.daemon.bootstrap.GradleDaemon) Module(org.gradle.api.internal.classpath.Module) File(java.io.File) DefaultClassPath(org.gradle.internal.classpath.DefaultClassPath)

Example 2 with DefaultModuleRegistry

use of org.gradle.api.internal.classpath.DefaultModuleRegistry 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)

Aggregations

DefaultModuleRegistry (org.gradle.api.internal.classpath.DefaultModuleRegistry)2 ClassPath (org.gradle.internal.classpath.ClassPath)2 File (java.io.File)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 UncheckedIOException (org.gradle.api.UncheckedIOException)1 ClassPathRegistry (org.gradle.api.internal.ClassPathRegistry)1 DefaultClassPathProvider (org.gradle.api.internal.DefaultClassPathProvider)1 DefaultClassPathRegistry (org.gradle.api.internal.DefaultClassPathRegistry)1 Module (org.gradle.api.internal.classpath.Module)1 ModuleRegistry (org.gradle.api.internal.classpath.ModuleRegistry)1 ClassLoaderFactory (org.gradle.internal.classloader.ClassLoaderFactory)1 DefaultClassLoaderFactory (org.gradle.internal.classloader.DefaultClassLoaderFactory)1 VisitableURLClassLoader (org.gradle.internal.classloader.VisitableURLClassLoader)1 DefaultClassPath (org.gradle.internal.classpath.DefaultClassPath)1 CurrentGradleInstallation (org.gradle.internal.installation.CurrentGradleInstallation)1 GradleInstallation (org.gradle.internal.installation.GradleInstallation)1 StreamByteBuffer (org.gradle.internal.io.StreamByteBuffer)1