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$
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      /**
85       * Location of the extra certchain file.
86       * See 
87       * <a href="http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jarsigner.html#Options">
88       *   Java SE 7 documentation
89       * </a>
90       * for more info.
91       * 
92       * @since 1.5
93       */
94      @Parameter( property = "jarsigner.certchain", readonly = true, required = false )
95      private File certchain;
96  
97      @Override
98      protected String getCommandlineInfo( final Commandline commandLine )
99      {
100         String commandLineInfo = commandLine != null ? commandLine.toString() : null;
101 
102         if ( commandLineInfo != null )
103         {
104             commandLineInfo = StringUtils.replace( commandLineInfo, this.keypass, "'*****'" );
105         }
106 
107         return commandLineInfo;
108     }
109 
110     @Override
111     protected void preProcessArchive( final File archive )
112         throws MojoExecutionException
113     {
114         if ( removeExistingSignatures )
115         {
116             try
117             {
118                 JarSignerUtil.unsignArchive( archive );
119             }
120             catch ( IOException e )
121             {
122                 throw new MojoExecutionException( "Failed to unsign archive " + archive + ": " + e.getMessage(), e );
123             }
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     protected JarSignerRequest createRequest( File archive )
131         throws MojoExecutionException
132     {
133         JarSignerSignRequest request = new JarSignerSignRequest();
134         request.setSigfile( sigfile );
135         request.setTsaLocation( tsa );
136         request.setTsaAlias( tsacert );
137         request.setCertchain( certchain );
138 
139         // Special handling for passwords through the Maven Security Dispatcher
140         request.setKeypass( decrypt( keypass ) );
141         return request;
142     }
143 
144 }