Search in sources :

Example 1 with GradleConnector

use of org.gradle.tooling.GradleConnector in project android by JetBrains.

the class AndroidGradleTargetBuilder method doBuild.

private static void doBuild(@NotNull CompileContext context, @NotNull List<String> buildTasks, @NotNull BuilderExecutionSettings executionSettings, @Nullable String androidHome) throws ProjectBuildException {
    GradleConnector connector = getGradleConnector(executionSettings);
    ProjectConnection connection = connector.connect();
    ByteArrayOutputStream stdout = new ByteArrayOutputStream(BUFFER_SIZE);
    ByteArrayOutputStream stderr = new ByteArrayOutputStream(BUFFER_SIZE);
    try {
        BuildLauncher launcher = connection.newBuild();
        launcher.forTasks(toStringArray(buildTasks));
        List<String> jvmArgs = Lists.newArrayList();
        BuildMode buildMode = executionSettings.getBuildMode();
        if (BuildMode.ASSEMBLE_TRANSLATE == buildMode) {
            String arg = AndroidGradleSettings.createJvmArg(GradleBuilds.ENABLE_TRANSLATION_JVM_ARG, true);
            jvmArgs.add(arg);
        }
        if (androidHome != null && !androidHome.isEmpty()) {
            String androidSdkArg = AndroidGradleSettings.createAndroidHomeJvmArg(androidHome);
            jvmArgs.add(androidSdkArg);
        }
        jvmArgs.addAll(executionSettings.getJvmOptions());
        LOG.info("Build JVM args: " + jvmArgs);
        if (!jvmArgs.isEmpty()) {
            launcher.setJvmArguments(toStringArray(jvmArgs));
        }
        List<String> commandLineArgs = Lists.newArrayList();
        commandLineArgs.addAll(executionSettings.getCommandLineOptions());
        commandLineArgs.add(AndroidGradleSettings.createProjectProperty(AndroidProject.PROPERTY_INVOKED_FROM_IDE, true));
        if (executionSettings.isParallelBuild() && !commandLineArgs.contains(PARALLEL_BUILD_OPTION)) {
            commandLineArgs.add(PARALLEL_BUILD_OPTION);
        }
        if (executionSettings.isOfflineBuild() && !commandLineArgs.contains(OFFLINE_MODE_OPTION)) {
            commandLineArgs.add(OFFLINE_MODE_OPTION);
        }
        if (executionSettings.isConfigureOnDemand() && !commandLineArgs.contains(CONFIGURE_ON_DEMAND_OPTION)) {
            commandLineArgs.add(CONFIGURE_ON_DEMAND_OPTION);
        }
        LOG.info("Build command line args: " + commandLineArgs);
        if (!commandLineArgs.isEmpty()) {
            launcher.withArguments(toStringArray(commandLineArgs));
        }
        File javaHomeDir = executionSettings.getJavaHomeDir();
        if (javaHomeDir != null) {
            launcher.setJavaHome(javaHomeDir);
        }
        launcher.setStandardOutput(stdout);
        launcher.setStandardError(stderr);
        launcher.run();
    } catch (BuildException e) {
        handleBuildException(e, context, stderr.toString());
    } finally {
        String outText = stdout.toString();
        context.processMessage(new ProgressMessage(outText, 1.0f));
        try {
            Closeables.close(stdout, true);
            Closeables.close(stderr, true);
        } catch (IOException e) {
            LOG.debug(e);
        }
        connection.close();
    }
}
Also used : ProgressMessage(org.jetbrains.jps.incremental.messages.ProgressMessage) BuildLauncher(org.gradle.tooling.BuildLauncher) ProjectConnection(org.gradle.tooling.ProjectConnection) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ProjectBuildException(org.jetbrains.jps.incremental.ProjectBuildException) BuildException(org.gradle.tooling.BuildException) IOException(java.io.IOException) BuildMode(com.android.tools.idea.gradle.util.BuildMode) File(java.io.File) GradleConnector(org.gradle.tooling.GradleConnector) DefaultGradleConnector(org.gradle.tooling.internal.consumer.DefaultGradleConnector)

Example 2 with GradleConnector

use of org.gradle.tooling.GradleConnector 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 3 with GradleConnector

use of org.gradle.tooling.GradleConnector 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 4 with GradleConnector

use of org.gradle.tooling.GradleConnector 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 5 with GradleConnector

use of org.gradle.tooling.GradleConnector in project liferay-ide by liferay.

the class GradleTooling method getModel.

public static <T> T getModel(Class<T> modelClass, File cacheDir, File projectDir) throws Exception {
    T retval = null;
    GradleConnector connector = GradleConnector.newConnector().forProjectDirectory(projectDir);
    ProjectConnection connection = null;
    try {
        connection = connector.connect();
        ModelBuilder<T> modelBuilder = (ModelBuilder<T>) connection.model(modelClass);
        File depsDir = new File(cacheDir, "deps");
        depsDir.mkdirs();
        String path = depsDir.getAbsolutePath();
        path = path.replaceAll("\\\\", "/");
        _extractJar(depsDir, "com.liferay.blade.gradle.model");
        _extractJar(depsDir, "com.liferay.blade.gradle.plugin");
        String initScriptTemplate = CoreUtil.readStreamToString(GradleTooling.class.getResourceAsStream("init.gradle"));
        String initScriptContents = initScriptTemplate.replaceFirst("%deps%", path);
        File scriptFile = new File(cacheDir, "init.gradle");
        if (FileUtil.notExists(scriptFile)) {
            scriptFile.createNewFile();
        }
        FileUtil.writeFileFromStream(scriptFile, new ByteArrayInputStream(initScriptContents.getBytes()));
        ModelBuilder<T> builder = modelBuilder.withArguments("--init-script", scriptFile.getAbsolutePath());
        retval = builder.get();
    } catch (Exception e) {
        GradleCore.logError("get gradle custom model error", e);
    } finally {
        if (connection != null) {
            connection.close();
        }
    }
    return retval;
}
Also used : ModelBuilder(org.gradle.tooling.ModelBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) ProjectConnection(org.gradle.tooling.ProjectConnection) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException) GradleConnector(org.gradle.tooling.GradleConnector)

Aggregations

GradleConnector (org.gradle.tooling.GradleConnector)18 ProjectConnection (org.gradle.tooling.ProjectConnection)15 File (java.io.File)11 DefaultGradleConnector (org.gradle.tooling.internal.consumer.DefaultGradleConnector)10 BuildLauncher (org.gradle.tooling.BuildLauncher)4 IOException (java.io.IOException)3 AndroidProject (com.android.builder.model.AndroidProject)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 InputStream (java.io.InputStream)2 URI (java.net.URI)2 Matcher (java.util.regex.Matcher)2 BuildException (org.gradle.tooling.BuildException)2 GradleConnectionException (org.gradle.tooling.GradleConnectionException)2 GradleVersion (org.gradle.util.GradleVersion)2 ProjectImportAction (org.jetbrains.plugins.gradle.model.ProjectImportAction)2 Before (org.junit.Before)2 BuildMode (com.android.tools.idea.gradle.util.BuildMode)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1