1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.plugins.gpg;
20
21 import java.io.File;
22 import java.util.List;
23
24 import org.apache.maven.plugin.MojoExecutionException;
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.apache.maven.plugin.logging.Log;
27
28
29
30
31
32
33
34 public abstract class AbstractGpgSigner {
35 public static final String SIGNATURE_EXTENSION = ".asc";
36
37 protected boolean useAgent;
38
39 protected boolean isInteractive = true;
40
41 protected boolean defaultKeyring = true;
42
43 protected String keyname;
44
45 private Log log;
46
47 protected String passphrase;
48
49 private File outputDir;
50
51 private File buildDir;
52
53 private File baseDir;
54
55 protected File homeDir;
56
57 protected String secretKeyring;
58
59 protected String publicKeyring;
60
61 protected String lockMode;
62
63 protected List<String> args;
64
65 public Log getLog() {
66 return log;
67 }
68
69 public void setArgs(List<String> args) {
70 this.args = args;
71 }
72
73 public void setInteractive(boolean b) {
74 isInteractive = b;
75 }
76
77 public void setLockMode(String lockMode) {
78 this.lockMode = lockMode;
79 }
80
81 public void setUseAgent(boolean b) {
82 useAgent = b;
83 }
84
85 public void setDefaultKeyring(boolean enabled) {
86 defaultKeyring = enabled;
87 }
88
89 public void setKeyName(String s) {
90 keyname = s;
91 }
92
93 public void setLog(Log log) {
94 this.log = log;
95 }
96
97 public void setPassPhrase(String s) {
98 passphrase = s;
99 }
100
101 public void setOutputDirectory(File out) {
102 outputDir = out;
103 }
104
105 public void setBuildDirectory(File out) {
106 buildDir = out;
107 }
108
109 public void setBaseDirectory(File out) {
110 baseDir = out;
111 }
112
113 public void setHomeDirectory(File homeDirectory) {
114 homeDir = homeDirectory;
115 }
116
117 public void setSecretKeyring(String path) {
118 secretKeyring = path;
119 }
120
121 public void setPublicKeyring(String path) {
122 publicKeyring = path;
123 }
124
125 public abstract String signerName();
126
127
128
129
130
131
132 public void prepare() throws MojoFailureException {}
133
134
135
136
137
138
139
140 public abstract String getKeyInfo();
141
142
143
144
145
146
147
148
149
150 public File generateSignatureForArtifact(File file) throws MojoExecutionException {
151
152
153
154
155 File signature = new File(file + SIGNATURE_EXTENSION);
156
157 boolean isInBuildDir = false;
158 if (buildDir != null) {
159 File parent = signature.getParentFile();
160 if (buildDir.equals(parent)) {
161 isInBuildDir = true;
162 }
163 }
164 if (!isInBuildDir && outputDir != null) {
165 String fileDirectory = "";
166 File signatureDirectory = signature;
167
168 while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
169 if (isPossibleRootOfArtifact(signatureDirectory)) {
170 break;
171 }
172 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
173 }
174 signatureDirectory = new File(outputDir, fileDirectory);
175 if (!signatureDirectory.exists()) {
176 signatureDirectory.mkdirs();
177 }
178 signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
179 }
180
181 if (signature.exists()) {
182 signature.delete();
183 }
184
185
186
187
188
189 generateSignatureForFile(file, signature);
190
191 return signature;
192 }
193
194
195
196
197
198
199
200
201 protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
202
203 private boolean isPossibleRootOfArtifact(File signatureDirectory) {
204 return signatureDirectory.equals(outputDir)
205 || signatureDirectory.equals(buildDir)
206 || signatureDirectory.equals(baseDir);
207 }
208 }