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 d8c622380d..69f70c222f 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 @@ -20,7 +20,6 @@ package org.apache.maven.internal.impl.model; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -58,34 +57,13 @@ public class BuildModelTransformer implements ModelTransformer { void handleParent(ModelTransformerContext context, Model model, Path pomFile, Model.Builder builder) { Parent parent = model.getParent(); if (parent != null) { - String groupId = parent.getGroupId(); - String artifactId = parent.getArtifactId(); String version = parent.getVersion(); - String path = Optional.ofNullable(parent.getRelativePath()).orElse(".."); - if (version == null && !path.isEmpty()) { - Optional resolvedParent = resolveRelativePath( - pomFile, context, Paths.get(path), parent.getGroupId(), parent.getArtifactId()); - if (resolvedParent.isPresent()) { - RelativeProject project = resolvedParent.get(); - if (groupId == null - || groupId.equals(project.getGroupId()) && artifactId == null - || artifactId.equals(project.getArtifactId())) { - groupId = project.getGroupId(); - artifactId = project.getArtifactId(); - version = resolvedParent.get().getVersion(); - } - } - } // CI Friendly version for parent String modVersion = replaceCiFriendlyVersion(context, version); // Update parent - builder.parent(parent.with() - .groupId(groupId) - .artifactId(artifactId) - .version(modVersion) - .build()); + builder.parent(parent.with().version(modVersion).build()); } } diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java index f3a9822b82..d341c75523 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelBuilder.java @@ -23,6 +23,7 @@ import java.io.InputStream; import java.lang.reflect.Field; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -31,6 +32,7 @@ import java.util.LinkedHashSet; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Properties; import java.util.concurrent.Callable; import java.util.concurrent.atomic.AtomicReference; @@ -740,6 +742,40 @@ public class DefaultModelBuilder implements ModelBuilder { if (modelSource.getPath() != null) { model = model.withPomFile(modelSource.getPath()); + Parent parent = model.getParent(); + if (parent != null) { + String groupId = parent.getGroupId(); + String artifactId = parent.getArtifactId(); + String version = parent.getVersion(); + String path = Optional.ofNullable(parent.getRelativePath()).orElse(".."); + if (version == null && !path.isEmpty()) { + Path pomFile = model.getPomFile(); + Path relativePath = Paths.get(path); + Path pomPath = pomFile.resolveSibling(relativePath).normalize(); + if (Files.isDirectory(pomPath)) { + pomPath = getModelProcessor().locateExistingPom(pomPath); + } + if (pomPath != null && Files.isRegularFile(pomPath)) { + ModelBuilderRequest parentRequest = + ModelBuilderRequest.build(request, ModelSource.fromPath(pomPath)); + Model parentModel = readFileModel(parentRequest, problems); + if (parentModel != null) { + String parentGroupId = getGroupId(parentModel); + String parentArtifactId = parentModel.getArtifactId(); + String parentVersion = getVersion(parentModel); + if ((groupId == null || groupId.equals(parentGroupId)) + && (artifactId == null || artifactId.equals(parentArtifactId))) { + model = model.withParent(parent.with() + .groupId(parentGroupId) + .artifactId(parentArtifactId) + .version(parentVersion) + .build()); + } + } + } + } + } + // subprojects discovery if (model.getSubprojects().isEmpty() && model.getModules().isEmpty() diff --git a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/MavenModelMerger.java b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/MavenModelMerger.java index 844b35188f..d340b280e0 100644 --- a/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/MavenModelMerger.java +++ b/maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/MavenModelMerger.java @@ -231,6 +231,37 @@ public class MavenModelMerger extends MavenMerger { } } + @Override + protected void mergeModelBase_Subprojects( + ModelBase.Builder builder, + ModelBase target, + ModelBase source, + boolean sourceDominant, + Map context) { + List src = source.getSubprojects(); + if (!src.isEmpty() && sourceDominant) { + List indices = new ArrayList<>(); + List tgt = target.getSubprojects(); + Set excludes = new LinkedHashSet<>(tgt); + List merged = new ArrayList<>(tgt.size() + src.size()); + merged.addAll(tgt); + for (int i = 0, n = tgt.size(); i < n; i++) { + indices.add(i); + } + for (int i = 0, n = src.size(); i < n; i++) { + String s = src.get(i); + if (!excludes.contains(s)) { + merged.add(s); + indices.add(~i); + } + } + builder.subprojects(merged); + builder.location( + "subprojects", + InputLocation.merge(target.getLocation("subprojects"), source.getLocation("subprojects"), indices)); + } + } + /* * TODO: The order of the merged list could be controlled by an attribute in the model association: target-first, * source-first, dominant-first, recessive-first