Search in sources :

Example 1 with DefaultGradleConnector

use of org.gradle.tooling.internal.consumer.DefaultGradleConnector in project spring-boot by spring-projects.

the class ProjectCreator method createProject.

public ProjectConnection createProject(String name) throws IOException {
    File projectDirectory = new File("target/" + name);
    projectDirectory.mkdirs();
    File gradleScript = new File(projectDirectory, "build.gradle");
    if (new File("src/test/resources", name).isDirectory()) {
        FileSystemUtils.copyRecursively(new File("src/test/resources", name), projectDirectory);
    } else {
        FileCopyUtils.copy(new File("src/test/resources/" + name + ".gradle"), gradleScript);
    }
    GradleConnector gradleConnector = GradleConnector.newConnector();
    gradleConnector.useGradleVersion(this.gradleVersion);
    ((DefaultGradleConnector) gradleConnector).embedded(true);
    return gradleConnector.forProjectDirectory(projectDirectory).connect();
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) File(java.io.File) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) GradleConnector(org.gradle.tooling.GradleConnector)

Example 2 with DefaultGradleConnector

use of org.gradle.tooling.internal.consumer.DefaultGradleConnector in project gradle by gradle.

the class Main method fetch.

private static void fetch(File buildDir, File gradleInstallDir, boolean embedded) {
    System.out.println("* Fetching model for " + buildDir);
    System.out.println("* Using tooling API " + GradleVersion.current().getVersion());
    Timer timer = new Timer();
    GradleConnector gradleConnector = GradleConnector.newConnector();
    gradleConnector.forProjectDirectory(buildDir);
    ((DefaultGradleConnector) gradleConnector).embedded(embedded);
    if (gradleInstallDir != null) {
        gradleConnector.useInstallation(gradleInstallDir);
    }
    ProjectConnection connect = gradleConnector.connect();
    try {
        for (int i = 0; i < 5; i++) {
            SyncAction.withProjectConnection(connect, null);
        }
    } finally {
        connect.close();
    }
    timer.stop();
    System.out.println("total time: " + timer.duration());
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) ProjectConnection(org.gradle.tooling.ProjectConnection) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) GradleConnector(org.gradle.tooling.GradleConnector)

Example 3 with DefaultGradleConnector

use of org.gradle.tooling.internal.consumer.DefaultGradleConnector in project gradle by gradle.

the class ToolingApiGradleExecutor method buildConnector.

private GradleConnector buildConnector(File gradleUserHome, File projectDir, boolean embedded, GradleProvider gradleProvider) {
    DefaultGradleConnector gradleConnector = (DefaultGradleConnector) GradleConnector.newConnector();
    gradleConnector.useDistributionBaseDir(GradleUserHomeLookup.gradleUserHome());
    gradleProvider.applyTo(gradleConnector);
    gradleConnector.useGradleUserHomeDir(gradleUserHome);
    gradleConnector.daemonBaseDir(new File(gradleUserHome, TEST_KIT_DAEMON_DIR_NAME));
    gradleConnector.forProjectDirectory(projectDir);
    gradleConnector.searchUpwards(false);
    gradleConnector.daemonMaxIdleTime(120, TimeUnit.SECONDS);
    gradleConnector.embedded(embedded);
    return gradleConnector;
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) File(java.io.File)

Example 4 with DefaultGradleConnector

use of org.gradle.tooling.internal.consumer.DefaultGradleConnector in project kotlin by JetBrains.

the class AbstractModelBuilderTest method setUp.

@Before
public void setUp() throws Exception {
    assumeThat(gradleVersion, versionMatcherRule.getMatcher());
    ensureTempDirCreated();
    String methodName = name.getMethodName();
    Matcher m = TEST_METHOD_NAME_PATTERN.matcher(methodName);
    if (m.matches()) {
        methodName = m.group(1);
    }
    testDir = new File(ourTempDir, methodName);
    FileUtil.ensureExists(testDir);
    InputStream buildScriptStream = getClass().getResourceAsStream("/" + methodName + "/" + GradleConstants.DEFAULT_SCRIPT_NAME);
    try {
        FileUtil.writeToFile(new File(testDir, GradleConstants.DEFAULT_SCRIPT_NAME), FileUtil.loadTextAndClose(buildScriptStream));
    } finally {
        StreamUtil.closeStream(buildScriptStream);
    }
    InputStream settingsStream = getClass().getResourceAsStream("/" + methodName + "/" + GradleConstants.SETTINGS_FILE_NAME);
    try {
        if (settingsStream != null) {
            FileUtil.writeToFile(new File(testDir, GradleConstants.SETTINGS_FILE_NAME), FileUtil.loadTextAndClose(settingsStream));
        }
    } finally {
        StreamUtil.closeStream(settingsStream);
    }
    GradleConnector connector = GradleConnector.newConnector();
    URI distributionUri = new DistributionLocator().getDistributionFor(GradleVersion.version(gradleVersion));
    connector.useDistribution(distributionUri);
    connector.forProjectDirectory(testDir);
    int daemonMaxIdleTime = 10;
    try {
        daemonMaxIdleTime = Integer.parseInt(System.getProperty("gradleDaemonMaxIdleTime", "10"));
    } catch (NumberFormatException ignore) {
    }
    ((DefaultGradleConnector) connector).daemonMaxIdleTime(daemonMaxIdleTime, TimeUnit.SECONDS);
    ProjectConnection connection = connector.connect();
    try {
        ProjectImportAction projectImportAction = new ProjectImportAction(false);
        projectImportAction.addExtraProjectModelClasses(getModels());
        BuildActionExecuter<ProjectImportAction.AllModels> buildActionExecutor = connection.action(projectImportAction);
        File initScript = GradleExecutionHelper.generateInitScript(false, getToolingExtensionClasses());
        assertNotNull(initScript);
        String jdkHome = IdeaTestUtil.requireRealJdkHome();
        buildActionExecutor.setJavaHome(new File(jdkHome));
        buildActionExecutor.setJvmArguments("-Xmx128m", "-XX:MaxPermSize=64m");
        buildActionExecutor.withArguments("--info", "--recompile-scripts", GradleConstants.INIT_SCRIPT_CMD_OPTION, initScript.getAbsolutePath());
        allModels = buildActionExecutor.run();
        assertNotNull(allModels);
    } finally {
        connection.close();
    }
}
Also used : Matcher(java.util.regex.Matcher) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) InputStream(java.io.InputStream) ProjectConnection(org.gradle.tooling.ProjectConnection) URI(java.net.URI) ProjectImportAction(org.jetbrains.plugins.gradle.model.ProjectImportAction) GradleConnector(org.gradle.tooling.GradleConnector) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) File(java.io.File) Before(org.junit.Before)

Example 5 with DefaultGradleConnector

use of org.gradle.tooling.internal.consumer.DefaultGradleConnector in project intellij-community by JetBrains.

the class GradleExecutionHelper method getConnection.

/**
   * Allows to retrieve gradle api connection to use for the given project.
   *
   * @param projectPath target project path
   * @param settings    execution settings to use
   * @return connection to use
   * @throws IllegalStateException if it's not possible to create the connection
   */
@NotNull
private static ProjectConnection getConnection(@NotNull String projectPath, @Nullable GradleExecutionSettings settings) throws IllegalStateException {
    File projectDir = new File(projectPath);
    GradleConnector connector = GradleConnector.newConnector();
    int ttl = -1;
    if (settings != null) {
        //noinspection EnumSwitchStatementWhichMissesCases
        switch(settings.getDistributionType()) {
            case LOCAL:
                String gradleHome = settings.getGradleHome();
                if (gradleHome != null) {
                    try {
                        // There were problems with symbolic links processing at the gradle side.
                        connector.useInstallation(new File(gradleHome).getCanonicalFile());
                    } catch (IOException e) {
                        connector.useInstallation(new File(settings.getGradleHome()));
                    }
                }
                break;
            case WRAPPED:
                if (settings.getWrapperPropertyFile() != null) {
                    File propertiesFile = new File(settings.getWrapperPropertyFile());
                    if (propertiesFile.exists()) {
                        Distribution distribution = new DistributionFactoryExt(new DefaultExecutorServiceFactory()).getWrappedDistribution(propertiesFile);
                        try {
                            setField(connector, "distribution", distribution);
                        } catch (Exception e) {
                            throw new ExternalSystemException(e);
                        }
                    }
                }
                break;
        }
        // Setup service directory if necessary.
        String serviceDirectory = settings.getServiceDirectory();
        if (serviceDirectory != null) {
            connector.useGradleUserHomeDir(new File(serviceDirectory));
        }
        // Setup logging if necessary.
        if (settings.isVerboseProcessing() && connector instanceof DefaultGradleConnector) {
            ((DefaultGradleConnector) connector).setVerboseLogging(true);
        }
        ttl = (int) settings.getRemoteProcessIdleTtlInMs();
    }
    if (ttl > 0 && connector instanceof DefaultGradleConnector) {
        // do not spawn gradle daemons during test execution
        final Application app = ApplicationManager.getApplication();
        ttl = (app != null && app.isUnitTestMode()) ? 10000 : ttl;
        ((DefaultGradleConnector) connector).daemonMaxIdleTime(ttl, TimeUnit.MILLISECONDS);
    }
    connector.forProjectDirectory(projectDir);
    ProjectConnection connection = connector.connect();
    if (connection == null) {
        throw new IllegalStateException(String.format("Can't create connection to the target project via gradle tooling api. Project path: '%s'", projectPath));
    }
    return connection;
}
Also used : DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) IOException(java.io.IOException) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) IOException(java.io.IOException) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector) ExternalSystemException(com.intellij.openapi.externalSystem.model.ExternalSystemException) Distribution(org.gradle.tooling.internal.consumer.Distribution) DefaultExecutorServiceFactory(org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory) File(java.io.File) DistributionFactoryExt(org.jetbrains.plugins.gradle.service.project.DistributionFactoryExt) Application(com.intellij.openapi.application.Application) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DefaultGradleConnector (org.gradle.tooling.internal.consumer.DefaultGradleConnector)8 File (java.io.File)7 GradleConnector (org.gradle.tooling.GradleConnector)6 ProjectConnection (org.gradle.tooling.ProjectConnection)4 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Matcher (java.util.regex.Matcher)2 NotNull (org.jetbrains.annotations.NotNull)2 ProjectImportAction (org.jetbrains.plugins.gradle.model.ProjectImportAction)2 Before (org.junit.Before)2 Application (com.intellij.openapi.application.Application)1 ExternalSystemException (com.intellij.openapi.externalSystem.model.ExternalSystemException)1 IOException (java.io.IOException)1 DefaultExecutorServiceFactory (org.gradle.tooling.internal.consumer.DefaultExecutorServiceFactory)1 Distribution (org.gradle.tooling.internal.consumer.Distribution)1 GradleVersion (org.gradle.util.GradleVersion)1 DistributionFactoryExt (org.jetbrains.plugins.gradle.service.project.DistributionFactoryExt)1