Skip to content

Commit e6d038a

Browse files
authored
[MNG-8252] Fully infer the parent coordinates if the location points to a valid model (#1706)
1 parent e0b01fb commit e6d038a

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/BuildModelTransformer.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.ArrayList;
2525
import java.util.Arrays;
2626
import java.util.List;
27-
import java.util.Objects;
2827
import java.util.Optional;
2928

3029
import org.apache.maven.api.di.Named;
@@ -59,18 +58,34 @@ public Model transform(ModelTransformerContext context, Model model, Path path)
5958
void handleParent(ModelTransformerContext context, Model model, Path pomFile, Model.Builder builder) {
6059
Parent parent = model.getParent();
6160
if (parent != null) {
61+
String groupId = parent.getGroupId();
62+
String artifactId = parent.getArtifactId();
6263
String version = parent.getVersion();
6364
String path = Optional.ofNullable(parent.getRelativePath()).orElse("..");
6465
if (version == null && !path.isEmpty()) {
6566
Optional<RelativeProject> resolvedParent = resolveRelativePath(
6667
pomFile, context, Paths.get(path), parent.getGroupId(), parent.getArtifactId());
6768
if (resolvedParent.isPresent()) {
68-
version = resolvedParent.get().getVersion();
69+
RelativeProject project = resolvedParent.get();
70+
if (groupId == null
71+
|| groupId.equals(project.getGroupId()) && artifactId == null
72+
|| artifactId.equals(project.getArtifactId())) {
73+
groupId = project.getGroupId();
74+
artifactId = project.getArtifactId();
75+
version = resolvedParent.get().getVersion();
76+
}
6977
}
7078
}
79+
7180
// CI Friendly version for parent
7281
String modVersion = replaceCiFriendlyVersion(context, version);
73-
builder.parent(parent.withVersion(modVersion));
82+
83+
// Update parent
84+
builder.parent(parent.with()
85+
.groupId(groupId)
86+
.artifactId(artifactId)
87+
.version(modVersion)
88+
.build());
7489
}
7590
}
7691

@@ -131,17 +146,8 @@ protected Optional<RelativeProject> resolveRelativePath(
131146
return Optional.empty();
132147
}
133148

134-
Optional<RelativeProject> mappedProject = Optional.ofNullable(context.getRawModel(pomFile, pomPath.normalize()))
149+
return Optional.ofNullable(context.getRawModel(pomFile, pomPath.normalize()))
135150
.map(BuildModelTransformer::toRelativeProject);
136-
137-
if (mappedProject.isPresent()) {
138-
RelativeProject project = mappedProject.get();
139-
140-
if (Objects.equals(groupId, project.getGroupId()) && Objects.equals(artifactId, project.getArtifactId())) {
141-
return mappedProject;
142-
}
143-
}
144-
return Optional.empty();
145151
}
146152

147153
private static RelativeProject toRelativeProject(final Model m) {

maven-api-impl/src/main/java/org/apache/maven/internal/impl/model/DefaultModelValidator.java

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -301,37 +301,6 @@ public DefaultModelValidator(ModelVersionProcessor versionProcessor) {
301301
public void validateFileModel(Model m, ModelBuilderRequest request, ModelProblemCollector problems) {
302302

303303
Parent parent = m.getParent();
304-
if (parent != null) {
305-
validateStringNotEmpty(
306-
"parent.groupId", problems, Severity.FATAL, Version.BASE, parent.getGroupId(), parent);
307-
308-
validateStringNotEmpty(
309-
"parent.artifactId", problems, Severity.FATAL, Version.BASE, parent.getArtifactId(), parent);
310-
311-
if (equals(parent.getGroupId(), m.getGroupId()) && equals(parent.getArtifactId(), m.getArtifactId())) {
312-
addViolation(
313-
problems,
314-
Severity.FATAL,
315-
Version.BASE,
316-
"parent.artifactId",
317-
null,
318-
"must be changed"
319-
+ ", the parent element cannot have the same groupId:artifactId as the project.",
320-
parent);
321-
}
322-
323-
if (equals("LATEST", parent.getVersion()) || equals("RELEASE", parent.getVersion())) {
324-
addViolation(
325-
problems,
326-
Severity.WARNING,
327-
Version.BASE,
328-
"parent.version",
329-
null,
330-
"is either LATEST or RELEASE (both of them are being deprecated)",
331-
parent);
332-
}
333-
}
334-
335304
if (request.getValidationLevel() == ModelBuilderRequest.VALIDATION_LEVEL_MINIMAL) {
336305
// profiles: they are essential for proper model building (may contribute profiles, dependencies...)
337306
HashSet<String> minProfileIds = new HashSet<>();
@@ -552,8 +521,37 @@ public void validateRawModel(Model m, ModelBuilderRequest request, ModelProblemC
552521
Parent parent = m.getParent();
553522

554523
if (parent != null) {
524+
validateStringNotEmpty(
525+
"parent.groupId", problems, Severity.FATAL, Version.BASE, parent.getGroupId(), parent);
526+
527+
validateStringNotEmpty(
528+
"parent.artifactId", problems, Severity.FATAL, Version.BASE, parent.getArtifactId(), parent);
529+
555530
validateStringNotEmpty(
556531
"parent.version", problems, Severity.FATAL, Version.BASE, parent.getVersion(), parent);
532+
533+
if (equals(parent.getGroupId(), m.getGroupId()) && equals(parent.getArtifactId(), m.getArtifactId())) {
534+
addViolation(
535+
problems,
536+
Severity.FATAL,
537+
Version.BASE,
538+
"parent.artifactId",
539+
null,
540+
"must be changed"
541+
+ ", the parent element cannot have the same groupId:artifactId as the project.",
542+
parent);
543+
}
544+
545+
if (equals("LATEST", parent.getVersion()) || equals("RELEASE", parent.getVersion())) {
546+
addViolation(
547+
problems,
548+
Severity.WARNING,
549+
Version.BASE,
550+
"parent.version",
551+
null,
552+
"is either LATEST or RELEASE (both of them are being deprecated)",
553+
parent);
554+
}
557555
}
558556
}
559557

0 commit comments

Comments
 (0)