From 2f5f61a0433cb5d0d24b7c51c44ccdc413d93c54 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 18 Sep 2024 14:26:20 +0200 Subject: [PATCH] [MNG-7255] Infer groupId for intra-reactor dependencies (#1696) --- .../impl/model/BuildModelTransformer.java | 30 +++++++++++++++---- ...DefaultModelTransformerContextBuilder.java | 18 +++++++++-- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java index 69f70c222f..b879649dfe 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java @@ -28,6 +28,7 @@ import java.util.Optional; import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Singleton; import org.apache.maven.api.model.Dependency; +import org.apache.maven.api.model.InputLocation; import org.apache.maven.api.model.Model; import org.apache.maven.api.model.Parent; import org.apache.maven.api.services.ModelTransformer; @@ -83,18 +84,35 @@ public class BuildModelTransformer implements ModelTransformer { List newDeps = new ArrayList<>(); boolean modified = false; for (Dependency dep : model.getDependencies()) { + Dependency.Builder depBuilder = null; if (dep.getVersion() == null) { Model depModel = context.getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId()); if (depModel != null) { - String v = depModel.getVersion(); - if (v == null && depModel.getParent() != null) { - v = depModel.getParent().getVersion(); + String version = depModel.getVersion(); + InputLocation versionLocation = depModel.getLocation("version"); + if (version == null && depModel.getParent() != null) { + version = depModel.getParent().getVersion(); + versionLocation = depModel.getParent().getLocation("version"); + } + depBuilder = Dependency.newBuilder(dep); + depBuilder.version(version).location("version", versionLocation); + if (dep.getGroupId() == null) { + String depGroupId = depModel.getGroupId(); + InputLocation groupIdLocation = depModel.getLocation("groupId"); + if (depGroupId == null && depModel.getParent() != null) { + depGroupId = depModel.getParent().getGroupId(); + groupIdLocation = depModel.getParent().getLocation("groupId"); + } + depBuilder.groupId(depGroupId).location("groupId", groupIdLocation); } - dep = dep.withVersion(v); - modified = true; } } - newDeps.add(dep); + if (depBuilder != null) { + newDeps.add(depBuilder.build()); + modified = true; + } else { + newDeps.add(dep); + } } if (modified) { builder.dependencies(newDeps); diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelTransformerContextBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelTransformerContextBuilder.java index 08a44afcd1..76d94a6d31 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelTransformerContextBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelTransformerContextBuilder.java @@ -83,7 +83,8 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu public Model getRawModel(Path from, String gId, String aId) { Model model = findRawModel(from, gId, aId); if (model != null) { - context.modelByGA.put(new GAKey(gId, aId), new Holder(model)); + String groupId = DefaultModelBuilder.getGroupId(model); + context.modelByGA.put(new GAKey(groupId, model.getArtifactId()), new Holder(model)); context.modelByPath.put(model.getPomFile(), new Holder(model)); } return model; @@ -214,8 +215,18 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu } public ModelSource getSource(String groupId, String artifactId) { - Set sources = mappedSources.get(groupId + ":" + artifactId); - if (sources == null) { + Set sources; + if (groupId != null) { + sources = mappedSources.get(groupId + ":" + artifactId); + if (sources == null) { + return null; + } + } else if (artifactId != null) { + sources = mappedSources.get(artifactId); + if (sources == null) { + return null; + } + } else { return null; } return sources.stream() @@ -231,5 +242,6 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu mappedSources .computeIfAbsent(groupId + ":" + artifactId, k -> new HashSet<>()) .add(source); + mappedSources.computeIfAbsent(artifactId, k -> new HashSet<>()).add(source); } }