Fix MavenInvoker

Closable in try block
This commit is contained in:
Fabian Krüger
2024-04-22 17:33:49 +02:00
committed by GitHub
parent 3f586a51f5
commit 46ea5d29d8
2 changed files with 10 additions and 21 deletions

View File

@@ -8,7 +8,6 @@
<version>0.1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>org.springframework.rewrite.example</groupId>
<artifactId>plugin-invoker-boot-upgrade-examples</artifactId>
<name>plugin-invoker-boot-upgrade-examples</name>
<description>plugin-invoker-boot-upgrade-examples</description>

View File

@@ -30,34 +30,24 @@ import java.nio.file.Path;
*/
public class GradleInvoker {
private static BuildLauncher getBuildLauncher(Path baseDir, OutputStream os, String[] tasks) {
ProjectConnection connection = getProjectConnection(baseDir);
BuildLauncher buildLauncher = connection.newBuild().setStandardOutput(os).forTasks(tasks);
return buildLauncher;
}
public static GradleInvocationResult runTasks(Path baseDir, String[] args, String... tasks) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayOutputStream es = new ByteArrayOutputStream();
BuildLauncher buildLauncher = getBuildLauncher(baseDir, os, tasks);
buildLauncher.addArguments(args);
buildLauncher.setStandardError(es);
buildLauncher.run();
return new GradleInvocationResult(new String(os.toByteArray()), new String(es.toByteArray()));
}
protected static ProjectConnection getProjectConnection(Path projectDir) {
ProjectConnection connection;
var connector = (DefaultGradleConnector) GradleConnector.newConnector();
if (Files.exists(projectDir.resolve("gradle/wrapper/gradle-wrapper.properties"))) {
DefaultGradleConnector connector = (DefaultGradleConnector) GradleConnector.newConnector();
if (Files.exists(baseDir.resolve("gradle/wrapper/gradle-wrapper.properties"))) {
connector.useBuildDistribution();
}
else {
connector.useGradleVersion("8.4");
}
connection = connector.forProjectDirectory(projectDir.toFile()).connect();
return connection;
try (ProjectConnection connection = connector.forProjectDirectory(baseDir.toFile()).connect()) {
BuildLauncher buildLauncher = connection.newBuild().setStandardOutput(os).forTasks(tasks);
buildLauncher.addArguments(args);
buildLauncher.setStandardError(es);
buildLauncher.run();
connection.close();
return new GradleInvocationResult(new String(os.toByteArray()), new String(es.toByteArray()));
}
}
}