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 @@ -170,9 +170,9 @@ protected void doExecute() throws MojoExecutionException {
}

if (isCopyPom() && !useRepositoryLayout) {
copyPoms(getOutputDirectory(), artifacts, this.stripVersion);
copyPoms(getOutputDirectory(), skippedArtifacts, this.stripVersion, this.stripClassifier);
copyPoms(getOutputDirectory(), artifacts, this.stripVersion, this.stripClassifier, true);
// Artifacts that already exist may not yet have poms
copyPoms(getOutputDirectory(), skippedArtifacts, this.stripVersion, this.stripClassifier, false);
}
}

Expand Down Expand Up @@ -341,6 +341,12 @@ public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersio
*/
public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersion, boolean removeClassifier)
throws MojoExecutionException {
copyPoms(destDir, artifacts, removeVersion, removeClassifier, false);
}

private void copyPoms(
File destDir, Set<Artifact> artifacts, boolean removeVersion, boolean removeClassifier, boolean overwrite)
throws MojoExecutionException {

for (Artifact artifact : artifacts) {
Artifact pomArtifact = getResolvedPomArtifact(artifact);
Expand All @@ -353,7 +359,7 @@ public void copyPoms(File destDir, Set<Artifact> artifacts, boolean removeVersio
destDir,
DependencyUtil.getFormattedFileName(
pomArtifact, removeVersion, prependGroupId, useBaseVersion, removeClassifier));
if (!pomDestFile.exists()) {
if (overwrite || !pomDestFile.exists()) {
try {
copyUtil.copyArtifactFile(pomArtifact, pomDestFile);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.HashSet;
import java.util.Set;

Expand All @@ -47,6 +49,9 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;

@MojoTest(realRepositorySession = true)
class TestCopyDependenciesMojo {
Expand Down Expand Up @@ -775,6 +780,82 @@ void testCopyPom(CopyDependenciesMojo mojo) throws Exception {
}
}

@Test
@InjectMojo(goal = "copy-dependencies")
void testCopyPomOverwritesExistingPomWhenArtifactIsCopied(CopyDependenciesMojo mojo) throws Exception {
Artifact artifact = stubFactory.createArtifact("org.example", "artifact", "2.0", Artifact.SCOPE_COMPILE);
Artifact pomArtifact =
stubFactory.createArtifact("org.example", "artifact", "2.0", Artifact.SCOPE_COMPILE, "pom", null);
writeFile(artifact.getFile(), "new jar");
writeFile(pomArtifact.getFile(), "new pom");

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact);
mojo.getProject().setArtifacts(artifacts);

CopyDependenciesMojo mojoSpy = spy(mojo);
doReturn(pomArtifact).when(mojoSpy).getResolvedPomArtifact(any(Artifact.class));
mojoSpy.setCopyPom(true);
mojoSpy.setStripVersion(true);
mojoSpy.overWriteReleases = true;
mojoSpy.overWriteIfNewer = false;

File jarDestination = new File(mojoSpy.outputDirectory, "artifact.jar");
File pomDestination = new File(mojoSpy.outputDirectory, "artifact.pom");
writeFile(jarDestination, "old jar");
writeFile(pomDestination, "old pom");

mojoSpy.execute();

assertEquals("new jar", readFile(jarDestination));
assertEquals("new pom", readFile(pomDestination));
}

@Test
@InjectMojo(goal = "copy-dependencies")
void testCopyPomForSkippedArtifactOnlyWhenMissing(CopyDependenciesMojo mojo) throws Exception {
Artifact artifact = stubFactory.createArtifact("org.example", "artifact", "2.0", Artifact.SCOPE_COMPILE);
Artifact pomArtifact =
stubFactory.createArtifact("org.example", "artifact", "2.0", Artifact.SCOPE_COMPILE, "pom", null);
writeFile(artifact.getFile(), "new jar");
writeFile(pomArtifact.getFile(), "new pom");

Set<Artifact> artifacts = new HashSet<>();
artifacts.add(artifact);
mojo.getProject().setArtifacts(artifacts);

CopyDependenciesMojo mojoSpy = spy(mojo);
doReturn(pomArtifact).when(mojoSpy).getResolvedPomArtifact(any(Artifact.class));
mojoSpy.setCopyPom(true);
mojoSpy.setStripVersion(true);
mojoSpy.overWriteReleases = false;
mojoSpy.overWriteIfNewer = false;

File jarDestination = new File(mojoSpy.outputDirectory, "artifact.jar");
File pomDestination = new File(mojoSpy.outputDirectory, "artifact.pom");
writeFile(jarDestination, "old jar");

mojoSpy.execute();

assertEquals("old jar", readFile(jarDestination));
assertEquals("new pom", readFile(pomDestination));

writeFile(pomDestination, "existing pom");
mojoSpy.execute();

assertEquals("old jar", readFile(jarDestination));
assertEquals("existing pom", readFile(pomDestination));
}

private static void writeFile(File file, String contents) throws IOException {
Files.createDirectories(file.getParentFile().toPath());
Files.write(file.toPath(), contents.getBytes(StandardCharsets.UTF_8));
}

private static String readFile(File file) throws IOException {
return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
}

@Test
@InjectMojo(goal = "copy-dependencies")
void testPrependGroupId(CopyDependenciesMojo mojo) throws Exception {
Expand Down
Loading