View Javadoc
1   package org.apache.maven.plugins.jarsigner;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.LifecyclePhase;
24  import org.apache.maven.plugins.annotations.Mojo;
25  import org.apache.maven.plugins.annotations.Parameter;
26  import org.apache.maven.shared.jarsigner.JarSignerRequest;
27  import org.apache.maven.shared.jarsigner.JarSignerSignRequest;
28  import org.apache.maven.shared.jarsigner.JarSignerUtil;
29  import org.apache.maven.shared.utils.StringUtils;
30  import org.apache.maven.shared.utils.cli.Commandline;
31  
32  import java.io.File;
33  import java.io.IOException;
34  
35  /**
36   * Signs a project artifact and attachments using jarsigner.
37   *
38   * @author <a href="cs@schulte.it">Christian Schulte</a>
39   * @version $Id: JarsignerSignMojo.java 1575440 2014-03-07 22:37:29Z tchemit $
40   * @since 1.0
41   */
42  @Mojo( name = "sign", defaultPhase = LifecyclePhase.PACKAGE )
43  public class JarsignerSignMojo
44      extends AbstractJarsignerMojo
45  {
46  
47      /**
48       * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
49       */
50      @Parameter( property = "jarsigner.keypass" )
51      private String keypass;
52  
53      /**
54       * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
55       */
56      @Parameter( property = "jarsigner.sigfile" )
57      private String sigfile;
58  
59      /**
60       * Indicates whether existing signatures should be removed from the processed JAR files prior to signing them. If
61       * enabled, the resulting JAR will appear as being signed only once.
62       *
63       * @since 1.1
64       */
65      @Parameter( property = "jarsigner.removeExistingSignatures", defaultValue = "false" )
66      private boolean removeExistingSignatures;
67  
68      /**
69       * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
70       *
71       * @since 1.3
72       */
73      @Parameter( property = "jarsigner.tsa" )
74      private String tsa;
75  
76      /**
77       * See <a href="http://java.sun.com/javase/6/docs/technotes/tools/windows/jarsigner.html#Options">options</a>.
78       *
79       * @since 1.3
80       */
81      @Parameter( property = "jarsigner.tsacert" )
82      private String tsacert;
83  
84      @Override
85      protected String getCommandlineInfo( final Commandline commandLine )
86      {
87          String commandLineInfo = commandLine != null ? commandLine.toString() : null;
88  
89          if ( commandLineInfo != null )
90          {
91              commandLineInfo = StringUtils.replace( commandLineInfo, this.keypass, "'*****'" );
92          }
93  
94          return commandLineInfo;
95      }
96  
97      @Override
98      protected void preProcessArchive( final File archive )
99          throws MojoExecutionException
100     {
101         if ( removeExistingSignatures )
102         {
103             try
104             {
105                 JarSignerUtil.unsignArchive( archive );
106             }
107             catch ( IOException e )
108             {
109                 throw new MojoExecutionException( "Failed to unsign archive " + archive + ": " + e.getMessage(), e );
110             }
111         }
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     protected JarSignerRequest createRequest( File archive )
118         throws MojoExecutionException
119     {
120         JarSignerSignRequest request = new JarSignerSignRequest();
121         request.setSigfile( sigfile );
122         request.setTsaLocation( tsa );
123         request.setTsaAlias( tsacert );
124 
125         // Special handling for passwords through the Maven Security Dispatcher
126         request.setKeypass( decrypt( keypass ) );
127         return request;
128     }
129 
130 }