1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.gmavenplus.mojo;
18
19 import org.apache.maven.plugins.annotations.Parameter;
20 import org.codehaus.gmavenplus.groovyworkarounds.DotGroovyFile;
21 import org.codehaus.gmavenplus.model.IncludeClasspath;
22 import org.codehaus.gmavenplus.model.internal.Version;
23 import org.codehaus.gmavenplus.util.FileUtils;
24
25 import java.io.File;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.net.MalformedURLException;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35 import static org.codehaus.gmavenplus.util.ReflectionUtils.findConstructor;
36 import static org.codehaus.gmavenplus.util.ReflectionUtils.findMethod;
37 import static org.codehaus.gmavenplus.util.ReflectionUtils.invokeConstructor;
38 import static org.codehaus.gmavenplus.util.ReflectionUtils.invokeMethod;
39
40
41
42
43
44
45
46
47 public abstract class AbstractGenerateStubsMojo extends AbstractGroovyStubSourcesMojo {
48
49
50
51
52 protected static final Version GROOVY_5_0_0_ALPHA1 = new Version(5, 0, 0, "alpha-1");
53
54
55
56
57 protected static final Version GROOVY_4_0_11 = new Version(4, 0, 11);
58
59
60
61
62 protected static final Version GROOVY_4_0_6 = new Version(4, 0, 6);
63
64
65
66
67 protected static final Version GROOVY_4_0_2 = new Version(4, 0, 2);
68
69
70
71
72 protected static final Version GROOVY_4_0_0_BETA1 = new Version(4, 0, 0, "beta-1");
73
74
75
76
77 protected static final Version GROOVY_4_0_0_ALPHA3 = new Version(4, 0, 0, "alpha-3");
78
79
80
81
82 protected static final Version GROOVY_4_0_0_ALPHA1 = new Version(4, 0, 0, "alpha-1");
83
84
85
86
87 protected static final Version GROOVY_3_0_8 = new Version(3, 0, 8);
88
89
90
91
92 protected static final Version GROOVY_3_0_6 = new Version(3, 0, 6);
93
94
95
96
97 protected static final Version GROOVY_3_0_3 = new Version(3, 0, 3);
98
99
100
101
102 protected static final Version GROOVY_3_0_0_BETA2 = new Version(3, 0, 0, "beta-2");
103
104
105
106
107 protected static final Version GROOVY_3_0_0_BETA1 = new Version(3, 0, 0, "beta-1");
108
109
110
111
112 protected static final Version GROOVY_3_0_0_ALPHA4 = new Version(3, 0, 0, "alpha-4");
113
114
115
116
117 protected static final Version GROOVY_3_0_0_ALPHA2 = new Version(3, 0, 0, "alpha-2");
118
119
120
121
122 protected static final Version GROOVY_3_0_0_ALPHA1 = new Version(3, 0, 0, "alpha-1");
123
124
125
126
127 protected static final Version GROOVY_2_6_0_ALPHA4 = new Version(2, 6, 0, "alpha-4");
128
129
130
131
132 protected static final Version GROOVY_2_6_0_ALPHA1 = new Version(2, 6, 0, "alpha-1");
133
134
135
136
137 protected static final Version GROOVY_2_5_7 = new Version(2, 5, 7);
138
139
140
141
142 protected static final Version GROOVY_2_5_3 = new Version(2, 5, 3);
143
144
145
146
147 protected static final Version GROOVY_2_3_3 = new Version(2, 3, 3);
148
149
150
151
152 protected static final Version GROOVY_2_1_3 = new Version(2, 1, 3);
153
154
155
156
157 protected static final Version GROOVY_1_9_0_BETA1 = new Version(1, 9, 0, "beta-1");
158
159
160
161
162 protected static final Version GROOVY_1_9_0_BETA3 = new Version(1, 9, 0, "beta-3");
163
164
165
166
167 protected static final Version GROOVY_1_8_2 = new Version(1, 8, 2);
168
169
170
171
172 protected static final Version GROOVY_1_8_3 = new Version(1, 8, 3);
173
174
175
176
177 @Parameter(defaultValue = "${project.build.sourceEncoding}")
178 protected String sourceEncoding;
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 @Parameter(property = "maven.compiler.target", defaultValue = "1.8")
217 protected String targetBytecode;
218
219
220
221
222
223
224 @Parameter(property = "skipBytecodeCheck", defaultValue = "false")
225 protected boolean skipBytecodeCheck;
226
227
228
229
230 @Parameter(defaultValue = "false")
231 protected boolean debug;
232
233
234
235
236 @Parameter(defaultValue = "false")
237 protected boolean verbose;
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252 @Parameter(defaultValue = "1")
253 protected int warningLevel;
254
255
256
257
258 @Parameter(defaultValue = "0")
259 protected int tolerance;
260
261
262
263
264
265
266
267
268
269
270
271
272 @Parameter(defaultValue = "PROJECT_ONLY")
273 protected IncludeClasspath includeClasspath;
274
275
276
277
278
279
280
281
282
283
284
285
286
287 protected synchronized void doStubGeneration(final Set<File> stubSources, final List<?> classpath, final File outputDirectory) throws ClassNotFoundException, InvocationTargetException, IllegalAccessException, InstantiationException, MalformedURLException {
288 if (stubSources == null || stubSources.isEmpty()) {
289 getLog().info("No sources specified for stub generation. Skipping.");
290 return;
291 }
292
293 setupClassWrangler(classpath, includeClasspath);
294
295 logPluginClasspath();
296 classWrangler.logGroovyVersion(mojoExecution.getMojoDescriptor().getGoal());
297
298 if (!groovyVersionSupportsAction()) {
299 getLog().error("Your Groovy version (" + classWrangler.getGroovyVersionString() + ") doesn't support stub generation. The minimum version of Groovy required is " + minGroovyVersion + ". Skipping stub generation.");
300 return;
301 }
302
303 if (!skipBytecodeCheck) {
304 verifyGroovyVersionSupportsTargetBytecode();
305 }
306
307
308 Class<?> compilerConfigurationClass = classWrangler.getClass("org.codehaus.groovy.control.CompilerConfiguration");
309 Class<?> javaStubCompilationUnitClass = classWrangler.getClass("org.codehaus.groovy.tools.javac.JavaStubCompilationUnit");
310 Class<?> groovyClassLoaderClass = classWrangler.getClass("groovy.lang.GroovyClassLoader");
311
312
313 Object compilerConfiguration = setupCompilerConfiguration(outputDirectory, compilerConfigurationClass);
314 Object groovyClassLoader = invokeConstructor(findConstructor(groovyClassLoaderClass, ClassLoader.class, compilerConfigurationClass), classWrangler.getClassLoader(), compilerConfiguration);
315 Object javaStubCompilationUnit = invokeConstructor(findConstructor(javaStubCompilationUnitClass, compilerConfigurationClass, groovyClassLoaderClass, File.class), compilerConfiguration, groovyClassLoader, outputDirectory);
316
317
318 addGroovySources(stubSources, compilerConfigurationClass, javaStubCompilationUnitClass, compilerConfiguration, javaStubCompilationUnit);
319
320
321 invokeMethod(findMethod(javaStubCompilationUnitClass, "compile"), javaStubCompilationUnit);
322 }
323
324
325
326
327
328
329
330
331
332
333
334 protected Object setupCompilerConfiguration(final File outputDirectory, final Class<?> compilerConfigurationClass) throws InvocationTargetException, IllegalAccessException, InstantiationException {
335 Object compilerConfiguration = invokeConstructor(findConstructor(compilerConfigurationClass));
336 invokeMethod(findMethod(compilerConfigurationClass, "setDebug", boolean.class), compilerConfiguration, debug);
337 invokeMethod(findMethod(compilerConfigurationClass, "setVerbose", boolean.class), compilerConfiguration, verbose);
338 invokeMethod(findMethod(compilerConfigurationClass, "setWarningLevel", int.class), compilerConfiguration, warningLevel);
339 invokeMethod(findMethod(compilerConfigurationClass, "setTolerance", int.class), compilerConfiguration, tolerance);
340 invokeMethod(findMethod(compilerConfigurationClass, "setTargetBytecode", String.class), compilerConfiguration, translateJavacTargetToTargetBytecode(targetBytecode));
341 if (sourceEncoding != null) {
342 invokeMethod(findMethod(compilerConfigurationClass, "setSourceEncoding", String.class), compilerConfiguration, sourceEncoding);
343 }
344 Map<String, Object> options = new HashMap<>();
345 options.put("stubDir", outputDirectory);
346 options.put("keepStubs", Boolean.TRUE);
347 invokeMethod(findMethod(compilerConfigurationClass, "setJointCompilationOptions", Map.class), compilerConfiguration, options);
348
349 return compilerConfiguration;
350 }
351
352
353
354
355
356
357
358
359
360
361
362
363 protected void addGroovySources(final Set<File> stubSources, final Class<?> compilerConfigurationClass, final Class<?> javaStubCompilationUnitClass, final Object compilerConfiguration, final Object javaStubCompilationUnit) throws InvocationTargetException, IllegalAccessException {
364 Set<String> scriptExtensions = new HashSet<>();
365 for (File stubSource : stubSources) {
366 scriptExtensions.add(FileUtils.getFileExtension(stubSource));
367 }
368 getLog().debug("Detected Groovy file extensions: " + scriptExtensions + ".");
369 if (supportsSettingExtensions()) {
370 invokeMethod(findMethod(compilerConfigurationClass, "setScriptExtensions", Set.class), compilerConfiguration, scriptExtensions);
371 }
372 getLog().debug("Adding Groovy to generate stubs for:");
373 Method addSource = findMethod(javaStubCompilationUnitClass, "addSource", File.class);
374 for (File stubSource : stubSources) {
375 getLog().debug(" " + stubSource);
376 if (supportsSettingExtensions()) {
377 invokeMethod(addSource, javaStubCompilationUnit, stubSource);
378 } else {
379 DotGroovyFile dotGroovyFile = new DotGroovyFile(stubSource).setScriptExtensions(scriptExtensions);
380 invokeMethod(addSource, javaStubCompilationUnit, dotGroovyFile);
381 }
382 }
383 }
384
385
386
387
388
389
390 protected boolean supportsSettingExtensions() {
391 return groovyAtLeast(GROOVY_1_8_3) && (groovyOlderThan(GROOVY_1_9_0_BETA1) || groovyNewerThan(GROOVY_1_9_0_BETA3));
392 }
393
394
395
396
397
398
399 protected void logGeneratedStubs(File outputDirectory) {
400 Set<File> stubs = getStubs(outputDirectory);
401 getLog().info("Generated " + stubs.size() + " stub" + (stubs.size() != 1 ? "s" : "") + ".");
402 }
403
404
405
406
407
408
409
410
411
412 protected void resetStubModifiedDates(final Set<File> stubs) {
413 for (File stub : stubs) {
414 boolean success = stub.setLastModified(0L);
415 if (!success) {
416 getLog().warn("Unable to set modified time on stub " + stub.getAbsolutePath() + ".");
417 }
418 }
419 }
420
421
422
423
424
425
426 protected void verifyGroovyVersionSupportsTargetBytecode() {
427 if ("1.5".equals(targetBytecode) || "5".equals(targetBytecode) || "1.6".equals(targetBytecode) || "6".equals(targetBytecode) || "1.7".equals(targetBytecode) || "7".equals(targetBytecode) || "1.8".equals(targetBytecode) || "8".equals(targetBytecode) || "1.9".equals(targetBytecode) || "9".equals(targetBytecode) || "10".equals(targetBytecode)) {
428 if (groovyNewerThan(GROOVY_5_0_0_ALPHA1)) {
429 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " isn't accepted by Groovy " + GROOVY_5_0_0_ALPHA1 + " or newer.");
430 }
431 }
432
433 if ("21".equals(targetBytecode)) {
434 if (groovyOlderThan(GROOVY_4_0_11)) {
435 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_4_0_11 + " or newer.");
436 }
437 } else if ("20".equals(targetBytecode)) {
438 if (groovyOlderThan(GROOVY_4_0_6)) {
439 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_4_0_6 + " or newer.");
440 }
441 } else if ("19".equals(targetBytecode)) {
442 if (groovyOlderThan(GROOVY_4_0_2)) {
443 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_4_0_2 + " or newer.");
444 }
445 } else if ("18".equals(targetBytecode)) {
446 if (groovyOlderThan(GROOVY_4_0_0_BETA1)) {
447 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_4_0_0_BETA1 + " or newer.");
448 }
449 } else if ("17".equals(targetBytecode)) {
450 if (groovyOlderThan(GROOVY_3_0_8) || (groovyAtLeast(GROOVY_4_0_0_ALPHA1) && groovyOlderThan(GROOVY_4_0_0_ALPHA3))) {
451 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_3_0_8 + "/" + GROOVY_4_0_0_ALPHA3 + " or newer.");
452 }
453 } else if ("16".equals(targetBytecode)) {
454 if (groovyOlderThan(GROOVY_3_0_6)) {
455 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_3_0_6 + " or newer.");
456 }
457 } else if ("15".equals(targetBytecode)) {
458 if (groovyOlderThan(GROOVY_3_0_3)) {
459 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_3_0_3 + " or newer.");
460 }
461 } else if ("14".equals(targetBytecode)) {
462 if (groovyOlderThan(GROOVY_3_0_0_BETA2)) {
463 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_3_0_0_BETA2 + " or newer.");
464 }
465 } else if ("13".equals(targetBytecode)) {
466 if (groovyOlderThan(GROOVY_2_5_7) || (groovyAtLeast(GROOVY_2_6_0_ALPHA1) && groovyOlderThan(GROOVY_3_0_0_BETA1))) {
467 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_5_7 + "/" + GROOVY_3_0_0_BETA1 + " or newer. No 2.6 version is supported.");
468 }
469 } else if ("12".equals(targetBytecode) || "11".equals(targetBytecode) || "10".equals(targetBytecode)) {
470 if (groovyOlderThan(GROOVY_2_5_3) || (groovyAtLeast(GROOVY_2_6_0_ALPHA1) && groovyOlderThan(GROOVY_3_0_0_ALPHA4))) {
471 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_5_3 + "/" + GROOVY_3_0_0_ALPHA4 + " or newer. No 2.6 version is supported.");
472 }
473 } else if ("9".equals(targetBytecode) || "1.9".equals(targetBytecode)) {
474 if (!isGroovyIndy() && (groovyOlderThan(GROOVY_2_5_3)
475 || (groovyAtLeast(GROOVY_2_6_0_ALPHA1) && groovyOlderThan(GROOVY_2_6_0_ALPHA4))
476 || (groovyAtLeast(GROOVY_3_0_0_ALPHA1) && groovyOlderThan(GROOVY_3_0_0_ALPHA2)))) {
477 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_5_3 + "/" + GROOVY_2_6_0_ALPHA4 + "/" + GROOVY_3_0_0_ALPHA2 + " or newer.");
478 } else if (isGroovyIndy() && (groovyOlderThan(GROOVY_2_5_3) || (groovyAtLeast(GROOVY_2_6_0_ALPHA1) && groovyOlderThan(GROOVY_3_0_0_ALPHA4)))) {
479 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_5_3 + "/" + GROOVY_3_0_0_ALPHA4 + " or newer. No 2.6 version is supported.");
480 }
481 } else if ("8".equals(targetBytecode) || "1.8".equals(targetBytecode)) {
482 if (groovyOlderThan(GROOVY_2_3_3)) {
483 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_3_3 + " or newer.");
484 }
485 } else if ("7".equals(targetBytecode) || "1.7".equals(targetBytecode) || "6".equals(targetBytecode) || "1.6".equals(targetBytecode)) {
486 if (groovyOlderThan(GROOVY_2_1_3)) {
487 throw new IllegalArgumentException("Target bytecode " + targetBytecode + " requires Groovy " + GROOVY_2_1_3 + " or newer.");
488 }
489 } else if (!"5".equals(targetBytecode) && !"1.5".equals(targetBytecode) && !"4".equals(targetBytecode) && !"1.4".equals(targetBytecode)) {
490 throw new IllegalArgumentException("Unrecognized target bytecode: '" + targetBytecode + "'. This check can be skipped with 'skipBytecodeCheck', but this may result in a different target bytecode being used.");
491 }
492 }
493
494 protected static String translateJavacTargetToTargetBytecode(String targetBytecode) {
495 Map<String, String> javacTargetToTargetBytecode = new HashMap<>();
496 javacTargetToTargetBytecode.put("5", "1.5");
497 javacTargetToTargetBytecode.put("6", "1.6");
498 javacTargetToTargetBytecode.put("7", "1.7");
499 javacTargetToTargetBytecode.put("8", "1.8");
500 javacTargetToTargetBytecode.put("1.9", "9");
501 return javacTargetToTargetBytecode.getOrDefault(targetBytecode, targetBytecode);
502 }
503
504 }