From 6a731c314886ef79407effef4560a771a7d2bdd0 Mon Sep 17 00:00:00 2001 From: Ndacyayisenga-droid Date: Tue, 4 Mar 2025 17:17:05 +0300 Subject: [PATCH 1/2] [MDEP-972] copy-dependencies: new feature to copy signatures alongside artifacts --- pom.xml | 11 ++++ .../CopyDependenciesMojo.java | 51 +++++++++++++++++ .../plugins/dependency/utils/CopyUtil.java | 15 +++++ .../TestCopyDependenciesMojo.java | 55 +++++++++++++++++++ 4 files changed, 132 insertions(+) diff --git a/pom.xml b/pom.xml index ca49d4c69..f792a94db 100644 --- a/pom.xml +++ b/pom.xml @@ -405,6 +405,17 @@ under the License. ${slf4jVersion} test + + org.apache.maven.resolver + maven-resolver-api + 1.9.22 + provided + + + org.apache.maven.resolver + maven-resolver-util + 1.9.22 + 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 e79e991d2..0d6eb7d73 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 @@ protected void copyArtifact( * @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 @@ protected void copyArtifact( } 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 f83bb15cf..28b5cc31a 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 void copyArtifactFile(Artifact sourceArtifact, File destination) throws I 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 ff2d42619..bc53caef4 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,61 @@ public void testCopyArtifactFile() throws Exception { 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 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. * From 8db643a0620d074cc5e413a7ed9148a2529af04e Mon Sep 17 00:00:00 2001 From: Ndacyayisenga-droid Date: Tue, 18 Mar 2025 11:57:42 +0300 Subject: [PATCH 2/2] Fix with mvn spotless:apply --- .../fromDependencies/TestCopyDependenciesMojo.java | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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 bc53caef4..36e85399b 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 @@ -114,14 +114,17 @@ public void testCopySignatureFiles() throws Exception { assertTrue("Failed to create output directory", mojo.outputDirectory.mkdirs()); } - File sourceDirectory = new File(System.getProperty("java.io.tmpdir"), "test-source-" + System.currentTimeMillis()); + 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()); + assertTrue( + "Failed to create parent directory", + artifactFile.getParentFile().mkdirs()); } if (artifactFile.exists()) { assertTrue("Failed to delete existing artifact file", artifactFile.delete()); @@ -130,7 +133,9 @@ public void testCopySignatureFiles() throws Exception { 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()); + assertTrue( + "Failed to create parent directory", + signatureFile.getParentFile().mkdirs()); } if (signatureFile.exists()) { assertTrue("Failed to delete existing signature file", signatureFile.delete());