Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down