package io.takari.maven.plugins.pgp;

import static java.nio.file.Files.copy;
import static java.nio.file.Files.createDirectories;

import io.takari.jpgp.PgpArtifactSigner;
import io.takari.maven.plugins.TakariLifecycleMojo;
import java.io.File;
import java.io.IOException;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Mojo(name = "signArtifact", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
public class SignArtifactMojo extends TakariLifecycleMojo {

  public static final String SIGNATURE_EXTENSION = ".asc";
  private final Logger logger = LoggerFactory.getLogger(SignArtifactMojo.class);

  @Parameter(property = "gpg.skip", defaultValue = "false")
  private boolean skip;

  @Parameter(property = "gpg.passphrase")
  private String passphrase;

  @Override
  protected void executeMojo() throws MojoExecutionException {

    List<SigningBundle> signingBundles = new ArrayList<>();

    if (!"pom".equals(project.getPackaging())) {
      //
      // Artifact
      //
      org.apache.maven.artifact.Artifact artifact = project.getArtifact();
      File file = artifact.getFile();
      if (file == null) {
        logger.info("There is no artifact present. Make sure you run this after the package phase.");
        return;
      }
      File projectArtifactSignature = sign(file);
      if (projectArtifactSignature != null) {
        signingBundles.add(new SigningBundle(artifact.getArtifactHandler().getExtension(), projectArtifactSignature));
      }
    }

    //
    // POM
    //
    File pomToSign = new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".pom");
    try {
      createDirectories(pomToSign.getParentFile().toPath());
      copy(project.getFile().toPath(), pomToSign.toPath(), StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
      throw new MojoExecutionException("Error copying POM for signing.", e);
    }
    File pomSignature = sign(pomToSign);
    if (pomSignature != null) {
      signingBundles.add(new SigningBundle("pom", pomSignature));
    }

    //
    // Attached artifact signatures
    //
    for (org.apache.maven.artifact.Artifact a : project.getAttachedArtifacts()) {
      File signatureFile = sign(a.getFile());
      if (signatureFile != null) {
        signingBundles.add(new SigningBundle(a.getArtifactHandler().getExtension(),
            a.getClassifier(), signatureFile));
      }
    }

    for (SigningBundle bundle : signingBundles) {
      projectHelper.attachArtifact(project, bundle.getExtension() + SIGNATURE_EXTENSION,
          bundle.getClassifier(), bundle.getSignature());
    }
  }

  private File sign(File file) throws MojoExecutionException {
    try {
      PgpArtifactSigner signer = new PgpArtifactSigner();
      if (passphrase != null) {
        return signer.sign(file, passphrase);
      } else {
        return signer.sign(file);
      }
    } catch (Exception e) {
      throw new MojoExecutionException("Error signing artifact " + file + ".", e);
    }
  }
}

@@ -665,16 +665,42 @@
 tifact%22,
+  configurator = %22takari%22,
  default
@@ -1184,16 +1184,131 @@
 tion %7B%0A%0A
+    if (skip) %7B%0A      logger.info(%22Skipping PGP signature generation as per configuration.%22);%0A      return;%0A    %7D%0A%0A
     List

Add the right flag to skip PGP signing