-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCommandLineMojo.java
executable file
·353 lines (319 loc) · 12.3 KB
/
CommandLineMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation. All rights reserved.
* Copyright (c) 2013, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.spec.maven;
import java.io.Console;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.jar.JarFile;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.glassfish.spec.Artifact;
import org.glassfish.spec.Spec;
import org.glassfish.spec.Spec.JarType;
/**
* Maven Goal to run spec verifications from the command line.
*
* @author Romain Grecourt
*/
@Mojo(name = "cli",
requiresProject = true,
defaultPhase = LifecyclePhase.VALIDATE)
public final class CommandLineMojo extends AbstractMojo {
/**
* Is it a final specification?.
*/
@Parameter(property = "isFinal", defaultValue = "false")
private boolean isFinal;
/**
* Is it an API jar?.
*/
@Parameter(property = "isApi", defaultValue = "true")
private String jarType;
/**
* Path to the API jar file.
*/
@Parameter(property = "apijar")
private String apiJar;
/**
* Path to the Impl jar file.
*/
@Parameter(property = "impljar")
private String implJar;
/**
* Implementation namespace.
*/
@Parameter(property = "implnamespace")
private String implNamespace;
/**
* Mode. Allowed values are "javaee", "jakarta"
*/
@Parameter(property = "specMode", defaultValue = "jakarta")
private String specMode;
/**
* API package.
*/
@Parameter(property = "apipackage")
private String apiPackage;
/**
* Spec version.
*/
@Parameter(property = "specversion")
private String specVersion;
/**
* Spec implementation version.
*/
@Parameter(property = "specimplversion")
private String specImplVersion;
/**
* Implementation version.
*/
@Parameter(property = "implversion")
private String implVersion;
/**
* New implementation version.
*/
@Parameter(property = "newimplversion")
private String newImplVersion;
/**
* New spec version.
*/
@Parameter(property = "newspecversion")
private String newSpecVersion;
/**
* Spec build.
*/
@Parameter(property = "specbuild")
private String specBuild;
/**
* Implementation build.
*/
@Parameter(property = "implbuild")
private String implBuild;
/**
* Property file.
*/
@Parameter(property = "properties")
private File properties;
/**
* Show the usage.
*/
@Parameter(property = "help", defaultValue = "true")
private boolean help;
/**
* The system console.
*/
private static Console cons;
/**
* Prompt with the string and return the user's input.
* @param msg the prompt message
* @return the user input
*/
private static String prompt(final String msg) {
if (cons == null) {
return null;
}
String s = cons.readLine("%s: ", msg);
if (s == null || s.length() == 0) {
return null;
}
return s;
}
/**
* Print error and exit.
* @param msg the error message
*/
private static void fail(final String msg) {
System.err.println("ERROR: " + msg);
System.exit(1);
}
/**
* Print a given parameter to the standard output.
* @param arg the parameter
* @param desc the description
*/
private static void printParam(final String arg, final String desc) {
StringBuilder sb = new StringBuilder("\t-D");
System.out.println(sb.append(arg).append(' ').append(desc).toString());
}
@Override
@SuppressWarnings({
"checkstyle:LineLength",
"checkstyle:MethodLength"
})
public void execute() throws MojoExecutionException, MojoFailureException {
if (help) {
printParam("help", "\t\t\tprint usage (this message)");
printParam("properties", "file\tread settings from property file");
printParam("nonfinal", "\t\tnon-final specification");
printParam("standalone", "\t\tAPI has a standalone implementation");
printParam("apijar", "api.jar\tAPI jar file");
printParam("impljar", "impl.jar\timplementation jar file");
printParam("apipackage", "package\tAPI package");
printParam("implpackage", "package\timplementation package");
printParam("specversion", "version\tversion number of the JCP specification");
printParam("specimplversion", "vers\tversion number of the API classes");
printParam("implversion", "version\tversion number of the implementation");
printParam("newspecversion", "vers\tversion number of the spec under development");
printParam("specbuild", "num\t\tbuild number of spec API jar file");
printParam("newimplversion", "vers\tversion number of the implementation when final");
printParam("implbuild", "num\t\tbuild number of implementation jar file");
printParam("specMode", "specMode\t'javaee' or 'jakarta'");
return;
}
Artifact artifact = null;
if (properties != null) {
FileInputStream fis = null;
try {
Properties p = new Properties();
fis = new FileInputStream(properties);
p.load(fis);
fis.close();
specMode = p.getProperty("SPEC_MODE", specMode);
apiPackage = p.getProperty("API_PACKAGE", apiPackage);
implNamespace = p.getProperty("IMPL_NAMESPACE", implNamespace);
jarType = p.getProperty("JAR_TYPE", jarType);
if (jarType.equals(JarType.impl.toString())) {
implVersion = p.getProperty("SPEC_IMPL_VERSION", implVersion);
specBuild = p.getProperty("SPEC_BUILD", specBuild);
newSpecVersion = p.getProperty("NEW_SPEC_VERSION", newSpecVersion);
apiJar = p.getProperty("API_JAR", apiJar);
try (JarFile jar = new JarFile(apiJar)) {
artifact = Artifact.fromJar(jar);
}
} else {
implVersion = p.getProperty("IMPL_VERSION", implVersion);
implBuild = p.getProperty("IMPL_BUILD", implBuild);
newImplVersion = p.getProperty("NEW_IMPL_VERSION", newImplVersion);
implJar = p.getProperty("IMPL_JAR", implJar);
try (JarFile jar = new JarFile(implJar)) {
artifact = Artifact.fromJar(jar);
}
}
specVersion = p.getProperty("SPEC_VERSION", specVersion);
// really, any of the above 4
isFinal = newSpecVersion == null;
} catch (FileNotFoundException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
} catch (IOException ex) {
throw new MojoExecutionException(ex.getMessage(), ex);
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
getLog().warn(ex.getMessage());
}
}
}
if (jarType.equals(JarType.impl.toString())) {
if (implJar != null) {
fail("--impljar must not be specified if no standalone implementation");
}
if (implNamespace != null) {
fail("--implpackage must not be specified if no standalone implementation");
}
if (implVersion != null) {
fail("--implversion must not be specified if no standalone implementation");
}
if (newImplVersion != null) {
fail("--newimplversion must not be specified if no standalone implementation");
}
}
if (isFinal) {
if (newSpecVersion != null) {
fail("--newspecversion must not be specified for final specification");
}
if (specBuild != null) {
fail("--specbuild must not be specified for final specification");
}
if (newImplVersion != null) {
fail("--newimplversion must not be specified for final specification");
}
if (implBuild != null) {
fail("--implbuild must not be specified for final specification");
}
}
// if no options, prompt for everything
if (properties == null
&& apiJar == null
&& implJar == null
&& implNamespace == null
&& apiPackage == null
&& specVersion == null
&& specImplVersion == null
&& implVersion == null
&& newImplVersion == null
&& newSpecVersion == null
&& specBuild == null
&& implBuild == null) {
cons = System.console();
String s;
s = prompt("Is this a non-final specification?");
isFinal = !(s.charAt(0) == 'y');
s = prompt("Is there a standalone implementation of this specification?");
if (!(s.charAt(0) == 'y')) {
jarType = JarType.impl.name();
} else {
jarType = JarType.api.name();
}
specMode = prompt("Enter the spec mode ('javaee' or 'jakarta')");
apiPackage = prompt("Enter the main API package (e.g., javax.wombat)");
specVersion = prompt("Enter the version number of the JCP specification");
if (jarType.equals(JarType.impl.toString())) {
specImplVersion = prompt("Enter the version number of the API jar file");
newSpecVersion = prompt("Enter the version number of the implementation that will be used when the implementation is final");
if (!isFinal) {
specBuild = prompt("Enter the build number of the implementation jar file");
}
artifact = new Artifact(apiPackage, apiPackage + Spec.API_SUFFIX, newSpecVersion);
} else {
implNamespace = prompt("Enter the main implementation package (e.g., com.sun.wombat)");
if (!isFinal) {
implBuild = prompt("Enter the build number of the implementation jar file");
}
newImplVersion = prompt("Enter the version number of the Impl jar file");
artifact = new Artifact(implNamespace, apiPackage, newImplVersion);
}
}
// TODO remove mojo parameters and replace with spec.
Spec spec = new Spec();
spec.setSpecMode(specMode);
spec.setArtifact(artifact);
spec.setSpecVersion(specVersion);
spec.setNewSpecVersion(newSpecVersion);
spec.setSpecImplVersion(specImplVersion);
spec.setImplVersion(implVersion);
spec.setSpecBuild(specBuild);
spec.setImplBuild(implBuild);
spec.setApiPackage(apiPackage);
spec.setImplNamespace(implNamespace);
spec.setJarType(jarType);
spec.setNonFinal(!isFinal);
spec.verify();
for (String error : spec.getErrors()) {
System.out.println(error);
}
}
}