Skip to content

Commit

Permalink
Improve handling of dependencies with invalid poms
Browse files Browse the repository at this point in the history
Closes gh-365
  • Loading branch information
wilkinsona committed Aug 8, 2023
1 parent 0bbf7b2 commit 1fe2c33
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ List<Model> buildModels(List<ModelInput> inputs) {
List<Model> models = new ArrayList<>();
InMemoryModelCache cache = new InMemoryModelCache();
for (ModelInput input : inputs) {
models.add(buildModel(input, cache));
Model model = buildModel(input, cache);
if (model != null) {
models.add(model);
}
}
return models;
}
Expand All @@ -70,10 +73,7 @@ private Model buildModel(ModelInput input, InMemoryModelCache cache) {
request.setModelCache(cache);
try {
ModelBuildingResult result = createModelBuilder(input.properties).build(request);
List<ModelProblem> errors = extractErrors(result.getProblems());
if (!errors.isEmpty()) {
reportErrors(errors, input.pom);
}
reportErrors(extractErrors(result.getProblems()), input.pom);
return result.getEffectiveModel();
}
catch (ModelBuildingException ex) {
Expand All @@ -86,15 +86,19 @@ private Model buildModel(ModelInput input, InMemoryModelCache cache) {
private List<ModelProblem> extractErrors(List<ModelProblem> problems) {
List<ModelProblem> errors = new ArrayList<>();
for (ModelProblem problem : problems) {
if (problem.getSeverity() == ModelProblem.Severity.ERROR) {
if (problem.getSeverity() == ModelProblem.Severity.ERROR
|| problem.getSeverity() == ModelProblem.Severity.FATAL) {
errors.add(problem);
}
}
return errors;
}

private void reportErrors(List<ModelProblem> errors, File file) {
StringBuilder message = new StringBuilder("Errors occurred while build effective model from " + file + ":");
if (errors.isEmpty()) {
return;
}
StringBuilder message = new StringBuilder("Errors occurred while building effective model from " + file + ":");
for (ModelProblem error : errors) {
message.append("\n " + error.getMessage() + " in " + error.getModelId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Arrays;
import java.util.List;

import org.gradle.testkit.runner.BuildResult;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

Expand Down Expand Up @@ -502,6 +503,13 @@ void resolutionSucceedsWhenDependencyReliesOnConstraintsFromPlatformDependencies
"spring-jcl-5.3.27.jar");
}

@Test
void resolutionSucceedsWhenDependencyHasAnInvalidPom() {
BuildResult result = this.gradleBuild.runner().withArguments("resolve").build();
assertThat(result.getOutput()).contains("Errors occurred while building effective model")
.contains("Building this project requires a newer version of Maven");
}

private void writeLines(Path path, String... lines) {
try {
Path resolvedPath = this.gradleBuild.runner().getProjectDir().toPath().resolve(path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
plugins {
id "java"
id "io.spring.dependency-management"
}

repositories {
mavenCentral()
}

dependencies {
// Invalid model version
implementation("org.eclipse.platform:org.eclipse.core.contenttype:3.8.200")
// Self-referential pom
implementation("eu.fraho.spring:security-jwt-base-spring-boot-starter:4.6.7")
}

tasks.register("resolve") {
doFirst {
def files = project.configurations.runtimeClasspath.resolve()
def output = new File("${buildDir}/resolved.txt")
output.parentFile.mkdirs()
files.collect { it.name }.each { output << "${it}\n" }
}
}

0 comments on commit 1fe2c33

Please sign in to comment.