Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,16 @@ 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())) {
// Verify groupId and version, intentionally allow artifactId to differ as a project may be multi-module.
String g1 = project.getGroupId();
String g2 = artifact.getGroupId();
String v1 = project.getVersion();
String v2 = artifact.getBaseVersion().toString();
if (!Objects.equals(g1, g2) || !Objects.equals(v1, v2)) {
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());
"The produced artifact must have the same groupId and version than the project it is attached to. Expecting "
+ g1 + ':' + project.getArtifactId() + ':' + v1 + " but received "
+ g2 + ':' + artifact.getArtifactId() + ':' + v2);
}
getMavenProject(project)
.addAttachedArtifact(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

class DefaultProjectManagerTest {
Expand All @@ -56,7 +57,16 @@ void attachArtifact() {
when(artifact.getBaseVersion()).thenReturn(versionParser.parseVersion("1.0-SNAPSHOT"));
projectManager.attachArtifact(project, artifact, path);

// Verify that no exception is thrown when only the artifactId differs
when(artifact.getArtifactId()).thenReturn("anotherArtifact");
assertThrows(IllegalArgumentException.class, () -> projectManager.attachArtifact(project, artifact, path));
projectManager.attachArtifact(project, artifact, path);

// Verify that an exception is thrown when the groupId differs
when(artifact.getGroupId()).thenReturn("anotherGroup");
String message = assertThrows(
IllegalArgumentException.class, () -> projectManager.attachArtifact(project, artifact, path))
.getMessage();
assertTrue(message.contains("myGroup:myArtifact:1.0-SNAPSHOT"));
assertTrue(message.contains("anotherGroup:anotherArtifact:1.0-SNAPSHOT"));
}
}