View Javadoc

1   package org.apache.maven.plugin.gpg;
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 java.io.File;
23  import java.io.IOException;
24  
25  import org.apache.maven.plugin.AbstractMojo;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.project.MavenProject;
29  
30  /**
31   * @author Benjamin Bentmann
32   */
33  public abstract class AbstractGpgMojo
34      extends AbstractMojo
35  {
36  
37      /**
38       * The directory from which gpg will load keyrings. If not specified, gpg will use the value configured for its
39       * installation, e.g. <code>~/.gnupg</code> or <code>%APPDATA%/gnupg</code>.
40       * 
41       * @parameter expression="${gpg.homedir}"
42       * @since 1.0
43       */
44      private File homedir;
45  
46      /**
47       * The passphrase to use when signing.
48       * 
49       * @parameter expression="${gpg.passphrase}"
50       */
51      private String passphrase;
52  
53      /**
54       * The "name" of the key to sign with. Passed to gpg as <code>--local-user</code>.
55       * 
56       * @parameter expression="${gpg.keyname}"
57       */
58      private String keyname;
59  
60      /**
61       * Passes <code>--use-agent</code> or <code>--no-use-agent</code> to gpg. If using an agent, the passphrase is
62       * optional as the agent will provide it.
63       * 
64       * @parameter expression="${gpg.useagent}" default-value="false"
65       */
66      private boolean useAgent;
67  
68      /**
69       * @parameter default-value="${settings.interactiveMode}"
70       * @readonly
71       */
72      private boolean interactive;
73  
74      GpgSigner newSigner( MavenProject project )
75          throws MojoExecutionException, MojoFailureException
76      {
77          GpgSigner signer = new GpgSigner();
78  
79          signer.setInteractive( interactive );
80          signer.setKeyName( keyname );
81          signer.setUseAgent( useAgent );
82          signer.setHomeDirectory( homedir );
83  
84          signer.setPassPhrase( passphrase );
85          if ( null == passphrase && !useAgent )
86          {
87              if ( !interactive )
88              {
89                  throw new MojoFailureException( "Cannot obtain passphrase in batch mode" );
90              }
91              try
92              {
93                  signer.setPassPhrase( signer.getPassphrase( project ) );
94              }
95              catch ( IOException e )
96              {
97                  throw new MojoExecutionException( "Exception reading passphrase", e );
98              }
99          }
100 
101         return signer;
102     }
103 
104 }