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 /**
85 * Whether to add the default keyrings from gpg's home directory to the list of used keyrings.
86 *
87 * @parameter expression="${gpg.defaultKeyring}" default-value="true"
88 * @since 1.2
89 */
90 private boolean defaultKeyring;
91
92 /**
93 * The path to a secret keyring to add to the list of keyrings. By default, only the {@code secring.gpg} from gpg's
94 * home directory is considered. Use this option (in combination with {@link #publicKeyring} and
95 * {@link #defaultKeyring} if required) to use a different secret key. <em>Note:</em> Relative paths are resolved
96 * against gpg's home directory, not the project base directory.
97 *
98 * @parameter expression="${gpg.secretKeyring}"
99 * @since 1.2
100 */
101 private String secretKeyring;
102
103 /**
104 * The path to a public keyring to add to the list of keyrings. By default, only the {@code pubring.gpg} from gpg's
105 * home directory is considered. Use this option (and {@link #defaultKeyring} if required) to use a different public
106 * key. <em>Note:</em> Relative paths are resolved against gpg's home directory, not the project base directory.
107 *
108 * @parameter expression="${gpg.publicKeyring}"
109 * @since 1.2
110 */
111 private String publicKeyring;
112
113 GpgSigner newSigner( MavenProject project )
114 throws MojoExecutionException, MojoFailureException
115 {
116 GpgSigner signer = new GpgSigner();
117
118 signer.setExecutable( executable );
119 signer.setInteractive( interactive );
120 signer.setKeyName( keyname );
121 signer.setUseAgent( useAgent );
122 signer.setHomeDirectory( homedir );
123 signer.setDefaultKeyring( defaultKeyring );
124 signer.setSecretKeyring( secretKeyring );
125 signer.setPublicKeyring( publicKeyring );
126
127 signer.setPassPhrase( passphrase );
128 if ( null == passphrase && !useAgent )
129 {
130 if ( !interactive )
131 {
132 throw new MojoFailureException( "Cannot obtain passphrase in batch mode" );
133 }
134 try
135 {
136 signer.setPassPhrase( signer.getPassphrase( project ) );
137 }
138 catch ( IOException e )
139 {
140 throw new MojoExecutionException( "Exception reading passphrase", e );
141 }
142 }
143
144 return signer;
145 }
146
147 }