Skip to content

Commit 84ed8be

Browse files
authored
[MNG-8331] Sometimes versioned dependencies disappear (#1821)
Ensure all dependencies end up in the model. Avoid work when there are no dependencies. Extract method, re-introduce optimization to return original model --- https://issues.apache.org/jira/browse/MNG-8331
1 parent f4eeca2 commit 84ed8be

File tree

1 file changed

+36
-29
lines changed

1 file changed

+36
-29
lines changed

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java

+36-29
Original file line numberDiff line numberDiff line change
@@ -564,40 +564,47 @@ public void mergeRepositories(List<Repository> toAdd, boolean replace) {
564564
// Infer inner reactor dependencies version
565565
//
566566
Model transformFileToRaw(Model model) {
567-
List<Dependency> newDeps = new ArrayList<>();
568-
boolean modified = false;
567+
if (model.getDependencies().isEmpty()) {
568+
return model;
569+
}
570+
List<Dependency> newDeps = new ArrayList<>(model.getDependencies().size());
571+
boolean changed = false;
569572
for (Dependency dep : model.getDependencies()) {
573+
Dependency newDep = null;
570574
if (dep.getVersion() == null) {
571-
Dependency.Builder depBuilder = null;
572-
Model depModel = getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId());
573-
if (depModel != null) {
574-
String version = depModel.getVersion();
575-
InputLocation versionLocation = depModel.getLocation("version");
576-
if (version == null && depModel.getParent() != null) {
577-
version = depModel.getParent().getVersion();
578-
versionLocation = depModel.getParent().getLocation("version");
579-
}
580-
depBuilder = Dependency.newBuilder(dep);
581-
depBuilder.version(version).location("version", versionLocation);
582-
if (dep.getGroupId() == null) {
583-
String depGroupId = depModel.getGroupId();
584-
InputLocation groupIdLocation = depModel.getLocation("groupId");
585-
if (depGroupId == null && depModel.getParent() != null) {
586-
depGroupId = depModel.getParent().getGroupId();
587-
groupIdLocation = depModel.getParent().getLocation("groupId");
588-
}
589-
depBuilder.groupId(depGroupId).location("groupId", groupIdLocation);
590-
}
591-
}
592-
if (depBuilder != null) {
593-
newDeps.add(depBuilder.build());
594-
modified = true;
595-
} else {
596-
newDeps.add(dep);
575+
newDep = inferDependencyVersion(model, dep);
576+
if (newDep != null) {
577+
changed = true;
597578
}
598579
}
580+
newDeps.add(newDep == null ? dep : newDep);
581+
}
582+
return changed ? model.withDependencies(newDeps) : model;
583+
}
584+
585+
private Dependency inferDependencyVersion(Model model, Dependency dep) {
586+
Model depModel = getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId());
587+
if (depModel == null) {
588+
return null;
589+
}
590+
Dependency.Builder depBuilder = Dependency.newBuilder(dep);
591+
String version = depModel.getVersion();
592+
InputLocation versionLocation = depModel.getLocation("version");
593+
if (version == null && depModel.getParent() != null) {
594+
version = depModel.getParent().getVersion();
595+
versionLocation = depModel.getParent().getLocation("version");
596+
}
597+
depBuilder.version(version).location("version", versionLocation);
598+
if (dep.getGroupId() == null) {
599+
String depGroupId = depModel.getGroupId();
600+
InputLocation groupIdLocation = depModel.getLocation("groupId");
601+
if (depGroupId == null && depModel.getParent() != null) {
602+
depGroupId = depModel.getParent().getGroupId();
603+
groupIdLocation = depModel.getParent().getLocation("groupId");
604+
}
605+
depBuilder.groupId(depGroupId).location("groupId", groupIdLocation);
599606
}
600-
return modified ? model.withDependencies(newDeps) : model;
607+
return depBuilder.build();
601608
}
602609

603610
String replaceCiFriendlyVersion(Map<String, String> properties, String version) {

0 commit comments

Comments
 (0)