Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,22 @@ public void injectTransformedArtifacts(RepositorySystemSession session, MavenPro
return;
}
if (Features.consumerPom(session.getConfigProperties())) {
Path buildDir =
project.getBuild() != null ? Paths.get(project.getBuild().getDirectory()) : null;
if (buildDir != null) {
Files.createDirectories(buildDir);
}
Path consumer = buildDir != null
? Files.createTempFile(buildDir, CONSUMER_POM_CLASSIFIER + "-", ".pom")
: Files.createTempFile(CONSUMER_POM_CLASSIFIER + "-", ".pom");
deferDeleteFile(consumer);
boolean alreadyAttached = project.getAttachedArtifacts().stream()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Low: The idempotency check here is placed after the temp file creation and deferDeleteFile() registration (lines 87-90). On repeated invocations where the consumer POM is already attached, a new temp file is created on disk, registered for deferred deletion, but never used.

Consider hoisting the alreadyAttached check to the top of the if (Features.consumerPom(...)) block, before creating the temp file. This would avoid unnecessary I/O on repeated invocations. The files are cleaned up by @PreDestroy doDeleteFiles() so this is not a leak, just wasteful work.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to apply this improvement before merging as we had some performance issue with local repository in rc6 or do you want to apply it in a separate PR/later version?

.anyMatch(a -> CONSUMER_POM_CLASSIFIER.equals(a.getClassifier()) && "pom".equals(a.getType()));
if (!alreadyAttached) {
Path buildDir = project.getBuild() != null
? Paths.get(project.getBuild().getDirectory())
: null;
if (buildDir != null) {
Files.createDirectories(buildDir);
}
Path consumer = buildDir != null
? Files.createTempFile(buildDir, CONSUMER_POM_CLASSIFIER + "-", ".pom")
: Files.createTempFile(CONSUMER_POM_CLASSIFIER + "-", ".pom");
deferDeleteFile(consumer);

project.addAttachedArtifact(createConsumerPomArtifact(project, consumer, session));
project.addAttachedArtifact(createConsumerPomArtifact(project, consumer, session));
}
} else if (project.getModel().getDelegate().isRoot()) {
throw new IllegalStateException(
"The use of the root attribute on the model requires the buildconsumer feature to be active");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;

import org.apache.maven.api.services.Sources;
import org.apache.maven.model.Build;
import org.apache.maven.model.Model;
import org.apache.maven.model.v4.MavenStaxReader;
import org.apache.maven.project.MavenProject;
import org.eclipse.aether.RepositorySystemSession;
import org.eclipse.aether.SessionData;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;
import org.xmlunit.assertj.XmlAssert;

Expand Down Expand Up @@ -106,4 +109,45 @@ void injectTransformedArtifactsWithoutPomShouldNotInjectAnyArtifacts() throws IO

assertThat(emptyProject.getAttachedArtifacts()).isEmpty();
}

@Test
void injectTransformedArtifactsTwiceShouldNotDuplicate(@TempDir Path tempDir) throws IOException {
// Set up a minimal POM file
Path pomFile = tempDir.resolve("pom.xml");
Files.writeString(
pomFile,
"<project><modelVersion>4.0.0</modelVersion>"
+ "<groupId>test</groupId><artifactId>test</artifactId>"
+ "<version>1.0</version></project>");

// Create project with the POM file and a build directory
Model model = new Model();
model.setGroupId("test");
model.setArtifactId("test");
model.setVersion("1.0");
Build build = new Build();
build.setDirectory(tempDir.resolve("target").toString());
model.setBuild(build);
MavenProject project = new MavenProject(model);
project.setFile(pomFile.toFile());

// Mock session with consumer POM feature enabled (default for Maven 4)
RepositorySystemSession session = Mockito.mock(RepositorySystemSession.class);
SessionData sessionData = Mockito.mock(SessionData.class);
when(session.getData()).thenReturn(sessionData);
when(session.getConfigProperties()).thenReturn(Collections.emptyMap());

ConsumerPomArtifactTransformer transformer =
new ConsumerPomArtifactTransformer((s, p, src) -> p.getModel().getDelegate());

// First invocation should attach the consumer POM
transformer.injectTransformedArtifacts(session, project);
assertThat(project.getAttachedArtifacts()).hasSize(1);
assertThat(project.getAttachedArtifacts().get(0).getClassifier()).isEqualTo("consumer");
assertThat(project.getAttachedArtifacts().get(0).getType()).isEqualTo("pom");

// Second invocation should be a no-op (not duplicate the artifact)
transformer.injectTransformedArtifacts(session, project);
assertThat(project.getAttachedArtifacts()).hasSize(1);
}
}
Loading