[MNG-7707] Skip consumer pom artifact transformation when no pom is available (fixes #1060)

This commit is contained in:
Giovanni van der Schelde 2023-04-21 21:26:10 +02:00 committed by Guillaume Nodet
parent 2db7c85b64
commit 81231b8865
2 changed files with 27 additions and 0 deletions

View File

@ -62,6 +62,10 @@ public final class ConsumerPomArtifactTransformer {
private static final String CONSUMER_POM_CLASSIFIER = "consumer";
public void injectTransformedArtifacts(MavenProject project, RepositorySystemSession session) throws IOException {
if (project.getFile() == null) {
// If there is no build POM there is no reason to inject artifacts for the consumer POM.
return;
}
if (isActive(session)) {
Path generatedFile;
String buildDirectory =

View File

@ -18,6 +18,7 @@
*/
package org.apache.maven.internal.transformation;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
@ -25,9 +26,17 @@ import java.nio.file.Paths;
import org.apache.maven.model.Model;
import org.apache.maven.model.building.TransformerContext;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.SessionData;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.xmlunit.assertj.XmlAssert;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
class ConsumerPomArtifactTransformerTest {
@Test
void transform() throws Exception {
@ -43,6 +52,20 @@ class ConsumerPomArtifactTransformerTest {
}
}
@Test
void injectTransformedArtifactsWithoutPomShouldNotInjectAnyArtifacts() throws IOException {
MavenProject emptyProject = new MavenProject();
RepositorySystemSession systemSessionMock = Mockito.mock(RepositorySystemSession.class);
SessionData sessionDataMock = Mockito.mock(SessionData.class);
when(systemSessionMock.getData()).thenReturn(sessionDataMock);
when(sessionDataMock.get(any())).thenReturn(new NoTransformerContext());
new ConsumerPomArtifactTransformer().injectTransformedArtifacts(emptyProject, systemSessionMock);
assertThat(emptyProject.getAttachedArtifacts()).isEmpty();
}
private static class NoTransformerContext implements TransformerContext {
@Override
public String getUserProperty(String key) {