[MNG-8252] Fully infer the parent coordinates if the location points to a valid model (#1706) (#1721)

This commit is contained in:
Guillaume Nodet 2024-09-12 06:37:14 +02:00 committed by GitHub
parent f137c13877
commit 237eeba760
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 68 additions and 23 deletions

View File

@ -20,7 +20,6 @@
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 Model transform(ModelTransformerContext context, Model model, Path path)
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<RelativeProject> 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());
}
}

View File

@ -23,6 +23,7 @@
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.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 @@ private Model doReadFileModel(
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()

View File

@ -231,6 +231,37 @@ protected void mergeModelBase_Modules(
}
}
@Override
protected void mergeModelBase_Subprojects(
ModelBase.Builder builder,
ModelBase target,
ModelBase source,
boolean sourceDominant,
Map<Object, Object> context) {
List<String> src = source.getSubprojects();
if (!src.isEmpty() && sourceDominant) {
List<Integer> indices = new ArrayList<>();
List<String> tgt = target.getSubprojects();
Set<String> excludes = new LinkedHashSet<>(tgt);
List<String> 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