View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.gpg;
20  
21  import java.io.File;
22  import java.util.List;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.LifecyclePhase;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.project.MavenProjectHelper;
32  
33  /**
34   * Sign project artifact, the POM, and attached artifacts with GnuPG for deployment.
35   *
36   * @author Jason van Zyl
37   * @author Jason Dillon
38   * @author Daniel Kulp
39   */
40  @Mojo(name = "sign", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
41  public class GpgSignAttachedMojo extends AbstractGpgMojo {
42  
43      /**
44       * A list of files to exclude from being signed. Can contain Ant-style wildcards and double wildcards. The default
45       * excludes are <code>**&#47;*.md5 **&#47;*.sha1 **&#47;*.sha256 **&#47;*.sha512 **&#47;*.asc **&#47;*.sigstore</code>.
46       *
47       * @since 1.0-alpha-4
48       */
49      @Parameter
50      private String[] excludes;
51  
52      /**
53       * The directory where to store signature files.
54       *
55       * @since 1.0-alpha-4
56       */
57      @Parameter(defaultValue = "${project.build.directory}/gpg", alias = "outputDirectory")
58      private File ascDirectory;
59  
60      /**
61       * The maven project.
62       */
63      @Component
64      protected MavenProject project;
65  
66      /**
67       * Maven ProjectHelper
68       */
69      @Component
70      private MavenProjectHelper projectHelper;
71  
72      @Override
73      protected void doExecute() throws MojoExecutionException, MojoFailureException {
74          // ----------------------------------------------------------------------------
75          // Collect files to sign
76          // ----------------------------------------------------------------------------
77  
78          FilesCollector collector = new FilesCollector(project, excludes, getLog());
79          List<FilesCollector.Item> items = collector.collect();
80  
81          // ----------------------------------------------------------------------------
82          // Sign collected files and attach all the signatures
83          // ----------------------------------------------------------------------------
84  
85          AbstractGpgSigner signer = newSigner(project);
86          signer.setOutputDirectory(ascDirectory);
87          signer.setBuildDirectory(new File(project.getBuild().getDirectory()));
88          signer.setBaseDirectory(project.getBasedir());
89  
90          getLog().info("Signer '" + signer.signerName() + "' is signing " + items.size() + " file"
91                  + ((items.size() > 1) ? "s" : "") + " with key " + signer.getKeyInfo());
92  
93          for (FilesCollector.Item item : items) {
94              getLog().debug("Generating signature for " + item.getFile());
95  
96              File signature = signer.generateSignatureForArtifact(item.getFile());
97  
98              projectHelper.attachArtifact(
99                      project,
100                     item.getExtension() + AbstractGpgSigner.SIGNATURE_EXTENSION,
101                     item.getClassifier(),
102                     signature);
103         }
104     }
105 }