Java Code Examples for org.gradle.tooling.ProjectConnection#close()

The following examples show how to use org.gradle.tooling.ProjectConnection#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GradleDaemon.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void doLoadDaemon() {
    GradleConnector gconn = GradleConnector.newConnector();
    ProjectConnection pconn = gconn.forProjectDirectory(LOADER_PROJECT_DIR).connect();
    BuildActionExecuter<String> action = pconn.action(DUMMY_ACTION);
    GradleCommandLine cmd = new GradleCommandLine();
    cmd.addSystemProperty(PROP_TOOLING_JAR, TOOLING_JAR);
    cmd.setFlag(GradleCommandLine.Flag.OFFLINE, true);
    cmd.addParameter(GradleCommandLine.Parameter.INIT_SCRIPT, INIT_SCRIPT);
    cmd.configure(action);
    try {
        action.run();
    } catch (GradleConnectionException | IllegalStateException ex) {
        // Well for some reason we were  not able to load Gradle.
        // Ignoring that for now
    } finally {
        pconn.close();
    }
}
 
Example 2
Source File: ToolingApiProjectGenerator.java    From gradle-initializr with Apache License 2.0 6 votes vote down vote up
@Override
public void generate(File targetDir, ProjectRequest projectRequest) {
    GradleConnector gradleConnector = GradleConnector.newConnector().forProjectDirectory(targetDir);

    if (projectRequest.getGradleVersion() != null) {
        gradleConnector.useGradleVersion(projectRequest.getGradleVersion());
    }

    ProjectConnection connection = gradleConnector.connect();

    try {
        connection.newBuild().forTasks(buildTasks(projectRequest)).run();
    } finally {
        connection.close();
    }
}
 
Example 3
Source File: GradleBuildComparison.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ProjectOutcomes executeBuild(ComparableGradleBuildExecuter executer) {
    ProjectConnection connection = createProjectConnection(executer);
    try {
        return executer.executeWith(connection);
    } finally {
        connection.close();
    }
}
 
Example 4
Source File: Main.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) {
    // Configure the connector and create the connection
    GradleConnector connector = GradleConnector.newConnector();

    if (args.length > 0) {
        connector.useInstallation(new File(args[0]));
        if (args.length > 1) {
            connector.useGradleUserHomeDir(new File(args[1]));
        }
    }

    connector.forProjectDirectory(new File("."));

    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks("help");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        launcher.setStandardOutput(outputStream);
        launcher.setStandardError(outputStream);

        // Run the build
        launcher.run();
    } finally {
        // Clean up
        connection.close();
    }
}
 
Example 5
Source File: GradleBuildComparison.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ProjectOutcomes executeBuild(ComparableGradleBuildExecuter executer) {
    ProjectConnection connection = createProjectConnection(executer);
    try {
        return executer.executeWith(connection);
    } finally {
        connection.close();
    }
}
 
Example 6
Source File: Main.java    From pushfish-android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) {
    // Configure the connector and create the connection
    GradleConnector connector = GradleConnector.newConnector();

    if (args.length > 0) {
        connector.useInstallation(new File(args[0]));
        if (args.length > 1) {
            connector.useGradleUserHomeDir(new File(args[1]));
        }
    }

    connector.forProjectDirectory(new File("."));

    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks("help");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        launcher.setStandardOutput(outputStream);
        launcher.setStandardError(outputStream);

        // Run the build
        launcher.run();
    } finally {
        // Clean up
        connection.close();
    }
}
 
Example 7
Source File: TemplateOperation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Set<FileObject> execute() {
    GradleConnector gconn = GradleConnector.newConnector();
    ProjectConnection pconn = gconn.forProjectDirectory(projectDir).connect();
    try {
        pconn.newBuild().withArguments("--offline").forTasks("wrapper").run(); //NOI18N
    } catch (GradleConnectionException | IllegalStateException ex) {
        // Well for some reason we were  not able to load Gradle.
        // Ignoring that for now
    } finally {
        pconn.close();
    }
    return null;
}
 
Example 8
Source File: CreateNewProject.java    From jacamo with GNU Lesser General Public License v3.0 5 votes vote down vote up
void runGradleWrapper() {
    ProjectConnection connection = GradleConnector
            .newConnector()
            .forProjectDirectory(path)
            .connect();
    connection.newBuild()
        .forTasks("wrapper")
        .run();
    connection.close();
}
 
Example 9
Source File: GradleBuildComparison.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ProjectOutcomes executeBuild(ComparableGradleBuildExecuter executer) {
    ProjectConnection connection = createProjectConnection(executer);
    try {
        return executer.executeWith(connection);
    } finally {
        connection.close();
    }
}
 
Example 10
Source File: Main.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) {
    // Configure the connector and create the connection
    GradleConnector connector = GradleConnector.newConnector();

    if (args.length > 0) {
        connector.useInstallation(new File(args[0]));
        if (args.length > 1) {
            connector.useGradleUserHomeDir(new File(args[1]));
        }
    }

    connector.forProjectDirectory(new File("."));

    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks("help");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        launcher.setStandardOutput(outputStream);
        launcher.setStandardError(outputStream);

        // Run the build
        launcher.run();
    } finally {
        // Clean up
        connection.close();
    }
}
 
Example 11
Source File: GradleBuildComparison.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private ProjectOutcomes executeBuild(ComparableGradleBuildExecuter executer) {
    ProjectConnection connection = createProjectConnection(executer);
    try {
        return executer.executeWith(connection);
    } finally {
        connection.close();
    }
}
 
Example 12
Source File: Main.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public static void main(String[] args) {
    // Configure the connector and create the connection
    GradleConnector connector = GradleConnector.newConnector();

    if (args.length > 0) {
        connector.useInstallation(new File(args[0]));
        if (args.length > 1) {
            connector.useGradleUserHomeDir(new File(args[1]));
        }
    }

    connector.forProjectDirectory(new File("."));

    ProjectConnection connection = connector.connect();
    try {
        // Configure the build
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks("help");
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        launcher.setStandardOutput(outputStream);
        launcher.setStandardError(outputStream);

        // Run the build
        launcher.run();
    } finally {
        // Clean up
        connection.close();
    }
}
 
Example 13
Source File: AbstractModelBuilderTest.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
@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);

  final 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);
  }

  final 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();

  GradleVersion _gradleVersion = GradleVersion.version(gradleVersion);
  final URI distributionUri = new DistributionLocator().getDistributionFor(_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 {
    boolean isGradleProjectDirSupported = _gradleVersion.compareTo(GradleVersion.version("2.4")) >= 0;
    boolean isCompositeBuildsSupported = isGradleProjectDirSupported && _gradleVersion.compareTo(GradleVersion.version("3.1")) >= 0;
    final ProjectImportAction projectImportAction = new ProjectImportAction(false, isGradleProjectDirSupported,
                                                                            isCompositeBuildsSupported);
    projectImportAction.addProjectImportExtraModelProvider(new ClassSetProjectImportExtraModelProvider(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", GradleConstants.INIT_SCRIPT_CMD_OPTION, initScript.getAbsolutePath());
    allModels = buildActionExecutor.run();
    assertNotNull(allModels);
  } finally {
    connection.close();
  }
}
 
Example 14
Source File: GradleBuild.java    From konduit-serving with Apache License 2.0 4 votes vote down vote up
public static void runGradleBuild(File directory, Config config) throws IOException {
    //Check for build.gradle.kts, properties
    //Check for gradlew/gradlew.bat
    File kts = new File(directory, "build.gradle.kts");
    if (!kts.exists()) {
        throw new IllegalStateException("build.gradle.kts doesn't exist");
    }
    File gradlew = new File(directory, "gradlew.bat");
    if (!gradlew.exists()) {
        throw new IllegalStateException("gradlew.bat doesn't exist");
    }

    //Execute gradlew
    ProjectConnection connection = GradleConnector.newConnector()
            .forProjectDirectory(directory)
            //.useGradleVersion("6.1")
            .connect();
    List<String> tasks = new ArrayList<>();
    tasks.add("wrapper");
    for(Deployment d : config.deployments()){
        for (String s : d.gradleTaskNames()) {
            if (!tasks.contains(s)) {
                tasks.add(s);
            }
        }
    }

    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        connection.newBuild().setStandardOutput(baos).setStandardError(System.err).forTasks(tasks.toArray(new String[0])).run();
        String output = baos.toString();
        Pattern pattern = Pattern.compile("(Successfully built )(\\w)+");
        Matcher matcher = pattern.matcher(output);
        String dockerId = StringUtils.EMPTY;
        while (matcher.find()){
            String[] words = matcher.group(0).split(" ");
            if (words.length >= 3) {
                dockerId = words[2];
            }
        }
        final String effDockerId = dockerId;
        System.out.println(output);
        if (StringUtils.isNotEmpty(dockerId)) {
            config.deployments().stream().forEach(
                    d -> {
                        if (d instanceof DockerDeployment)
                            ((DockerDeployment) d).imageId(effDockerId);
                    });
        }
    } finally {
        connection.close();
    }
}