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 public void prepare() throws MojoFailureException {}
128
129
130
131
132
133
134
135
136 public File generateSignatureForArtifact(File file) throws MojoExecutionException {
137
138
139
140
141 File signature = new File(file + SIGNATURE_EXTENSION);
142
143 boolean isInBuildDir = false;
144 if (buildDir != null) {
145 File parent = signature.getParentFile();
146 if (buildDir.equals(parent)) {
147 isInBuildDir = true;
148 }
149 }
150 if (!isInBuildDir && outputDir != null) {
151 String fileDirectory = "";
152 File signatureDirectory = signature;
153
154 while ((signatureDirectory = signatureDirectory.getParentFile()) != null) {
155 if (isPossibleRootOfArtifact(signatureDirectory)) {
156 break;
157 }
158 fileDirectory = signatureDirectory.getName() + File.separatorChar + fileDirectory;
159 }
160 signatureDirectory = new File(outputDir, fileDirectory);
161 if (!signatureDirectory.exists()) {
162 signatureDirectory.mkdirs();
163 }
164 signature = new File(signatureDirectory, file.getName() + SIGNATURE_EXTENSION);
165 }
166
167 if (signature.exists()) {
168 signature.delete();
169 }
170
171
172
173
174
175 generateSignatureForFile(file, signature);
176
177 return signature;
178 }
179
180
181
182
183
184
185
186
187 protected abstract void generateSignatureForFile(File file, File signature) throws MojoExecutionException;
188
189 private boolean isPossibleRootOfArtifact(File signatureDirectory) {
190 return signatureDirectory.equals(outputDir)
191 || signatureDirectory.equals(buildDir)
192 || signatureDirectory.equals(baseDir);
193 }
194 }