[MNG-7255] Infer groupId for intra-reactor dependencies (#1696)

This commit is contained in:
Guillaume Nodet 2024-09-18 14:26:20 +02:00 committed by GitHub
parent 9811df363b
commit 2f5f61a043
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 9 deletions

View File

@ -28,6 +28,7 @@ import java.util.Optional;
import org.apache.maven.api.di.Named; import org.apache.maven.api.di.Named;
import org.apache.maven.api.di.Singleton; import org.apache.maven.api.di.Singleton;
import org.apache.maven.api.model.Dependency; 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.Model;
import org.apache.maven.api.model.Parent; import org.apache.maven.api.model.Parent;
import org.apache.maven.api.services.ModelTransformer; import org.apache.maven.api.services.ModelTransformer;
@ -83,19 +84,36 @@ public class BuildModelTransformer implements ModelTransformer {
List<Dependency> newDeps = new ArrayList<>(); List<Dependency> newDeps = new ArrayList<>();
boolean modified = false; boolean modified = false;
for (Dependency dep : model.getDependencies()) { for (Dependency dep : model.getDependencies()) {
Dependency.Builder depBuilder = null;
if (dep.getVersion() == null) { if (dep.getVersion() == null) {
Model depModel = context.getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId()); Model depModel = context.getRawModel(model.getPomFile(), dep.getGroupId(), dep.getArtifactId());
if (depModel != null) { if (depModel != null) {
String v = depModel.getVersion(); String version = depModel.getVersion();
if (v == null && depModel.getParent() != null) { InputLocation versionLocation = depModel.getLocation("version");
v = depModel.getParent().getVersion(); if (version == null && depModel.getParent() != null) {
version = depModel.getParent().getVersion();
versionLocation = depModel.getParent().getLocation("version");
} }
dep = dep.withVersion(v); depBuilder = Dependency.newBuilder(dep);
depBuilder.version(version).location("version", versionLocation);
if (dep.getGroupId() == null) {
String depGroupId = depModel.getGroupId();
InputLocation groupIdLocation = depModel.getLocation("groupId");
if (depGroupId == null && depModel.getParent() != null) {
depGroupId = depModel.getParent().getGroupId();
groupIdLocation = depModel.getParent().getLocation("groupId");
}
depBuilder.groupId(depGroupId).location("groupId", groupIdLocation);
}
}
}
if (depBuilder != null) {
newDeps.add(depBuilder.build());
modified = true; modified = true;
} } else {
}
newDeps.add(dep); newDeps.add(dep);
} }
}
if (modified) { if (modified) {
builder.dependencies(newDeps); builder.dependencies(newDeps);
} }

View File

@ -83,7 +83,8 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu
public Model getRawModel(Path from, String gId, String aId) { public Model getRawModel(Path from, String gId, String aId) {
Model model = findRawModel(from, gId, aId); Model model = findRawModel(from, gId, aId);
if (model != null) { if (model != null) {
context.modelByGA.put(new GAKey(gId, aId), new Holder(model)); String groupId = DefaultModelBuilder.getGroupId(model);
context.modelByGA.put(new GAKey(groupId, model.getArtifactId()), new Holder(model));
context.modelByPath.put(model.getPomFile(), new Holder(model)); context.modelByPath.put(model.getPomFile(), new Holder(model));
} }
return model; return model;
@ -214,10 +215,20 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu
} }
public ModelSource getSource(String groupId, String artifactId) { public ModelSource getSource(String groupId, String artifactId) {
Set<ModelSource> sources = mappedSources.get(groupId + ":" + artifactId); Set<ModelSource> sources;
if (groupId != null) {
sources = mappedSources.get(groupId + ":" + artifactId);
if (sources == null) { if (sources == null) {
return null; return null;
} }
} else if (artifactId != null) {
sources = mappedSources.get(artifactId);
if (sources == null) {
return null;
}
} else {
return null;
}
return sources.stream() return sources.stream()
.reduce((a, b) -> { .reduce((a, b) -> {
throw new IllegalStateException(String.format( throw new IllegalStateException(String.format(
@ -231,5 +242,6 @@ class DefaultModelTransformerContextBuilder implements ModelTransformerContextBu
mappedSources mappedSources
.computeIfAbsent(groupId + ":" + artifactId, k -> new HashSet<>()) .computeIfAbsent(groupId + ":" + artifactId, k -> new HashSet<>())
.add(source); .add(source);
mappedSources.computeIfAbsent(artifactId, k -> new HashSet<>()).add(source);
} }
} }