gnodet commented on code in PR #11549:
URL: https://github.com/apache/maven/pull/11549#discussion_r2619712775
##########
impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultProjectManager.java:
##########
@@ -119,15 +119,32 @@ public void attachArtifact(@Nonnull Project project,
@Nonnull ProducedArtifact a
artifact.getExtension(),
null);
}
- if (!Objects.equals(project.getGroupId(), artifact.getGroupId())
- || !Objects.equals(project.getArtifactId(),
artifact.getArtifactId())
- || !Objects.equals(
- project.getVersion(),
artifact.getBaseVersion().toString())) {
- throw new IllegalArgumentException(
- "The produced artifact must have the same
groupId/artifactId/version than the project it is attached to. Expecting "
- + project.getGroupId() + ":" +
project.getArtifactId() + ":" + project.getVersion()
- + " but received " + artifact.getGroupId() + ":" +
artifact.getArtifactId() + ":"
- + artifact.getBaseVersion());
+ // Verify groupId and version, intentionally allow artifactId to
differ as a project may be multi-module.
+ String g1 = project.getGroupId();
+ String a1 = project.getArtifactId();
+ String v1 = project.getVersion();
+ String g2 = artifact.getGroupId();
+ String a2 = artifact.getArtifactId();
+ String v2 = artifact.getBaseVersion().toString();
+
+ // ArtifactId may differ only for multi-module projects (source roots
with module name)
+ boolean isMultiModule = false;
+ boolean artifactMatchesModule = false;
+ for (SourceRoot sr : getSourceRoots(project)) {
+ Optional<String> moduleName = sr.module();
+ if (moduleName.isPresent()) {
+ isMultiModule = true;
+ if (moduleName.get().equals(a2)) {
+ artifactMatchesModule = true;
+ break;
+ }
+ }
+ }
+ if (!(Objects.equals(g1, g2) && Objects.equals(v1, v2) &&
(artifactMatchesModule || Objects.equals(a1, a2)))) {
+ throw new IllegalArgumentException(String.format(
+ "The produced artifact must have the same groupId%s and
version as the project it is attached to.%n"
+ + "Expecting %s:%s:%s but received %s:%s:%s.",
+ isMultiModule ? "" : ", artifactID", g1, a1, v1, g2, a2,
v2));
Review Comment:
```suggestion
String message;
if (isMultiModule) {
// Multi-module project: artifactId may match any declared
module name
message = String.format(
"Cannot attach artifact to project: groupId and
version must match the project, "
+ "and artifactId must match either the
project or a declared module name.%n"
+ " Project coordinates: %s:%s:%s%n"
+ " Artifact coordinates: %s:%s:%s%n"
+ " Hint: The artifactId '%s' does not
match the project artifactId '%s' "
+ "nor any declared module name in source
roots.",
g1, a1, v1, g2, a2, v2, a2, a1);
} else {
// Non-modular project: artifactId must match exactly
message = String.format(
"Cannot attach artifact to project: groupId,
artifactId and version must match the project.%n"
+ " Project coordinates: %s:%s:%s%n"
+ " Artifact coordinates: %s:%s:%s",
g1, a1, v1, g2, a2, v2);
}
throw new IllegalArgumentException(message);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]