Skip to content

Commit

Permalink
Handle signature files (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
RobiNino authored Jul 10, 2024
1 parent 36645e2 commit 8d25f55
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.Set;

import static org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils.addArtifactInfoToDeployDetails;
import static org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils.createArtifactBuilder;

public class IvyPublicationExtractor extends PublicationExtractor<IvyPublication> {

Expand Down Expand Up @@ -95,10 +94,7 @@ private File extractIvyDescriptor(IvyPublication publication, Map<QName, String>
if (!ivyFile.exists()) {
return null;
}
DeployDetails.Builder builder = createArtifactBuilder(ivyFile, publication.getName());
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(
publication.getModule(), "xml", "ivy", null, extraInfo, ivyFile);
addArtifactToDeployDetails(publication, builder, artifactInfo);
buildAndPublishArtifactWithSignatures(ivyFile, publication, publication.getModule(), "xml", "ivy", null, extraInfo);
return ivyFile;
}

Expand Down Expand Up @@ -132,11 +128,7 @@ private void extractIvyArtifacts(File ivyFile, IvyPublication publication, Map<Q
if (file.equals(ivyFile)) {
continue;
}
DeployDetails.Builder builder = createArtifactBuilder(file, publication.getName());
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(
artifact.getName(), artifact.getExtension(), artifact.getType(), artifact.getClassifier(),
extraInfo, file);
addArtifactToDeployDetails(publication, builder, artifactInfo);
buildAndPublishArtifactWithSignatures(file, publication, artifact.getName(), artifact.getExtension(), artifact.getType(), artifact.getClassifier(), extraInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Objects;

import static org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils.addArtifactInfoToDeployDetails;
import static org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils.createArtifactBuilder;

public class MavenPublicationExtractor extends PublicationExtractor<MavenPublication> {

Expand Down Expand Up @@ -76,10 +75,7 @@ private void extractMavenDescriptor(MavenPublication publication) {
if (!pomFile.exists()) {
return;
}
DeployDetails.Builder builder = createArtifactBuilder(pomFile, publication.getName());
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(
publication.getArtifactId(), "pom", "pom", null, pomFile);
addArtifactToDeployDetails(publication, builder, artifactInfo);
buildAndPublishArtifactWithSignatures(pomFile, publication, publication.getArtifactId(), "pom", "pom", null, null);
}

/**
Expand Down Expand Up @@ -112,11 +108,6 @@ private void extractMavenArtifacts(MavenPublication publication) {

private void createPublishArtifactInfoAndAddToDeployDetails(MavenArtifact artifact, MavenPublication publication) {
File file = artifact.getFile();
DeployDetails.Builder builder = createArtifactBuilder(file, publication.getName());
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(
publication.getArtifactId(), artifact.getExtension(),
artifact.getExtension(), artifact.getClassifier(),
file);
addArtifactToDeployDetails(publication, builder, artifactInfo);
buildAndPublishArtifactWithSignatures(file, publication, publication.getArtifactId(), artifact.getExtension(), artifact.getExtension(), artifact.getClassifier(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@

import org.gradle.api.publish.Publication;
import org.gradle.api.publish.tasks.GenerateModuleMetadata;
import org.gradle.plugins.signing.Sign;
import org.jfrog.build.extractor.clientConfiguration.deploy.DeployDetails;
import org.jfrog.gradle.plugin.artifactory.extractor.PublishArtifactInfo;
import org.jfrog.gradle.plugin.artifactory.task.ArtifactoryTask;

import javax.xml.namespace.QName;
import java.io.File;
import java.util.Map;

import static org.jfrog.gradle.plugin.artifactory.utils.PublicationUtils.createArtifactBuilder;

public abstract class PublicationExtractor<ActualPublication extends Publication> {
protected ArtifactoryTask artifactoryTask;
// Signature types supported by the Signing plugin.
private final String[] SIGNATURE_EXTENSIONS = {"asc", "sig"};

public PublicationExtractor(ArtifactoryTask artifactoryTask) {
this.artifactoryTask = artifactoryTask;
Expand Down Expand Up @@ -65,12 +70,60 @@ public void extractModuleInfo() {
continue;
}

DeployDetails.Builder builder = createArtifactBuilder(moduleMetadata, generateModuleMetadata.getPublication().get().getName());

//noinspection unchecked
ActualPublication actualPublication = (ActualPublication) publication;
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(getPublicationArtifactId(actualPublication), "module", "module", null, moduleMetadata);
addArtifactToDeployDetails(actualPublication, builder, artifactInfo);
buildAndPublishArtifactWithSignatures(moduleMetadata, actualPublication, getPublicationArtifactId(actualPublication), "module", "module", null, null);
}
}

/**
* Build and publish the artifact and add it to deploy details.
* If the Signing plugin was used, do the same to the artifact's signatures.
*
* @param file - The file to publish
* @param publication - The publication to extract details from
* @param artifactId - The artifact ID
* @param artifactExtension - The artifact extension
* @param artifactType - The artifact type
* @param artifactClassifier - The artifact classifier
* @param extraInfo - Extra information to add to the deploy details
*/
protected void buildAndPublishArtifactWithSignatures(File file, ActualPublication publication, String artifactId, String artifactExtension, String artifactType, String artifactClassifier, Map<QName, String> extraInfo) {
buildAndPublishArtifact(file, publication, artifactId, artifactExtension, artifactType, artifactClassifier, extraInfo);
if (!isSignTaskExists()) {
return;
}
for (String signatureExtension : SIGNATURE_EXTENSIONS) {
File signatureFile = new File(file.getAbsolutePath() + "." + signatureExtension);
if (!signatureFile.exists()) {
continue;
}
buildAndPublishArtifact(signatureFile, publication, artifactId, artifactType + "." + signatureExtension, artifactType + "." + signatureExtension, artifactClassifier, extraInfo);
}
}

/**
* Build and publish the artifact and add it to deploy details.
*
* @param file - The file to publish
* @param publication - The publication to extract details from
* @param artifactId - The artifact ID
* @param artifactExtension - The artifact extension
* @param artifactType - The artifact type
* @param artifactClassifier - The artifact classifier
* @param extraInfo - Extra information to add to the deploy details
*/
private void buildAndPublishArtifact(File file, ActualPublication publication, String artifactId, String artifactExtension, String artifactType, String artifactClassifier, Map<QName, String> extraInfo) {
DeployDetails.Builder builder = createArtifactBuilder(file, publication.getName());
PublishArtifactInfo artifactInfo = new PublishArtifactInfo(
artifactId, artifactExtension, artifactType, artifactClassifier, extraInfo, file);
addArtifactToDeployDetails(publication, builder, artifactInfo);
}

/**
* @return true if the Signing plugin was used in the project
*/
private boolean isSignTaskExists() {
return !artifactoryTask.getProject().getTasks().withType(Sign.class).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.gradle.api.publish.tasks.GenerateModuleMetadata;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.*;
import org.gradle.plugins.signing.Sign;
import org.jfrog.build.api.builder.ModuleType;
import org.jfrog.build.api.multiMap.Multimap;
import org.jfrog.build.api.multiMap.SetMultimap;
Expand Down Expand Up @@ -195,6 +196,7 @@ public void moduleType(String moduleType) {
* Extract the publications arguments for the task and make sure this task dependsOn the specified publications tasks.
*/
private void checkDependsOnArtifactsToPublish() {
createDependencyOnSigningTask();
extractPublications();
if (!hasPublications()) {
if (publishPublicationsSpecified) {
Expand All @@ -211,6 +213,10 @@ private void checkDependsOnArtifactsToPublish() {
createDependencyOnModuleMetadata();
}

private void createDependencyOnSigningTask() {
dependsOn(getProject().getTasks().withType(Sign.class));
}

/**
* Extract the given publications arguments, process each entry, validate the argument and fetch its matching Publication
*/
Expand Down

0 comments on commit 8d25f55

Please sign in to comment.