mirror of https://github.com/apache/maven.git
[MNG-8252] Fully infer the parent coordinates if the location points to a valid model (#1706) (#1721)
This commit is contained in:
parent
f137c13877
commit
237eeba760
|
@ -20,7 +20,6 @@
|
||||||
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
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) {
|
void handleParent(ModelTransformerContext context, Model model, Path pomFile, Model.Builder builder) {
|
||||||
Parent parent = model.getParent();
|
Parent parent = model.getParent();
|
||||||
if (parent != null) {
|
if (parent != null) {
|
||||||
String groupId = parent.getGroupId();
|
|
||||||
String artifactId = parent.getArtifactId();
|
|
||||||
String version = parent.getVersion();
|
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
|
// CI Friendly version for parent
|
||||||
String modVersion = replaceCiFriendlyVersion(context, version);
|
String modVersion = replaceCiFriendlyVersion(context, version);
|
||||||
|
|
||||||
// Update parent
|
// Update parent
|
||||||
builder.parent(parent.with()
|
builder.parent(parent.with().version(modVersion).build());
|
||||||
.groupId(groupId)
|
|
||||||
.artifactId(artifactId)
|
|
||||||
.version(modVersion)
|
|
||||||
.build());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -31,6 +32,7 @@
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
@ -740,6 +742,40 @@ private Model doReadFileModel(
|
||||||
if (modelSource.getPath() != null) {
|
if (modelSource.getPath() != null) {
|
||||||
model = model.withPomFile(modelSource.getPath());
|
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
|
// subprojects discovery
|
||||||
if (model.getSubprojects().isEmpty()
|
if (model.getSubprojects().isEmpty()
|
||||||
&& model.getModules().isEmpty()
|
&& model.getModules().isEmpty()
|
||||||
|
|
|
@ -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,
|
* 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
|
* source-first, dominant-first, recessive-first
|
||||||
|
|
Loading…
Reference in New Issue