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       * For gpg2, specify true as --no-use-agent was removed in gpg2 and doesn't ask for a passphrase anymore.
64       * 
65       * @parameter expression="${gpg.useagent}" default-value="false"
66       */
67      private boolean useAgent;
68  
69      /**
70       * @parameter default-value="${settings.interactiveMode}"
71       * @readonly
72       */
73      private boolean interactive;
74  
75      /**
76       * The path to the GnuPG executable to use for artifact signing. Defaults to either "gpg" or "gpg.exe" depending on
77       * the operating system.
78       * 
79       * @parameter expression="${gpg.executable}"
80       * @since 1.1
81       */
82      private String executable;
83      
84      GpgSigner newSigner( MavenProject project )
85          throws MojoExecutionException, MojoFailureException
86      {
87          GpgSigner signer = new GpgSigner();
88  
89          signer.setExecutable( executable );
90          signer.setInteractive( interactive );
91          signer.setKeyName( keyname );
92          signer.setUseAgent( useAgent );
93          signer.setHomeDirectory( homedir );
94  
95          signer.setPassPhrase( passphrase );
96          if ( null == passphrase && !useAgent )
97          {
98              if ( !interactive )
99              {
100                 throw new MojoFailureException( "Cannot obtain passphrase in batch mode" );
101             }
102             try
103             {
104                 signer.setPassPhrase( signer.getPassphrase( project ) );
105             }
106             catch ( IOException e )
107             {
108                 throw new MojoExecutionException( "Exception reading passphrase", e );
109             }
110         }
111 
112         return signer;
113     }
114 
115 }