private void runTask(final ProjectConnection connection, final Path path, final String task) { synchronized (LOCK) { Map<String, String> xml = new HashMap<>(); try { // clean jaxp XML_PROPS.forEach(p -> xml.put(p, (String) System.getProperties().remove(p))); try { connection.newBuild() .setStandardError(System.err) .setStandardOutput(System.out) .forTasks(task) .run(); } catch (Exception ex) { getLogger().debug("Execution of " + task + " resulted in exception", ex); } } finally { // restore jaxp xml.forEach((k, v) -> { if (v != null) { System.setProperty(k, v); } }); } } }
@Override protected void executeLaunch(BuildLauncher launcher) { launcher.run(); }
private void build() { while (that.lastBuild < that.lastChange) { that.lastBuild = System.currentTimeMillis(); try { // Errors must be absorbed else the watcher will crap itself. builder.run(); } catch (Throwable e) { e.printStackTrace(); } } } };
@Override public Void execute(File directory, ProjectConnection connection) { connection.newBuild() .withArguments(arguments.toArray(new String[arguments.size()])) .run(); return null; } });
/** * Runs tasks in the project. * @param tasks the tasks * @return this */ public GradleAdapter launch(String... tasks) { if (LOG.isInfoEnabled()) { LOG.info("Gradle tasks: {}", Arrays.toString(tasks)); } configure(connection.newBuild().forTasks(tasks)).run(); return this; }
@Override public Void execute(File projectDirectory, ProjectConnection connection) { connection.newBuild() .forTasks("wrapper") .run(); return null; } });
private void runPrepareCompileTask() throws IOException { if (!this.prepareCompileTask.isEmpty()) { final ProjectConnection connection = this.getProjectConnection(); try { final String[] tasks = prepareCompileTask.toArray(new String[0]); final BuildLauncher buildLauncher = connection.newBuild(); log.info("project {} run tasks:{}", this.name, tasks); GradleProject.setBuildJVMArgs(buildLauncher); buildLauncher.forTasks(tasks).run(); } finally { connection.close(); } } }
private void runPrepareTestCompileTask() throws IOException { if (!this.prepareTestCompileTask.isEmpty()) { final ProjectConnection connection = this.getProjectConnection(); try { final String[] tasks = prepareTestCompileTask.toArray(new String[0]); final BuildLauncher buildLauncher = connection.newBuild(); log.info("project {} run tasks:{}", this.name, tasks); GradleProject.setBuildJVMArgs(buildLauncher); buildLauncher.forTasks(tasks).run(); } finally { connection.close(); } } }
@Override public Void execute(File projectDirectory, ProjectConnection connection) { connection.newBuild() .forTasks("doNothing") .withArguments("--refresh-dependencies") .run(); return null; } });
public boolean build(String ...tasks) { ProjectConnection connection = connector.connect(); BuildLauncher build = connection.newBuild(); if (tasks.length > 0) { build.forTasks(tasks); } try { build.run(); }finally { connection.close(); } return true; }
protected byte[] runTasks(String... tasks) { ProjectConnection connection = GradleConnector.newConnector().forProjectDirectory(new File(InputConfiguration.get().getAbsolutePathToProjectRoot())).connect(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); LOGGER.info("Run gradle tasks: {}", String.join(" ", tasks)); try { BuildLauncher build = connection.newBuild(); build.forTasks(tasks); build.setStandardOutput(outputStream); build.setStandardError(outputStream); build.run(); } catch (Exception e) { throw new RuntimeException(e); } finally { connection.close(); } return outputStream.toByteArray(); }
private static void initProjectIfNotExists(BuildConfiguration buildConfig, CancellationTokenSource tokenSource, IProgressMonitor monitor) { File projectDir = buildConfig.getRootProjectDirectory().getAbsoluteFile(); if (!projectDir.exists()) { if (projectDir.mkdir()) { List<String> tasks = ImmutableList.of("init", "--type", "java-library"); InternalGradleBuild gradleBuild = CorePlugin.internalGradleWorkspace().getGradleBuild(buildConfig); RunConfiguration runConfiguration = CorePlugin.configurationManager().createDefaultRunConfiguration(buildConfig); GradleProgressAttributes progressAttributes = GradleProgressAttributes.builder(tokenSource, monitor) .forNonInteractiveBackgroundProcess() .withFilteredProgress() .build(); gradleBuild.newBuildLauncher(runConfiguration, progressAttributes).forTasks(tasks.toArray(new String[tasks.size()])).run(); } } }
public void run(final File projectFolder, final ResultHandler<? super Void> resultHandler, final Consumer<BuildLauncher> configClosure) { final ProjectConnection connector = GradleConnector.newConnector().useGradleVersion(GradleHelper.GRADLE_VERSION).forProjectDirectory(projectFolder).connect(); try { final BuildLauncher build = connector.newBuild(); final ProgressListener _function = (ProgressEvent event) -> { GradleHelper.logger.debug("Gradle build event=\'{}\'", event.getDisplayName()); }; build.addProgressListener(_function, OperationType.values()); configClosure.accept(build); if ((resultHandler != null)) { build.run(resultHandler); } else { build.run(); } } finally { connector.close(); } }
try { BuildLauncher build = connection.newBuild(); //select tasks to run: build.forTasks("clean", "test"); //include some build arguments: build.withArguments("--no-search-upward", "-i", "--project-dir", "someProjectDir"); //configure the standard input: build.setStandardInput(new ByteArrayInputStream("consume this!".getBytes())); //in case you want the build to use java different than default: build.setJavaHome(new File("/path/to/java")); //if your build needs crazy amounts of memory: build.setJvmArguments("-Xmx2048m", "-XX:MaxPermSize=512m"); //if you want to listen to the progress events: ProgressListener listener = null; // use your implementation build.addProgressListener(listener); //kick the build off: build.run(); } finally { connection.close(); }
@Override public Assignable importBuildOutput() { final String oldValue = removeSAXParserFactoryProperty(); getBuildLauncher().forTasks(tasks).withArguments(arguments).run(); setSAXParserFactoryProperty(oldValue); return this; }
@Override public Assignable importBuildOutput() { final String oldValue = removeSAXParserFactoryProperty(); getBuildLauncher().forTasks(tasks).withArguments(arguments).run(); setSAXParserFactoryProperty(oldValue); return this; }
private void runTasks(final List<String> tasksToRun, IProgressMonitor monitor, CancellationTokenSource tokenSource) { RunConfiguration runConfiguration = CorePlugin.configurationManager().createDefaultRunConfiguration(this.buildConfig); GradleProgressAttributes progressAttributes = GradleProgressAttributes.builder(tokenSource, monitor) .forBackgroundProcess() .withFilteredProgress() .build(); BuildLauncher launcher = CorePlugin.internalGradleWorkspace().getGradleBuild(this.buildConfig).newBuildLauncher(runConfiguration, progressAttributes); launcher.forTasks(tasksToRun.toArray(new String[tasksToRun.size()])).run(); } }
void prepareCompileAndroidJava() { ProjectConnection connection = this.project.getProjectConnection(); try { BuildLauncher buildLauncher = connection.newBuild(); String genTask = this.project.getName() + this.genSourceTaskName; buildLauncher.forTasks(genTask).run(); int size = this.project.getDependencies().size(); String aar = Joiner.on(File.separator) .join(this.project.getProjectRoot(), BUILD_DIR, INTERMEDIATE_DIR, EXPLODED_DIR); List<File> jars = FileUtils.collectFiles(new File(aar), EXT_JAR); for (File jar : jars) { addAAR(jar); } int after = this.project.getDependencies().size(); if (size != after) { CachedASMReflector.getInstance().createClassIndexes(jars); this.project.resetCachedClasspath(); } } finally { connection.close(); } }
private void prepareCompileAndroidTestJavaV2() { ProjectConnection connection = this.project.getProjectConnection(); try { BuildLauncher buildLauncher = connection.newBuild(); String genTestTask = this.project.getName() + genUnitTestTaskName; String genAndroidTestTask = this.project.getName() + genAndroidTestTaskName; buildLauncher.forTasks(genTestTask, genAndroidTestTask).run(); int size = this.project.getDependencies().size(); String aar = Joiner.on(File.separator) .join(this.project.getProjectRoot(), BUILD_DIR, INTERMEDIATE_DIR, EXPLODED_DIR); List<File> jars = FileUtils.collectFiles(new File(aar), EXT_JAR); for (File jar : jars) { addAAR(jar); } int after = this.project.getDependencies().size(); if (size != after) { CachedASMReflector.getInstance().createClassIndexes(jars); this.project.resetCachedClasspath(); } } finally { connection.close(); } }
/* * perform Gradle tasks such as compile, build, test */ public static void performGradleTasks(String path, String ... tasks) { GradleConnector connector = GradleConnector.newConnector(); connector.forProjectDirectory(new File(path)); ProjectConnection connection = connector.connect(); try { // Configure the build BuildLauncher launcher = connection.newBuild(); launcher.forTasks(tasks); launcher.setStandardOutput(System.out); launcher.setStandardError(System.err); // Run the build launcher.run(); } finally { // Clean up connection.close(); } }