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

This commit is contained in:
Guillaume Nodet 2024-09-11 18:30:15 +02:00 committed by GitHub
parent e0b01fb5a4
commit e6d038ac07
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 48 additions and 44 deletions

View File

@ -24,7 +24,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.apache.maven.api.di.Named;
@ -59,18 +58,34 @@ 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()) {
version = resolvedParent.get().getVersion();
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);
builder.parent(parent.withVersion(modVersion));
// Update parent
builder.parent(parent.with()
.groupId(groupId)
.artifactId(artifactId)
.version(modVersion)
.build());
}
}
@ -131,17 +146,8 @@ protected Optional<RelativeProject> resolveRelativePath(
return Optional.empty();
}
Optional<RelativeProject> mappedProject = Optional.ofNullable(context.getRawModel(pomFile, pomPath.normalize()))
return Optional.ofNullable(context.getRawModel(pomFile, pomPath.normalize()))
.map(BuildModelTransformer::toRelativeProject);
if (mappedProject.isPresent()) {
RelativeProject project = mappedProject.get();
if (Objects.equals(groupId, project.getGroupId()) && Objects.equals(artifactId, project.getArtifactId())) {
return mappedProject;
}
}
return Optional.empty();
}
private static RelativeProject toRelativeProject(final Model m) {

View File

@ -301,37 +301,6 @@ public DefaultModelValidator(ModelVersionProcessor versionProcessor) {
public void validateFileModel(Model m, ModelBuilderRequest request, ModelProblemCollector problems) {
Parent parent = m.getParent();
if (parent != null) {
validateStringNotEmpty(
"parent.groupId", problems, Severity.FATAL, Version.BASE, parent.getGroupId(), parent);
validateStringNotEmpty(
"parent.artifactId", problems, Severity.FATAL, Version.BASE, parent.getArtifactId(), parent);
if (equals(parent.getGroupId(), m.getGroupId()) && equals(parent.getArtifactId(), m.getArtifactId())) {
addViolation(
problems,
Severity.FATAL,
Version.BASE,
"parent.artifactId",
null,
"must be changed"
+ ", the parent element cannot have the same groupId:artifactId as the project.",
parent);
}
if (equals("LATEST", parent.getVersion()) || equals("RELEASE", parent.getVersion())) {
addViolation(
problems,
Severity.WARNING,
Version.BASE,
"parent.version",
null,
"is either LATEST or RELEASE (both of them are being deprecated)",
parent);
}
}
if (request.getValidationLevel() == ModelBuilderRequest.VALIDATION_LEVEL_MINIMAL) {
// profiles: they are essential for proper model building (may contribute profiles, dependencies...)
HashSet<String> minProfileIds = new HashSet<>();
@ -552,8 +521,37 @@ public void validateRawModel(Model m, ModelBuilderRequest request, ModelProblemC
Parent parent = m.getParent();
if (parent != null) {
validateStringNotEmpty(
"parent.groupId", problems, Severity.FATAL, Version.BASE, parent.getGroupId(), parent);
validateStringNotEmpty(
"parent.artifactId", problems, Severity.FATAL, Version.BASE, parent.getArtifactId(), parent);
validateStringNotEmpty(
"parent.version", problems, Severity.FATAL, Version.BASE, parent.getVersion(), parent);
if (equals(parent.getGroupId(), m.getGroupId()) && equals(parent.getArtifactId(), m.getArtifactId())) {
addViolation(
problems,
Severity.FATAL,
Version.BASE,
"parent.artifactId",
null,
"must be changed"
+ ", the parent element cannot have the same groupId:artifactId as the project.",
parent);
}
if (equals("LATEST", parent.getVersion()) || equals("RELEASE", parent.getVersion())) {
addViolation(
problems,
Severity.WARNING,
Version.BASE,
"parent.version",
null,
"is either LATEST or RELEASE (both of them are being deprecated)",
parent);
}
}
}