Better handling of gradle connection errors

This commit is contained in:
Richard Willis 2019-12-05 16:35:11 +01:00
Родитель ea4a0172b6
Коммит 90cb599539
2 изменённых файлов: 20 добавлений и 8 удалений

2
java-gradle-tasks/.vscode/launch.json поставляемый
Просмотреть файл

@ -10,7 +10,7 @@
"request": "launch",
"mainClass": "com.github.badsyntax.gradletasks.CliApp",
"projectName": "gradle-tasks",
"args": "../test-fixtures/gradle-groovy-custom-build-file tasks.json",
"args": "../test-fixtures/gradle-kotlin-default-build-file tasks.json",
}
]
}

Просмотреть файл

@ -6,7 +6,7 @@ import java.io.IOException;
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonArray;
import org.gradle.tooling.GradleConnectionException;
import org.gradle.tooling.GradleConnector;
import org.gradle.tooling.ProjectConnection;
import org.gradle.tooling.model.GradleProject;
@ -37,7 +37,7 @@ public class CliApp {
app.writeProjectsToFile();
}
private void writeProjectsToFile() throws IOException {
private void writeProjectsToFile() throws IOException, CliAppException {
JsonArray projects = getProjects();
String jsonString = projects.toString();
FileOutputStream outputStream = new FileOutputStream(targetFile);
@ -46,12 +46,22 @@ public class CliApp {
outputStream.close();
}
private JsonArray getProjects() {
ProjectConnection connection =
GradleConnector.newConnector().forProjectDirectory(sourceDir).connect();
GradleBuild rootBuild = connection.model(GradleBuild.class).get();
GradleProject rootProject = connection.model(GradleProject.class).get();
private JsonArray getProjects() throws CliAppException {
JsonArray jsonProjects = Json.array();
ProjectConnection connection = null;
GradleBuild rootBuild;
GradleProject rootProject;
try {
connection = GradleConnector.newConnector().forProjectDirectory(sourceDir).connect();
rootBuild = connection.model(GradleBuild.class).get();
rootProject = connection.model(GradleProject.class).get();
} catch (GradleConnectionException err) {
if (connection != null) {
connection.close();
}
throw new CliAppException(err.getMessage());
}
rootBuild.getProjects().stream().map(project -> {
GradleProject gradleProject = rootProject.findByPath(project.getPath());
@ -73,7 +83,9 @@ public class CliApp {
.add("buildFile", buildScript.getSourceFile().getAbsolutePath())
.add("tasks", jsonTasks);
}).forEach(jsonProjects::add);
connection.close();
return jsonProjects;
}
}