Skip to content

Commit 377115a

Browse files
[MDEP-972] copy-dependencies: copy signatures alongside artifacts (#514)
1 parent 96a660f commit 377115a

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

pom.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,17 @@ under the License.
405405
<version>${slf4jVersion}</version>
406406
<scope>test</scope>
407407
</dependency>
408+
<dependency>
409+
<groupId>org.apache.maven.resolver</groupId>
410+
<artifactId>maven-resolver-api</artifactId>
411+
<version>1.9.22</version>
412+
<scope>provided</scope>
413+
</dependency>
414+
<dependency>
415+
<groupId>org.apache.maven.resolver</groupId>
416+
<artifactId>maven-resolver-util</artifactId>
417+
<version>1.9.22</version>
418+
</dependency>
408419
</dependencies>
409420

410421
<build>

src/main/java/org/apache/maven/plugins/dependency/fromDependencies/CopyDependenciesMojo.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,14 @@ public class CopyDependenciesMojo extends AbstractFromDependenciesMojo {
9797
@Parameter(property = "mdep.addParentPoms", defaultValue = "false")
9898
protected boolean addParentPoms;
9999

100+
/**
101+
* Also copy the signature files (.asc) of each artifact.
102+
*
103+
* @since 3.2.0
104+
*/
105+
@Parameter(property = "mdep.copySignatures", defaultValue = "false")
106+
protected boolean copySignatures;
107+
100108
@Inject
101109
// CHECKSTYLE_OFF: ParameterNumber
102110
public CopyDependenciesMojo(
@@ -240,6 +248,8 @@ protected void copyArtifact(
240248
* @see CopyUtil#copyArtifactFile(Artifact, File)
241249
* @see DependencyUtil#getFormattedOutputDirectory(boolean, boolean, boolean, boolean, boolean, boolean, File, Artifact)
242250
*/
251+
private static final String SIGNATURE_EXTENSION = ".asc";
252+
243253
protected void copyArtifact(
244254
Artifact artifact,
245255
boolean removeVersion,
@@ -266,12 +276,53 @@ protected void copyArtifact(
266276
}
267277
try {
268278
copyUtil.copyArtifactFile(artifact, destFile);
279+
280+
// Copy the signature file if the copySignatures flag is true
281+
if (copySignatures) {
282+
copySignatureFile(artifact, destDir, destFileName);
283+
}
284+
269285
} catch (IOException e) {
270286
throw new MojoExecutionException(
271287
"Failed to copy artifact '" + artifact + "' (" + artifact.getFile() + ") to " + destFile, e);
272288
}
273289
}
274290

291+
/**
292+
* Copies the signature file of the artifact to the destination directory, if it exists or can be resolved.
293+
* If the signature file does not exist and cannot be resolved, a warning is logged.
294+
* @param artifact the artifact whose signature file should be copied
295+
* @param destDir the destination directory
296+
* @param destFileName the destination file name without the extension
297+
*/
298+
private void copySignatureFile(Artifact artifact, File destDir, String destFileName) {
299+
File signatureFile = new File(artifact.getFile().getAbsolutePath() + SIGNATURE_EXTENSION);
300+
301+
if (!signatureFile.exists()) {
302+
try {
303+
org.eclipse.aether.artifact.Artifact aArtifact = RepositoryUtils.toArtifact(artifact);
304+
org.eclipse.aether.artifact.Artifact aSignatureArtifact =
305+
new SubArtifact(aArtifact, null, "jar" + SIGNATURE_EXTENSION);
306+
org.eclipse.aether.artifact.Artifact resolvedSignature = getResolverUtil()
307+
.resolveArtifact(aSignatureArtifact, getProject().getRemoteProjectRepositories());
308+
signatureFile = resolvedSignature.getFile();
309+
} catch (ArtifactResolutionException e) {
310+
getLog().warn("Failed to resolve signature file for artifact: " + artifact, e);
311+
}
312+
}
313+
314+
if (signatureFile != null && signatureFile.exists()) {
315+
File signatureDestFile = new File(destDir, destFileName + SIGNATURE_EXTENSION);
316+
try {
317+
copyUtil.copyFile(signatureFile, signatureDestFile);
318+
} catch (IOException e) {
319+
getLog().warn("Failed to copy signature file: " + signatureFile, e);
320+
}
321+
} else {
322+
getLog().warn("Signature file for artifact " + artifact + " not found and could not be resolved.");
323+
}
324+
}
325+
275326
/**
276327
* Copy the pom files associated with the artifacts.
277328
*

src/main/java/org/apache/maven/plugins/dependency/utils/CopyUtil.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,19 @@ public void copyArtifactFile(Artifact sourceArtifact, File destination) throws I
7272
FileUtils.copyFile(source, destination);
7373
buildContext.refresh(destination);
7474
}
75+
76+
/**
77+
* Copies a file to a destination and refreshes the build context for the new file.
78+
*
79+
* @param source the source file to copy
80+
* @param destination the destination file
81+
* @throws IOException if copy has failed
82+
*
83+
* @since 3.2.0
84+
*/
85+
public void copyFile(File source, File destination) throws IOException {
86+
logger.debug("Copying file '{}' to {}", source, destination);
87+
FileUtils.copyFile(source, destination);
88+
buildContext.refresh(destination);
89+
}
7590
}

src/test/java/org/apache/maven/plugins/dependency/fromDependencies/TestCopyDependenciesMojo.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,66 @@ public void testCopyArtifactFile() throws Exception {
101101
assertTrue(dest.exists());
102102
}
103103

104+
/**
105+
* Tests the copying of signature files associated with artifacts.
106+
*
107+
* @throws Exception if an error occurs during the test
108+
*/
109+
public void testCopySignatureFiles() throws Exception {
110+
// Enable the copySignatures parameter
111+
mojo.copySignatures = true;
112+
113+
if (!mojo.outputDirectory.exists()) {
114+
assertTrue("Failed to create output directory", mojo.outputDirectory.mkdirs());
115+
}
116+
117+
File sourceDirectory =
118+
new File(System.getProperty("java.io.tmpdir"), "test-source-" + System.currentTimeMillis());
119+
if (!sourceDirectory.exists()) {
120+
assertTrue("Failed to create source directory", sourceDirectory.mkdirs());
121+
}
122+
123+
File artifactFile = new File(sourceDirectory, "maven-dependency-plugin-1.0.jar");
124+
if (!artifactFile.getParentFile().exists()) {
125+
assertTrue(
126+
"Failed to create parent directory",
127+
artifactFile.getParentFile().mkdirs());
128+
}
129+
if (artifactFile.exists()) {
130+
assertTrue("Failed to delete existing artifact file", artifactFile.delete());
131+
}
132+
assertTrue("Failed to create artifact file", artifactFile.createNewFile());
133+
134+
File signatureFile = new File(sourceDirectory, "maven-dependency-plugin-1.0.jar.asc");
135+
if (!signatureFile.getParentFile().exists()) {
136+
assertTrue(
137+
"Failed to create parent directory",
138+
signatureFile.getParentFile().mkdirs());
139+
}
140+
if (signatureFile.exists()) {
141+
assertTrue("Failed to delete existing signature file", signatureFile.delete());
142+
}
143+
assertTrue("Failed to create signature file", signatureFile.createNewFile());
144+
145+
Artifact artifact = stubFactory.createArtifact(
146+
"org.apache.maven.plugins", "maven-dependency-plugin", "1.0", Artifact.SCOPE_COMPILE);
147+
artifact.setFile(artifactFile);
148+
149+
Set<Artifact> artifacts = new HashSet<>();
150+
artifacts.add(artifact);
151+
mojo.getProject().setArtifacts(artifacts);
152+
153+
mojo.execute();
154+
155+
File copiedSignatureFile = new File(mojo.outputDirectory, "maven-dependency-plugin-1.0.jar.asc");
156+
assertTrue("Signature file was not copied", copiedSignatureFile.exists());
157+
158+
// Clean up
159+
artifactFile.delete();
160+
signatureFile.delete();
161+
sourceDirectory.delete();
162+
}
163+
104164
/**
105165
* Tests the proper discovery and configuration of the mojo.
106166
*

0 commit comments

Comments
 (0)