This is an automated email from the ASF dual-hosted git repository.

hboutemy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-dependency-plugin.git


The following commit(s) were added to refs/heads/master by this push:
     new 377115aa [MDEP-972] copy-dependencies: copy signatures alongside 
artifacts (#514)
377115aa is described below

commit 377115aadb72aa2a8b11a6d2fea7708adad9eafb
Author: Tayebwa Noah <[email protected]>
AuthorDate: Tue Mar 25 11:20:36 2025 +0300

    [MDEP-972] copy-dependencies: copy signatures alongside artifacts (#514)
---
 pom.xml                                            | 11 ++++
 .../fromDependencies/CopyDependenciesMojo.java     | 51 ++++++++++++++++++
 .../maven/plugins/dependency/utils/CopyUtil.java   | 15 ++++++
 .../fromDependencies/TestCopyDependenciesMojo.java | 60 ++++++++++++++++++++++
 4 files changed, 137 insertions(+)

diff --git a/pom.xml b/pom.xml
index ca49d4c6..f792a94d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -405,6 +405,17 @@ under the License.
       <version>${slf4jVersion}</version>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.maven.resolver</groupId>
+      <artifactId>maven-resolver-api</artifactId>
+      <version>1.9.22</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.maven.resolver</groupId>
+      <artifactId>maven-resolver-util</artifactId>
+      <version>1.9.22</version>
+    </dependency>
   </dependencies>
 
   <build>
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 e79e991d..0d6eb7d7 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
@@ -97,6 +97,14 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
     @Parameter(property = "mdep.addParentPoms", defaultValue = "false")
     protected boolean addParentPoms;
 
+    /**
+     * Also copy the signature files (.asc) of each artifact.
+     *
+     * @since 3.2.0
+     */
+    @Parameter(property = "mdep.copySignatures", defaultValue = "false")
+    protected boolean copySignatures;
+
     @Inject
     // CHECKSTYLE_OFF: ParameterNumber
     public CopyDependenciesMojo(
@@ -240,6 +248,8 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
      * @see CopyUtil#copyArtifactFile(Artifact, File)
      * @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, 
boolean, boolean, boolean, boolean, File, Artifact)
      */
+    private static final String SIGNATURE_EXTENSION = ".asc";
+
     protected void copyArtifact(
             Artifact artifact,
             boolean removeVersion,
@@ -266,12 +276,53 @@ public class CopyDependenciesMojo extends 
AbstractFromDependenciesMojo {
         }
         try {
             copyUtil.copyArtifactFile(artifact, destFile);
+
+            // Copy the signature file if the copySignatures flag is true
+            if (copySignatures) {
+                copySignatureFile(artifact, destDir, destFileName);
+            }
+
         } catch (IOException e) {
             throw new MojoExecutionException(
                     "Failed to copy artifact '" + artifact + "' (" + 
artifact.getFile() + ") to " + destFile, e);
         }
     }
 
+    /**
+     * Copies the signature file of the artifact to the destination directory, 
if it exists or can be resolved.
+     * If the signature file does not exist and cannot be resolved, a warning 
is logged.
+     * @param artifact the artifact whose signature file should be copied
+     * @param destDir the destination directory
+     * @param destFileName the destination file name without the extension
+     */
+    private void copySignatureFile(Artifact artifact, File destDir, String 
destFileName) {
+        File signatureFile = new File(artifact.getFile().getAbsolutePath() + 
SIGNATURE_EXTENSION);
+
+        if (!signatureFile.exists()) {
+            try {
+                org.eclipse.aether.artifact.Artifact aArtifact = 
RepositoryUtils.toArtifact(artifact);
+                org.eclipse.aether.artifact.Artifact aSignatureArtifact =
+                        new SubArtifact(aArtifact, null, "jar" + 
SIGNATURE_EXTENSION);
+                org.eclipse.aether.artifact.Artifact resolvedSignature = 
getResolverUtil()
+                        .resolveArtifact(aSignatureArtifact, 
getProject().getRemoteProjectRepositories());
+                signatureFile = resolvedSignature.getFile();
+            } catch (ArtifactResolutionException e) {
+                getLog().warn("Failed to resolve signature file for artifact: 
" + artifact, e);
+            }
+        }
+
+        if (signatureFile != null && signatureFile.exists()) {
+            File signatureDestFile = new File(destDir, destFileName + 
SIGNATURE_EXTENSION);
+            try {
+                copyUtil.copyFile(signatureFile, signatureDestFile);
+            } catch (IOException e) {
+                getLog().warn("Failed to copy signature file: " + 
signatureFile, e);
+            }
+        } else {
+            getLog().warn("Signature file for artifact " + artifact + " not 
found and could not be resolved.");
+        }
+    }
+
     /**
      * Copy the pom files associated with the artifacts.
      *
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 f83bb15c..28b5cc31 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
@@ -72,4 +72,19 @@ public class CopyUtil {
         FileUtils.copyFile(source, destination);
         buildContext.refresh(destination);
     }
+
+    /**
+     * Copies a file to a destination and refreshes the build context for the 
new file.
+     *
+     * @param source the source file to copy
+     * @param destination the destination file
+     * @throws IOException if copy has failed
+     *
+     * @since 3.2.0
+     */
+    public void copyFile(File source, File destination) throws IOException {
+        logger.debug("Copying file '{}' to {}", source, destination);
+        FileUtils.copyFile(source, destination);
+        buildContext.refresh(destination);
+    }
 }
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 ff2d4261..36e85399 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
@@ -101,6 +101,66 @@ public class TestCopyDependenciesMojo extends 
AbstractDependencyMojoTestCase {
         assertTrue(dest.exists());
     }
 
+    /**
+     * Tests the copying of signature files associated with artifacts.
+     *
+     * @throws Exception if an error occurs during the test
+     */
+    public void testCopySignatureFiles() throws Exception {
+        // Enable the copySignatures parameter
+        mojo.copySignatures = true;
+
+        if (!mojo.outputDirectory.exists()) {
+            assertTrue("Failed to create output directory", 
mojo.outputDirectory.mkdirs());
+        }
+
+        File sourceDirectory =
+                new File(System.getProperty("java.io.tmpdir"), "test-source-" 
+ System.currentTimeMillis());
+        if (!sourceDirectory.exists()) {
+            assertTrue("Failed to create source directory", 
sourceDirectory.mkdirs());
+        }
+
+        File artifactFile = new File(sourceDirectory, 
"maven-dependency-plugin-1.0.jar");
+        if (!artifactFile.getParentFile().exists()) {
+            assertTrue(
+                    "Failed to create parent directory",
+                    artifactFile.getParentFile().mkdirs());
+        }
+        if (artifactFile.exists()) {
+            assertTrue("Failed to delete existing artifact file", 
artifactFile.delete());
+        }
+        assertTrue("Failed to create artifact file", 
artifactFile.createNewFile());
+
+        File signatureFile = new File(sourceDirectory, 
"maven-dependency-plugin-1.0.jar.asc");
+        if (!signatureFile.getParentFile().exists()) {
+            assertTrue(
+                    "Failed to create parent directory",
+                    signatureFile.getParentFile().mkdirs());
+        }
+        if (signatureFile.exists()) {
+            assertTrue("Failed to delete existing signature file", 
signatureFile.delete());
+        }
+        assertTrue("Failed to create signature file", 
signatureFile.createNewFile());
+
+        Artifact artifact = stubFactory.createArtifact(
+                "org.apache.maven.plugins", "maven-dependency-plugin", "1.0", 
Artifact.SCOPE_COMPILE);
+        artifact.setFile(artifactFile);
+
+        Set<Artifact> artifacts = new HashSet<>();
+        artifacts.add(artifact);
+        mojo.getProject().setArtifacts(artifacts);
+
+        mojo.execute();
+
+        File copiedSignatureFile = new File(mojo.outputDirectory, 
"maven-dependency-plugin-1.0.jar.asc");
+        assertTrue("Signature file was not copied", 
copiedSignatureFile.exists());
+
+        // Clean up
+        artifactFile.delete();
+        signatureFile.delete();
+        sourceDirectory.delete();
+    }
+
     /**
      * Tests the proper discovery and configuration of the mojo.
      *

Reply via email to