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.jarsigner;
20  
21  import java.io.File;
22  import java.io.IOException;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.shared.jarsigner.JarSigner;
29  import org.apache.maven.shared.jarsigner.JarSignerRequest;
30  import org.apache.maven.shared.jarsigner.JarSignerUtil;
31  import org.apache.maven.shared.jarsigner.JarSignerVerifyRequest;
32  import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
33  import org.apache.maven.shared.utils.cli.javatool.JavaToolResult;
34  
35  /**
36   * Checks the signatures of a project artifact and attachments using jarsigner.
37   *
38   * @author <a href="cs@schulte.it">Christian Schulte</a>
39   * @since 1.0
40   */
41  @Mojo(name = "verify", defaultPhase = LifecyclePhase.VERIFY, threadSafe = true)
42  public class JarsignerVerifyMojo extends AbstractJarsignerMojo {
43  
44      /**
45       * See <a href="https://docs.oracle.com/javase/7/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
46       */
47      @Parameter(property = "jarsigner.certs", defaultValue = "false")
48      private boolean certs;
49  
50      /**
51       * When <code>true</code> this will make the execute() operation fail,
52       * throwing an exception, when verifying a non signed jar.
53       * Primarily to keep backwards compatibility with existing code, and allow reusing the
54       * bean in unattended operations when set to <code>false</code>.
55       *
56       * @since 1.3
57       **/
58      @Parameter(property = "jarsigner.errorWhenNotSigned", defaultValue = "false")
59      private boolean errorWhenNotSigned;
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      protected JarSignerRequest createRequest(File archive) {
66          JarSignerVerifyRequest request = new JarSignerVerifyRequest();
67          request.setCerts(certs);
68          return request;
69      }
70  
71      @Override
72      protected void preProcessArchive(File archive) throws MojoExecutionException {
73          super.preProcessArchive(archive);
74  
75          if (errorWhenNotSigned) {
76  
77              // check archive if signed
78              boolean archiveSigned;
79              try {
80                  archiveSigned = JarSignerUtil.isArchiveSigned(archive);
81              } catch (IOException e) {
82                  throw new MojoExecutionException(
83                          "Failed to check if archive " + archive + " is signed: " + e.getMessage(), e);
84              }
85  
86              if (!archiveSigned) {
87  
88                  // fails, archive must be signed
89                  throw new MojoExecutionException(getMessage("archiveNotSigned", archive));
90              }
91          }
92      }
93  
94      @Override
95      protected void executeJarSigner(JarSigner jarSigner, JarSignerRequest request)
96              throws JavaToolException, MojoExecutionException {
97          JavaToolResult result = jarSigner.execute(request);
98          int resultCode = result.getExitCode();
99          if (resultCode != 0) {
100             throw new MojoExecutionException(
101                     getMessage("failure", getCommandlineInfo(result.getCommandline()), resultCode));
102         }
103     }
104 }