From a000a040db90b6ba447afe3122965f508a2a4a35 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Wed, 11 Sep 2024 15:14:30 +0200 Subject: [PATCH] Track location --- .../impl/model/BuildModelTransformer.java | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 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 abd91f2d92f3..ac6bcedbf998 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 @@ -30,6 +30,7 @@ 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; @@ -90,26 +91,39 @@ void handleReactorDependencies(ModelTransformerContext context, Model model, Pat List newDeps = new ArrayList<>(); boolean modified = false; String groupId = model.getGroupId(); + InputLocation groupIdLocation = model.getLocation("groupId"); if (groupId == null) { groupId = model.getParent().getGroupId(); + groupIdLocation = model.getParent().getLocation("groupId"); } for (Dependency dep : model.getDependencies()) { - if (dep.getGroupId() == null) { - dep = dep.withGroupId(groupId); - modified = true; + Dependency.Builder depBuilder = null; + String depGroupId = dep.getGroupId(); + if (depGroupId == null) { + depGroupId = groupId; + depBuilder = Dependency.newBuilder(dep).groupId(groupId).location("groupId", groupIdLocation); } if (dep.getVersion() == null) { - Model depModel = context.getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId()); + Model depModel = context.getRawModel(model.getPomFile(), depGroupId, 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"); + } + if (depBuilder == null) { + depBuilder = Dependency.newBuilder(dep); } - dep = dep.withVersion(v); - modified = true; + depBuilder.version(version).location("version", versionLocation); } } - newDeps.add(dep); + if (depBuilder != null) { + newDeps.add(depBuilder.build()); + modified = true; + } else { + newDeps.add(dep); + } } if (modified) { builder.dependencies(newDeps);