diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java index d7a3ba61d..a9de46fe0 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromConfiguration/CopyMojo.java @@ -22,6 +22,7 @@ import java.io.IOException; import java.util.List; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; import org.apache.maven.plugins.annotations.Component; @@ -108,16 +109,18 @@ protected void doExecute() throws MojoExecutionException, MojoFailureException { * * @param artifactItem containing the information about the Artifact to copy. * @throws MojoExecutionException with a message if an error occurs. - * @see CopyUtil#copyFile(File, File) + * @see CopyUtil#copyArtifactFile(Artifact, File) */ protected void copyArtifact(ArtifactItem artifactItem) throws MojoExecutionException { File destFile = new File(artifactItem.getOutputDirectory(), artifactItem.getDestFileName()); try { - copyUtil.copyFile(artifactItem.getArtifact().getFile(), destFile); + copyUtil.copyArtifactFile(artifactItem.getArtifact(), destFile); } catch (IOException e) { throw new MojoExecutionException( - "Failed copy " + artifactItem.getArtifact().getFile() + " to " + destFile, e); + "Failed to copy artifact '" + artifactItem.getArtifact() + "' (" + + artifactItem.getArtifact().getFile() + ") to " + destFile, + e); } } diff --git a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java index 97956ec72..48759af25 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java +++ b/src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java @@ -201,7 +201,7 @@ protected void copyArtifact( * @param theUseBaseVersion specifies if the baseVersion of the artifact should be used instead of the version. * @param removeClassifier specifies if the classifier should be removed from the file name when copying. * @throws MojoExecutionException with a message if an error occurs. - * @see CopyUtil#copyFile(File, File) + * @see CopyUtil#copyArtifactFile(Artifact, File) * @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, boolean, boolean, boolean, boolean, File, Artifact) */ protected void copyArtifact( @@ -227,9 +227,10 @@ protected void copyArtifact( File destFile = new File(destDir, destFileName); try { - copyUtil.copyFile(artifact.getFile(), destFile); + copyUtil.copyArtifactFile(artifact, destFile); } catch (IOException e) { - throw new MojoExecutionException("Failed copy " + artifact.getFile() + " to " + destFile, e); + throw new MojoExecutionException( + "Failed to copy artifact '" + artifact + "' (" + artifact.getFile() + ") to " + destFile, e); } } @@ -271,10 +272,12 @@ public void copyPoms(File destDir, Set artifacts, boolean removeVersio pomArtifact, removeVersion, prependGroupId, useBaseVersion, removeClassifier)); if (!pomDestFile.exists()) { try { - copyUtil.copyFile(pomArtifact.getFile(), pomDestFile); + copyUtil.copyArtifactFile(pomArtifact, pomDestFile); } catch (IOException e) { throw new MojoExecutionException( - "Failed copy " + pomArtifact.getFile() + " to " + pomDestFile, e); + "Failed to copy artifact '" + pomArtifact + "' (" + pomArtifact.getFile() + ") to " + + pomDestFile, + e); } } } diff --git a/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java b/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java index 8078081e1..21d067ae7 100644 --- a/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java +++ b/src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java @@ -25,6 +25,7 @@ import java.io.File; import java.io.IOException; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.MojoExecutionException; import org.codehaus.plexus.util.FileUtils; import org.slf4j.Logger; @@ -50,20 +51,23 @@ public CopyUtil(BuildContext buildContext) { } /** - * Does the actual copy of the file and logging. + * Does the actual copy of the artifact (file) and logging. * - * @param source represents the file to copy. + * @param sourceArtifact represents the artifact (file) to copy. * @param destination file name of destination file. - * @throws IOException with a message if an error occurs. + * @throws IOException if copy has failed + * @throws MojoExecutionException if artifact file is a directory (which has not been packaged yet) * * @since 3.7.0 */ - public void copyFile(File source, File destination) throws IOException, MojoExecutionException { - logger.info("Copying {} to {}", source, destination); + public void copyArtifactFile(Artifact sourceArtifact, File destination) throws IOException, MojoExecutionException { + logger.info("Copying artifact '{}' ({}) to {}", sourceArtifact, sourceArtifact.getFile(), destination); + File source = sourceArtifact.getFile(); if (source.isDirectory()) { // usual case is a future jar packaging, but there are special cases: classifier and other packaging - throw new MojoExecutionException("Artifact has not been packaged yet. When used on reactor artifact, " + throw new MojoExecutionException("Artifact '" + sourceArtifact + + "' has not been packaged yet (is a directory). When used on reactor artifact, " + "copy should be executed after packaging: see MDEP-187."); } diff --git a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java index 5fb0180ea..cde5ea883 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java +++ b/src/test/java/org/apache/maven/plugins/dependency/AbstractDependencyMojoTestCase.java @@ -22,6 +22,7 @@ import java.io.IOException; import org.apache.commons.io.FileUtils; +import org.apache.maven.artifact.Artifact; import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; @@ -63,14 +64,14 @@ protected void tearDown() { FileUtils.deleteDirectory(testDir); } catch (IOException e) { e.printStackTrace(); - fail("Trying to remove directory:" + testDir + System.lineSeparator() + e); + fail("Trying to remove directory: " + testDir + System.lineSeparator() + e); } assertFalse(testDir.exists()); } } - protected void copyFile(File artifact, File destFile) throws MojoExecutionException, IOException { - new CopyUtil(new DefaultBuildContext()).copyFile(artifact, destFile); + protected void copyArtifactFile(Artifact sourceArtifact, File destFile) throws MojoExecutionException, IOException { + new CopyUtil(new DefaultBuildContext()).copyArtifactFile(sourceArtifact, destFile); } protected void installLocalRepository(LegacySupport legacySupport) throws ComponentLookupException { diff --git a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java index c2f7c6946..e2a14f168 100644 --- a/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java +++ b/src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java @@ -30,6 +30,7 @@ import org.apache.maven.plugin.LegacySupport; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.MojoFailureException; +import org.apache.maven.plugin.testing.stubs.ArtifactStub; import org.apache.maven.plugins.dependency.AbstractDependencyMojoTestCase; import org.apache.maven.plugins.dependency.testUtils.stubs.DependencyProjectStub; import org.apache.maven.plugins.dependency.utils.DependencyUtil; @@ -80,14 +81,19 @@ public void assertNoMarkerFile(Artifact artifact) throws MojoExecutionException assertFalse(handle.isMarkerSet()); } - public void testCopyFile() throws Exception { + public void testCopyArtifactFile() throws Exception { + final Artifact srcArtifact = new ArtifactStub(); + srcArtifact.setGroupId("org.apache.maven.plugins"); + srcArtifact.setArtifactId("maven-dependency-plugin-dummy"); + srcArtifact.setVersion("1.0"); File src = File.createTempFile("copy", null); + srcArtifact.setFile(src); File dest = new File(mojo.outputDirectory, "toMe.jar"); assertFalse(dest.exists()); - copyFile(src, dest); + copyArtifactFile(srcArtifact, dest); assertTrue(dest.exists()); }