Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MDEP-972] copy-dependencies: copy signatures alongside artifacts #514

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,66 @@ 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<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.
*
Expand Down