/
AbstractCompilerMojo.java
619 lines (505 loc) · 17.4 KB
/
AbstractCompilerMojo.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright 2006 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package copied.org.apache.maven.plugin;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.compiler.Compiler;
import org.codehaus.plexus.compiler.CompilerConfiguration;
import org.codehaus.plexus.compiler.CompilerError;
import org.codehaus.plexus.compiler.CompilerException;
import org.codehaus.plexus.compiler.CompilerOutputStyle;
import org.codehaus.plexus.compiler.manager.CompilerManager;
import org.codehaus.plexus.compiler.manager.NoSuchCompilerException;
import org.codehaus.plexus.compiler.util.scan.InclusionScanException;
import org.codehaus.plexus.compiler.util.scan.SourceInclusionScanner;
import org.codehaus.plexus.compiler.util.scan.mapping.SingleTargetSourceMapping;
import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
import org.codehaus.plexus.compiler.util.scan.mapping.SuffixMapping;
import org.codehaus.plexus.util.StringUtils;
/**
* TODO: At least one step could be optimized, currently the plugin will do two
* scans of all the source code if the compiler has to have the entire set of
* sources. This is currently the case for at least the C# compiler and most
* likely all the other .NET compilers too.
*
* @author others
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
* @version $Id: AbstractCompilerMojo.java 210 2007-02-20 03:02:41Z jvanzyl $
*/
public abstract class AbstractCompilerMojo extends AbstractMojo {
public static final String DEFAULT_SOURCE_VERSION = "1.5";
public static final String DEFAULT_TARGET_VERSION = "1.5";
// ----------------------------------------------------------------------
// Configurables
// ----------------------------------------------------------------------
/**
* Whether to include debugging information in the compiled class files. The
* default value is true.
*
* @parameter expression="${maven.compiler.debug}" default-value="true"
*/
private boolean debug;
/**
* Whether to output messages about what the compiler is doing
*
* @parameter expression="${maven.compiler.verbose}" default-value="false"
*/
private boolean verbose;
/**
* Output source locations where deprecated APIs are used
*
* @parameter expression="${maven.compiler.showDeprecation}"
* default-value="false"
*/
private boolean showDeprecation;
/**
* Optimize compiled code using the compiler's optimization methods
*
* @parameter expression="${maven.compiler.optimize}" default-value="false"
*/
private boolean optimize;
/**
* Output warnings
*
* @parameter expression="${maven.compiler.showWarnings}"
* default-value="false"
*/
private boolean showWarnings;
/**
* The -source argument for the Java compiler
*
* @parameter expression="${maven.compiler.source}"
*/
protected String source;
/**
* The -target argument for the Java compiler
*
* @parameter expression="${maven.compiler.target}"
*/
protected String target;
/**
* The -encoding argument for the Java compiler (kept for backwards compatibility)
*
* @parameter expression="${maven.compiler.encoding}"
* @readonly
* @deprecated use {@link #encoding}
*/
private String mavenCompilerEncoding;
/**
* The -encoding argument for the Java compiler
*
* @parameter expression="${project.build.sourceEncoding}"
*/
private String encoding;
/**
* The granularity in milliseconds of the last modification date for testing
* whether a source needs recompilation
*
* @parameter expression="${lastModGranularityMs}" default-value="0"
*/
private int staleMillis;
/**
* The compiler id of the compiler to use.
*
* @parameter expression="${maven.compiler.compilerId}" default-value="jdt"
*/
private String compilerId;
/**
* Version of the compiler to use, ex. "1.3", "1.5", if fork is set to true
*
* @parameter expression="${maven.compiler.compilerVersion}"
*/
private String compilerVersion;
/**
* Allows running the compiler in a separate process. If "false" it uses the
* built in compiler, while if "true" it will use an executable.
*
* @parameter default-value="false"
*/
private boolean fork;
/**
* Initial size, in megabytes, of the memory allocation pool, ex. "64",
* "64m" if fork is set to true
*
* @parameter expression="${maven.compiler.meminitial}"
*/
private String meminitial;
/**
* maximum size, in megabytes, of the memory allocation pool, ex. "128",
* "128m" if fork is set to true
*
* @parameter expression="${maven.compiler.maxmem}"
*/
private String maxmem;
/**
* The executable of the compiler to use when fork is true.
*
* @parameter expression="${maven.compiler.executable}"
*/
private String executable;
/**
* <p>
* Arguments to be passed to the compiler (prepending a dash) if fork is set
* to true.
* </p>
* <p>
* This is because the list of valid arguments passed to a Java compiler
* varies based on the compiler version.
* </p>
*
* @parameter
*/
private Map compilerArguments;
/**
* <p>
* Unformatted argument string to be passed to the compiler if fork is set
* to true.
* </p>
* <p>
* This is because the list of valid arguments passed to a Java compiler
* varies based on the compiler version.
* </p>
*
* @parameter
*/
private String compilerArgument;
/**
* Used to control the name of the output file when compiling a set of
* sources to a single file.
*
* @parameter expression="${project.build.finalName}"
*/
private String outputFileName;
// ----------------------------------------------------------------------
// Read-only parameters
// ----------------------------------------------------------------------
/**
* The directory to run the compiler from if fork is true.
*
* @parameter expression="${basedir}"
* @required
* @readonly
*/
private File basedir;
/**
* The target directory of the compiler if fork is true.
*
* @parameter expression="${project.build.directory}"
* @required
* @readonly
*/
private File buildDirectory;
/**
* Plexus compiler manager.
*
* @component
*/
private CompilerManager compilerManager;
protected abstract SourceInclusionScanner getSourceInclusionScanner(
int staleMillis);
protected abstract SourceInclusionScanner getSourceInclusionScanner(
String inputFileEnding);
protected abstract List getClasspathElements() throws MojoExecutionException;
protected abstract List getCompileSourceRoots() throws MojoExecutionException;
protected abstract File getOutputDirectory();
public void execute() throws MojoExecutionException,
CompilationFailureException {
// ----------------------------------------------------------------------
// Look up the compiler. This is done before other code than can
// cause the mojo to return before the lookup is done possibly resulting
// in misconfigured POMs still building.
// ----------------------------------------------------------------------
Compiler compiler;
getLog().debug("Using compiler '" + compilerId + "'.");
try {
compiler = compilerManager.getCompiler(compilerId);
} catch (NoSuchCompilerException e) {
throw new MojoExecutionException("No such compiler '"
+ e.getCompilerId() + "'.");
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
List<String> compileSourceRoots = removeEmptyCompileSourceRoots(getCompileSourceRoots());
if (compileSourceRoots.isEmpty()) {
getLog().info("No sources to compile");
return;
}
if (getLog().isDebugEnabled()) {
getLog().debug(
"Source directories: "
+ compileSourceRoots.toString().replace(',', '\n'));
getLog().debug(
"Classpath: "
+ getClasspathElements().toString().replace(',',
'\n'));
getLog().debug("Output directory: " + getOutputDirectory());
}
// ----------------------------------------------------------------------
// Create the compiler configuration
// ----------------------------------------------------------------------
CompilerConfiguration compilerConfiguration = getCompilerConfiguration(compileSourceRoots);
// TODO: have an option to always compile (without need to clean)
Set staleSources;
boolean canUpdateTarget;
try {
staleSources = computeStaleSources(compilerConfiguration, compiler,
getSourceInclusionScanner(staleMillis));
canUpdateTarget = compiler.canUpdateTarget(compilerConfiguration);
if (compiler.getCompilerOutputStyle().equals(
CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES)
&& !canUpdateTarget) {
getLog().info("RESCANNING!");
// TODO: This second scan for source files is sub-optimal
String inputFileEnding = compiler
.getInputFileEnding(compilerConfiguration);
Set sources = computeStaleSources(compilerConfiguration,
compiler, getSourceInclusionScanner(inputFileEnding));
compilerConfiguration.setSourceFiles(sources);
} else {
compilerConfiguration.setSourceFiles(staleSources);
}
} catch (CompilerException e) {
throw new MojoExecutionException(
"Error while computing stale sources.", e);
}
if (staleSources.isEmpty()) {
getLog().info("Nothing to compile - all classes are up to date");
return;
}
// ----------------------------------------------------------------------
// Dump configuration
// ----------------------------------------------------------------------
if (getLog().isDebugEnabled()) {
getLog().debug("Classpath:");
for (Iterator it = getClasspathElements().iterator(); it.hasNext();) {
String s = (String) it.next();
getLog().debug(" " + s);
}
getLog().debug("Source roots:");
for (Iterator it = getCompileSourceRoots().iterator(); it.hasNext();) {
String root = (String) it.next();
getLog().debug(" " + root);
}
if (fork) {
try {
if (compilerConfiguration.getExecutable() != null) {
getLog().debug("Excutable: ");
getLog().debug(
" " + compilerConfiguration.getExecutable());
}
String[] cl = compiler
.createCommandLine(compilerConfiguration);
if (cl != null && cl.length > 0) {
StringBuffer sb = new StringBuffer();
sb.append(cl[0]);
for (int i = 1; i < cl.length; i++) {
sb.append(" ");
sb.append(cl[i]);
}
getLog().debug("Command line options:");
getLog().debug(sb);
}
} catch (CompilerException ce) {
getLog().debug(ce);
}
}
}
// ----------------------------------------------------------------------
// Compile!
// ----------------------------------------------------------------------
List messages;
try {
messages = compiler.compile(compilerConfiguration);
} catch (Exception e) {
// TODO: don't catch Exception
throw new MojoExecutionException("Fatal error compiling", e);
}
boolean compilationError = false;
for (Iterator i = messages.iterator(); i.hasNext();) {
CompilerError message = (CompilerError) i.next();
if (message.isError()) {
compilationError = true;
}
}
if (compilationError) {
throw new CompilationFailureException(messages);
} else {
for (Iterator i = messages.iterator(); i.hasNext();) {
CompilerError message = (CompilerError) i.next();
getLog().warn(message.toString());
}
}
}
protected CompilerConfiguration getCompilerConfiguration(List<String> compileSourceRoots) throws MojoExecutionException {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation(getOutputDirectory()
.getAbsolutePath());
compilerConfiguration.setClasspathEntries(getClasspathElements());
compilerConfiguration.setSourceLocations(compileSourceRoots);
compilerConfiguration.setOptimize(optimize);
compilerConfiguration.setDebug(debug);
compilerConfiguration.setVerbose(verbose);
compilerConfiguration.setShowWarnings(showWarnings);
compilerConfiguration.setShowDeprecation(showDeprecation);
compilerConfiguration.setSourceVersion(source != null? source: DEFAULT_SOURCE_VERSION);
compilerConfiguration.setTargetVersion(target != null? target: DEFAULT_TARGET_VERSION);
compilerConfiguration.setSourceEncoding(getEncoding());
if ((compilerArguments != null) || (compilerArgument != null)) {
LinkedHashMap cplrArgsCopy = new LinkedHashMap();
if (compilerArguments != null) {
for (Iterator i = compilerArguments.entrySet().iterator(); i
.hasNext();) {
Map.Entry me = (Map.Entry) i.next();
String key = (String) me.getKey();
String value = (String) me.getValue();
if (!key.startsWith("-")) {
key = "-" + key;
}
cplrArgsCopy.put(key, value);
}
}
if (!StringUtils.isEmpty(compilerArgument)) {
cplrArgsCopy.put(compilerArgument, null);
}
compilerConfiguration.setCustomCompilerArguments(cplrArgsCopy);
}
compilerConfiguration.setFork(fork);
if (fork) {
if (!StringUtils.isEmpty(meminitial)) {
String value = getMemoryValue(meminitial);
if (value != null) {
compilerConfiguration.setMeminitial(value);
} else {
getLog().info(
"Invalid value for meminitial '" + meminitial
+ "'. Ignoring this option.");
}
}
if (!StringUtils.isEmpty(maxmem)) {
String value = getMemoryValue(maxmem);
if (value != null) {
compilerConfiguration.setMaxmem(value);
} else {
getLog().info(
"Invalid value for maxmem '" + maxmem
+ "'. Ignoring this option.");
}
}
}
compilerConfiguration.setExecutable(executable);
compilerConfiguration.setWorkingDirectory(basedir);
compilerConfiguration.setCompilerVersion(compilerVersion);
compilerConfiguration.setBuildDirectory(buildDirectory);
compilerConfiguration.setOutputFileName(outputFileName);
return compilerConfiguration;
}
private String getEncoding() {
if (encoding != null) {
return encoding;
}
return mavenCompilerEncoding;
}
private String getMemoryValue(String setting) {
String value = null;
// Allow '128' or '128m'
if (isDigits(setting)) {
value = setting + "m";
} else {
if ((isDigits(setting.substring(0, setting.length() - 1)))
&& (setting.toLowerCase().endsWith("m"))) {
value = setting;
}
}
return value;
}
private boolean isDigits(String string) {
for (int i = 0; i < string.length(); i++) {
if (!Character.isDigit(string.charAt(i))) {
return false;
}
}
return true;
}
private Set computeStaleSources(
CompilerConfiguration compilerConfiguration, Compiler compiler,
SourceInclusionScanner scanner) throws MojoExecutionException,
CompilerException {
CompilerOutputStyle outputStyle = compiler.getCompilerOutputStyle();
SourceMapping mapping;
File outputDirectory;
if (outputStyle == CompilerOutputStyle.ONE_OUTPUT_FILE_PER_INPUT_FILE) {
mapping = new SuffixMapping(compiler
.getInputFileEnding(compilerConfiguration), compiler
.getOutputFileEnding(compilerConfiguration));
outputDirectory = getOutputDirectory();
} else if (outputStyle == CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES) {
mapping = new SingleTargetSourceMapping(compiler
.getInputFileEnding(compilerConfiguration), compiler
.getOutputFile(compilerConfiguration));
outputDirectory = buildDirectory;
} else {
throw new MojoExecutionException("Unknown compiler output style: '"
+ outputStyle + "'.");
}
scanner.addSourceMapping(mapping);
Set staleSources = new HashSet();
for (Iterator it = getCompileSourceRoots().iterator(); it.hasNext();) {
String sourceRoot = (String) it.next();
File rootFile = new File(sourceRoot);
if (!rootFile.isDirectory()) {
continue;
}
try {
staleSources.addAll(scanner.getIncludedSources(rootFile,
outputDirectory));
} catch (InclusionScanException e) {
throw new MojoExecutionException(
"Error scanning source root: \'" + sourceRoot + "\' "
+ "for stale files to recompile.", e);
}
}
return staleSources;
}
/**
* @todo also in ant plugin. This should be resolved at some point so that
* it does not need to be calculated continuously - or should the
* plugins accept empty source roots as is?
*/
protected static List removeEmptyCompileSourceRoots(
List compileSourceRootsList) {
List newCompileSourceRootsList = new ArrayList();
if (compileSourceRootsList != null) {
// copy as I may be modifying it
for (Iterator i = compileSourceRootsList.iterator(); i.hasNext();) {
String srcDir = (String) i.next();
if (!newCompileSourceRootsList.contains(srcDir)
&& new File(srcDir).exists()) {
newCompileSourceRootsList.add(srcDir);
}
}
}
return newCompileSourceRootsList;
}
}