forked from apache/maven-compiler-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractCompilerMojo.java
1895 lines (1629 loc) · 67.9 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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package org.apache.maven.plugin.compiler;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.DefaultArtifact;
import org.apache.maven.artifact.handler.ArtifactHandler;
import org.apache.maven.artifact.handler.manager.ArtifactHandlerManager;
import org.apache.maven.artifact.resolver.ArtifactResolutionRequest;
import org.apache.maven.artifact.resolver.ArtifactResolutionResult;
import org.apache.maven.artifact.resolver.ResolutionErrorHandler;
import org.apache.maven.artifact.versioning.VersionRange;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecution;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.project.MavenProject;
import org.apache.maven.repository.RepositorySystem;
import org.apache.maven.shared.incremental.IncrementalBuildHelper;
import org.apache.maven.shared.incremental.IncrementalBuildHelperRequest;
import org.apache.maven.shared.utils.ReaderFactory;
import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.io.FileUtils;
import org.apache.maven.shared.utils.logging.MessageBuilder;
import org.apache.maven.shared.utils.logging.MessageUtils;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
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.CompilerMessage;
import org.codehaus.plexus.compiler.CompilerNotImplementedException;
import org.codehaus.plexus.compiler.CompilerOutputStyle;
import org.codehaus.plexus.compiler.CompilerResult;
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.languages.java.jpms.JavaModuleDescriptor;
import org.codehaus.plexus.languages.java.version.JavaVersion;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
/**
* 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:[email protected]">Trygve Laugstøl</a>
* @since 2.0
*/
public abstract class AbstractCompilerMojo
extends AbstractMojo
{
protected static final String PS = System.getProperty( "path.separator" );
static final String DEFAULT_SOURCE = "1.7";
static final String DEFAULT_TARGET = "1.7";
// Used to compare with older targets
static final String MODULE_INFO_TARGET = "1.9";
// ----------------------------------------------------------------------
// Configurables
// ----------------------------------------------------------------------
/**
* Indicates whether the build will continue even if there are compilation errors.
*
* @since 2.0.2
*/
@Parameter( property = "maven.compiler.failOnError", defaultValue = "true" )
private boolean failOnError = true;
/**
* Indicates whether the build will continue even if there are compilation warnings.
*
* @since 3.6
*/
@Parameter( property = "maven.compiler.failOnWarning", defaultValue = "false" )
private boolean failOnWarning;
/**
* Set to <code>true</code> to include debugging information in the compiled class files.
*/
@Parameter( property = "maven.compiler.debug", defaultValue = "true" )
private boolean debug = true;
/**
* Set to <code>true</code> to generate metadata for reflection on method parameters.
* @since 3.6.2
*/
@Parameter( property = "maven.compiler.parameters", defaultValue = "false" )
private boolean parameters;
/**
* Set to <code>true</code> to show messages about what the compiler is doing.
*/
@Parameter( property = "maven.compiler.verbose", defaultValue = "false" )
private boolean verbose;
/**
* Sets whether to show source locations where deprecated APIs are used.
*/
@Parameter( property = "maven.compiler.showDeprecation", defaultValue = "false" )
private boolean showDeprecation;
/**
* Set to <code>true</code> to optimize the compiled code using the compiler's optimization methods.
* @deprecated This property is a no-op in {@code javac}.
*/
@Deprecated
@Parameter( property = "maven.compiler.optimize", defaultValue = "false" )
private boolean optimize;
/**
* Set to <code>true</code> to show compilation warnings.
*/
@Parameter( property = "maven.compiler.showWarnings", defaultValue = "false" )
private boolean showWarnings;
/**
* <p>The -source argument for the Java compiler.</p>
*
* <b>NOTE: </b>Since 3.8.0 the default value has changed from 1.5 to 1.6.
* Since 3.9.0 the default value has changed from 1.6 to 1.7
*/
@Parameter( property = "maven.compiler.source", defaultValue = DEFAULT_SOURCE )
protected String source;
/**
* <p>The -target argument for the Java compiler.</p>
*
* <b>NOTE: </b>Since 3.8.0 the default value has changed from 1.5 to 1.6.
* Since 3.9.0 the default value has changed from 1.6 to 1.7
*/
@Parameter( property = "maven.compiler.target", defaultValue = DEFAULT_TARGET )
protected String target;
/**
* The -release argument for the Java compiler, supported since Java9
*
* @since 3.6
*/
@Parameter( property = "maven.compiler.release" )
protected String release;
/**
* The -encoding argument for the Java compiler.
*
* @since 2.1
*/
@Parameter( property = "encoding", defaultValue = "${project.build.sourceEncoding}" )
private String encoding;
/**
* Sets the granularity in milliseconds of the last modification
* date for testing whether a source needs recompilation.
*/
@Parameter( property = "lastModGranularityMs", defaultValue = "0" )
private int staleMillis;
/**
* The compiler id of the compiler to use. See this
* <a href="non-javac-compilers.html">guide</a> for more information.
*/
@Parameter( property = "maven.compiler.compilerId", defaultValue = "javac" )
private String compilerId;
/**
* Version of the compiler to use, ex. "1.3", "1.5", if {@link #fork} is set to <code>true</code>.
*/
@Parameter( property = "maven.compiler.compilerVersion" )
private String compilerVersion;
/**
* Allows running the compiler in a separate process.
* If <code>false</code> it uses the built in compiler, while if <code>true</code> it will use an executable.
*/
@Parameter( property = "maven.compiler.fork", defaultValue = "false" )
private boolean fork;
/**
* Initial size, in megabytes, of the memory allocation pool, ex. "64", "64m"
* if {@link #fork} is set to <code>true</code>.
*
* @since 2.0.1
*/
@Parameter( property = "maven.compiler.meminitial" )
private String meminitial;
/**
* Sets the maximum size, in megabytes, of the memory allocation pool, ex. "128", "128m"
* if {@link #fork} is set to <code>true</code>.
*
* @since 2.0.1
*/
@Parameter( property = "maven.compiler.maxmem" )
private String maxmem;
/**
* Sets the executable of the compiler to use when {@link #fork} is <code>true</code>.
*/
@Parameter( property = "maven.compiler.executable" )
private String executable;
/**
* <p>
* Sets whether annotation processing is performed or not. Only applies to JDK 1.6+
* If not set, both compilation and annotation processing are performed at the same time.
* </p>
* <p>Allowed values are:</p>
* <ul>
* <li><code>none</code> - no annotation processing is performed.</li>
* <li><code>only</code> - only annotation processing is done, no compilation.</li>
* </ul>
*
* @since 2.2
*/
@Parameter
private String proc;
/**
* <p>
* Names of annotation processors to run. Only applies to JDK 1.6+
* If not set, the default annotation processors discovery process applies.
* </p>
*
* @since 2.2
*/
@Parameter
private String[] annotationProcessors;
/**
* <p>
* Classpath elements to supply as annotation processor path. If specified, the compiler will detect annotation
* processors only in those classpath elements. If omitted, the default classpath is used to detect annotation
* processors. The detection itself depends on the configuration of {@code annotationProcessors}.
* </p>
* <p>
* Each classpath element is specified using their Maven coordinates (groupId, artifactId, version, classifier,
* type). Transitive dependencies are added automatically. Example:
* </p>
*
* <pre>
* <configuration>
* <annotationProcessorPaths>
* <path>
* <groupId>org.sample</groupId>
* <artifactId>sample-annotation-processor</artifactId>
* <version>1.2.3</version>
* </path>
* <!-- ... more ... -->
* </annotationProcessorPaths>
* </configuration>
* </pre>
*
* @since 3.5
*/
@Parameter
private List<DependencyCoordinate> annotationProcessorPaths;
/**
* <p>
* Sets the arguments to be passed to the compiler (prepending a dash).
* </p>
* <p>
* This is because the list of valid arguments passed to a Java compiler varies based on the compiler version.
* </p>
* <p>
* Note that {@code -J} options are only passed through if {@link #fork} is set to {@code true}.
* </p>
* <p>
* To pass <code>-Xmaxerrs 1000 -Xlint -Xlint:-path -Averbose=true</code> you should include the following:
* </p>
*
* <pre>
* <compilerArguments>
* <Xmaxerrs>1000</Xmaxerrs>
* <Xlint/>
* <Xlint:-path/>
* <Averbose>true</Averbose>
* </compilerArguments>
* </pre>
*
* @since 2.0.1
* @deprecated use {@link #compilerArgs} instead.
*/
@Parameter
@Deprecated
protected Map<String, String> compilerArguments;
/**
* <p>
* Sets the arguments to be passed to the compiler.
* </p>
* <p>
* Note that {@code -J} options are only passed through if {@link #fork} is set to {@code true}.
* </p>
* Example:
* <pre>
* <compilerArgs>
* <arg>-Xmaxerrs</arg>
* <arg>1000</arg>
* <arg>-Xlint</arg>
* <arg>-J-Duser.language=en_us</arg>
* </compilerArgs>
* </pre>
*
* @since 3.1
*/
@Parameter
protected List<String> compilerArgs;
/**
* <p>
* Sets the unformatted single argument string to be passed to the compiler. To pass multiple arguments such as
* <code>-Xmaxerrs 1000</code> (which are actually two arguments) you have to use {@link #compilerArguments}.
* </p>
* <p>
* This is because the list of valid arguments passed to a Java compiler varies based on the compiler version.
* </p>
* <p>
* Note that {@code -J} options are only passed through if {@link #fork} is set to {@code true}.
* </p>
*/
@Parameter
protected String compilerArgument;
/**
* Sets the name of the output file when compiling a set of
* sources to a single file.
* <p/>
* expression="${project.build.finalName}"
*/
@Parameter
private String outputFileName;
/**
* Keyword list to be appended to the <code>-g</code> command-line switch. Legal values are none or a
* comma-separated list of the following keywords: <code>lines</code>, <code>vars</code>, and <code>source</code>.
* If debug level is not specified, by default, nothing will be appended to <code>-g</code>.
* If debug is not turned on, this attribute will be ignored.
*
* @since 2.1
*/
@Parameter( property = "maven.compiler.debuglevel" )
private String debuglevel;
/**
*
*/
@Component
private ToolchainManager toolchainManager;
/**
* <p>
* Specify the requirements for this jdk toolchain for using a different {@code javac} than the one of the JRE used
* by Maven. This overrules the toolchain selected by the
* <a href="https://maven.apache.org/plugins/maven-toolchains-plugin/">maven-toolchain-plugin</a>.
* </p>
* (see <a href="https://maven.apache.org/guides/mini/guide-using-toolchains.html"> Guide to Toolchains</a> for more
* info)
*
* <pre>
* <configuration>
* <jdkToolchain>
* <version>11</version>
* </jdkToolchain>
* ...
* </configuration>
*
* <configuration>
* <jdkToolchain>
* <version>1.8</version>
* <vendor>zulu</vendor>
* </jdkToolchain>
* ...
* </configuration>
* </pre>
* <strong>note:</strong> requires at least Maven 3.3.1
*
* @since 3.6
*/
@Parameter
private Map<String, String> jdkToolchain;
// ----------------------------------------------------------------------
// Read-only parameters
// ----------------------------------------------------------------------
/**
* The directory to run the compiler from if fork is true.
*/
@Parameter( defaultValue = "${basedir}", required = true, readonly = true )
private File basedir;
/**
* The target directory of the compiler if fork is true.
*/
@Parameter( defaultValue = "${project.build.directory}", required = true, readonly = true )
private File buildDirectory;
/**
* Plexus compiler manager.
*/
@Component
private CompilerManager compilerManager;
/**
* The current build session instance. This is used for toolchain manager API calls.
*/
@Parameter( defaultValue = "${session}", readonly = true, required = true )
private MavenSession session;
/**
* The current project instance. This is used for propagating generated-sources paths as compile/testCompile source
* roots.
*/
@Parameter( defaultValue = "${project}", readonly = true, required = true )
private MavenProject project;
/**
* Strategy to re use javacc class created:
* <ul>
* <li><code>reuseCreated</code> (default): will reuse already created but in case of multi-threaded builds, each
* thread will have its own instance</li>
* <li><code>reuseSame</code>: the same Javacc class will be used for each compilation even for multi-threaded build
* </li>
* <li><code>alwaysNew</code>: a new Javacc class will be created for each compilation</li>
* </ul>
* Note this parameter value depends on the os/jdk you are using, but the default value should work on most of env.
*
* @since 2.5
*/
@Parameter( defaultValue = "${reuseCreated}", property = "maven.compiler.compilerReuseStrategy" )
private String compilerReuseStrategy = "reuseCreated";
/**
* @since 2.5
*/
@Parameter( defaultValue = "false", property = "maven.compiler.skipMultiThreadWarning" )
private boolean skipMultiThreadWarning;
/**
* compiler can now use javax.tools if available in your current jdk, you can disable this feature
* using -Dmaven.compiler.forceJavacCompilerUse=true or in the plugin configuration
*
* @since 3.0
*/
@Parameter( defaultValue = "false", property = "maven.compiler.forceJavacCompilerUse" )
private boolean forceJavacCompilerUse;
/**
* @since 3.0 needed for storing the status for the incremental build support.
*/
@Parameter( defaultValue = "${mojoExecution}", readonly = true, required = true )
private MojoExecution mojoExecution;
/**
* File extensions to check timestamp for incremental build.
* Default contains only <code>class</code> and <code>jar</code>.
*
* @since 3.1
*/
@Parameter
private List<String> fileExtensions;
/**
* <p>to enable/disable incremental compilation feature.</p>
* <p>This leads to two different modes depending on the underlying compiler. The default javac compiler does the
* following:</p>
* <ul>
* <li>true <strong>(default)</strong> in this mode the compiler plugin determines whether any JAR files the
* current module depends on have changed in the current build run; or any source file was added, removed or
* changed since the last compilation. If this is the case, the compiler plugin recompiles all sources.</li>
* <li>false <strong>(not recommended)</strong> this only compiles source files which are newer than their
* corresponding class files, namely which have changed since the last compilation. This does not
* recompile other classes which use the changed class, potentially leaving them with references to methods that no
* longer exist, leading to errors at runtime.</li>
* </ul>
*
* @since 3.1
*/
@Parameter( defaultValue = "true", property = "maven.compiler.useIncrementalCompilation" )
private boolean useIncrementalCompilation = true;
/**
* Package info source files that only contain javadoc and no annotation on the package
* can lead to no class file being generated by the compiler. This causes a file miss
* on the next compilations and forces an unnecessary recompilation. The default value
* of <code>true</code> causes an empty class file to be generated. This behavior can
* be changed by setting this parameter to <code>false</code>.
*
* @since 3.10
*/
@Parameter( defaultValue = "true", property = "maven.compiler.createMissingPackageInfoClass" )
private boolean createMissingPackageInfoClass = true;
/**
* Resolves the artifacts needed.
*/
@Component
private RepositorySystem repositorySystem;
/**
* Artifact handler manager.
*/
@Component
private ArtifactHandlerManager artifactHandlerManager;
/**
* Throws an exception on artifact resolution errors.
*/
@Component
private ResolutionErrorHandler resolutionErrorHandler;
protected abstract SourceInclusionScanner getSourceInclusionScanner( int staleMillis );
protected abstract SourceInclusionScanner getSourceInclusionScanner( String inputFileEnding );
protected abstract List<String> getClasspathElements();
protected abstract List<String> getModulepathElements();
protected abstract Map<String, JavaModuleDescriptor> getPathElements();
protected abstract List<String> getCompileSourceRoots();
protected abstract void preparePaths( Set<File> sourceFiles );
protected abstract File getOutputDirectory();
protected abstract String getSource();
protected abstract String getTarget();
protected abstract String getRelease();
protected abstract String getCompilerArgument();
protected abstract Map<String, String> getCompilerArguments();
protected abstract File getGeneratedSourcesDirectory();
protected abstract String getDebugFileName();
protected final MavenProject getProject()
{
return project;
}
private boolean targetOrReleaseSet;
@Override
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() + "'." );
}
//-----------toolchains start here ----------------------------------
//use the compilerId as identifier for toolchains as well.
Toolchain tc = getToolchain();
if ( tc != null )
{
getLog().info( "Toolchain in maven-compiler-plugin: " + tc );
if ( executable != null )
{
getLog().warn( "Toolchains are ignored, 'executable' parameter is set to " + executable );
}
else
{
fork = true;
//TODO somehow shaky dependency between compilerId and tool executable.
executable = tc.findTool( compilerId );
}
}
// ----------------------------------------------------------------------
//
// ----------------------------------------------------------------------
List<String> compileSourceRoots = removeEmptyCompileSourceRoots( getCompileSourceRoots() );
if ( compileSourceRoots.isEmpty() )
{
getLog().info( "No sources to compile" );
return;
}
// Verify that target or release is set
if ( !targetOrReleaseSet )
{
MessageBuilder mb = MessageUtils.buffer().a( "No explicit value set for target or release! " )
.a( "To ensure the same result even after upgrading this plugin, please add " ).newline()
.newline();
writePlugin( mb );
getLog().warn( mb.toString() );
}
// ----------------------------------------------------------------------
// Create the compiler configuration
// ----------------------------------------------------------------------
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setOutputLocation( getOutputDirectory().getAbsolutePath() );
compilerConfiguration.setOptimize( optimize );
compilerConfiguration.setDebug( debug );
compilerConfiguration.setDebugFileName( getDebugFileName() );
if ( debug && StringUtils.isNotEmpty( debuglevel ) )
{
String[] split = StringUtils.split( debuglevel, "," );
for ( String aSplit : split )
{
if ( !( aSplit.equalsIgnoreCase( "none" ) || aSplit.equalsIgnoreCase( "lines" )
|| aSplit.equalsIgnoreCase( "vars" ) || aSplit.equalsIgnoreCase( "source" ) ) )
{
throw new IllegalArgumentException( "The specified debug level: '" + aSplit + "' is unsupported. "
+ "Legal values are 'none', 'lines', 'vars', and 'source'." );
}
}
compilerConfiguration.setDebugLevel( debuglevel );
}
compilerConfiguration.setParameters( parameters );
compilerConfiguration.setVerbose( verbose );
compilerConfiguration.setShowWarnings( showWarnings );
compilerConfiguration.setFailOnWarning( failOnWarning );
compilerConfiguration.setShowDeprecation( showDeprecation );
compilerConfiguration.setSourceVersion( getSource() );
compilerConfiguration.setTargetVersion( getTarget() );
compilerConfiguration.setReleaseVersion( getRelease() );
compilerConfiguration.setProc( proc );
File generatedSourcesDirectory = getGeneratedSourcesDirectory();
compilerConfiguration.setGeneratedSourcesDirectory( generatedSourcesDirectory != null
? generatedSourcesDirectory.getAbsoluteFile() : null );
if ( generatedSourcesDirectory != null )
{
if ( !generatedSourcesDirectory.exists() )
{
generatedSourcesDirectory.mkdirs();
}
String generatedSourcesPath = generatedSourcesDirectory.getAbsolutePath();
compileSourceRoots.add( generatedSourcesPath );
if ( isTestCompile() )
{
getLog().debug( "Adding " + generatedSourcesPath + " to test-compile source roots:\n "
+ StringUtils.join( project.getTestCompileSourceRoots()
.iterator(), "\n " ) );
project.addTestCompileSourceRoot( generatedSourcesPath );
getLog().debug( "New test-compile source roots:\n "
+ StringUtils.join( project.getTestCompileSourceRoots()
.iterator(), "\n " ) );
}
else
{
getLog().debug( "Adding " + generatedSourcesPath + " to compile source roots:\n "
+ StringUtils.join( project.getCompileSourceRoots()
.iterator(), "\n " ) );
project.addCompileSourceRoot( generatedSourcesPath );
getLog().debug( "New compile source roots:\n " + StringUtils.join( project.getCompileSourceRoots()
.iterator(), "\n " ) );
}
}
compilerConfiguration.setSourceLocations( compileSourceRoots );
compilerConfiguration.setAnnotationProcessors( annotationProcessors );
compilerConfiguration.setProcessorPathEntries( resolveProcessorPathEntries() );
compilerConfiguration.setSourceEncoding( encoding );
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 );
if ( CompilerConfiguration.CompilerReuseStrategy.AlwaysNew.getStrategy().equals( this.compilerReuseStrategy ) )
{
compilerConfiguration.setCompilerReuseStrategy( CompilerConfiguration.CompilerReuseStrategy.AlwaysNew );
}
else if ( CompilerConfiguration.CompilerReuseStrategy.ReuseSame.getStrategy().equals(
this.compilerReuseStrategy ) )
{
if ( getRequestThreadCount() > 1 )
{
if ( !skipMultiThreadWarning )
{
getLog().warn( "You are in a multi-thread build and compilerReuseStrategy is set to reuseSame."
+ " This can cause issues in some environments (os/jdk)!"
+ " Consider using reuseCreated strategy."
+ System.getProperty( "line.separator" )
+ "If your env is fine with reuseSame, you can skip this warning with the "
+ "configuration field skipMultiThreadWarning "
+ "or -Dmaven.compiler.skipMultiThreadWarning=true" );
}
}
compilerConfiguration.setCompilerReuseStrategy( CompilerConfiguration.CompilerReuseStrategy.ReuseSame );
}
else
{
compilerConfiguration.setCompilerReuseStrategy( CompilerConfiguration.CompilerReuseStrategy.ReuseCreated );
}
getLog().debug( "CompilerReuseStrategy: " + compilerConfiguration.getCompilerReuseStrategy().getStrategy() );
compilerConfiguration.setForceJavacCompilerUse( forceJavacCompilerUse );
boolean canUpdateTarget;
IncrementalBuildHelper incrementalBuildHelper = new IncrementalBuildHelper( mojoExecution, session );
final Set<File> sources;
IncrementalBuildHelperRequest incrementalBuildHelperRequest = null;
if ( useIncrementalCompilation )
{
getLog().debug( "useIncrementalCompilation enabled" );
try
{
canUpdateTarget = compiler.canUpdateTarget( compilerConfiguration );
sources = getCompileSources( compiler, compilerConfiguration );
preparePaths( sources );
incrementalBuildHelperRequest = new IncrementalBuildHelperRequest().inputFiles( sources );
// CHECKSTYLE_OFF: LineLength
if ( ( compiler.getCompilerOutputStyle().equals( CompilerOutputStyle.ONE_OUTPUT_FILE_FOR_ALL_INPUT_FILES ) && !canUpdateTarget )
|| isDependencyChanged()
|| isSourceChanged( compilerConfiguration, compiler )
|| incrementalBuildHelper.inputFileTreeChanged( incrementalBuildHelperRequest ) )
// CHECKSTYLE_ON: LineLength
{
getLog().info( "Changes detected - recompiling the module!" );
compilerConfiguration.setSourceFiles( sources );
}
else
{
getLog().info( "Nothing to compile - all classes are up to date" );
return;
}
}
catch ( CompilerException e )
{
throw new MojoExecutionException( "Error while computing stale sources.", e );
}
}
else
{
getLog().debug( "useIncrementalCompilation disabled" );
Set<File> staleSources;
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 );
staleSources = computeStaleSources( compilerConfiguration, compiler,
getSourceInclusionScanner( inputFileEnding ) );
}
}
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;
}
compilerConfiguration.setSourceFiles( staleSources );
try
{
// MCOMPILER-366: if sources contain the module-descriptor it must be used to define the modulepath
sources = getCompileSources( compiler, compilerConfiguration );
if ( getLog().isDebugEnabled() )
{
getLog().debug( "#sources: " + sources.size() );
for ( File file : sources )
{
getLog().debug( file.getPath() );
}
}
preparePaths( sources );
}
catch ( CompilerException e )
{
throw new MojoExecutionException( "Error while computing stale sources.", e );
}
}
// Dividing pathElements of classPath and modulePath is based on sourceFiles
compilerConfiguration.setClasspathEntries( getClasspathElements() );
compilerConfiguration.setModulepathEntries( getModulepathElements() );
Map<String, String> effectiveCompilerArguments = getCompilerArguments();
String effectiveCompilerArgument = getCompilerArgument();
if ( ( effectiveCompilerArguments != null ) || ( effectiveCompilerArgument != null )
|| ( compilerArgs != null ) )
{
if ( effectiveCompilerArguments != null )
{
for ( Map.Entry<String, String> me : effectiveCompilerArguments.entrySet() )
{
String key = me.getKey();
String value = me.getValue();
if ( !key.startsWith( "-" ) )
{
key = "-" + key;
}
if ( key.startsWith( "-A" ) && StringUtils.isNotEmpty( value ) )
{
compilerConfiguration.addCompilerCustomArgument( key + "=" + value, null );
}
else
{
compilerConfiguration.addCompilerCustomArgument( key, value );
}
}
}
if ( !StringUtils.isEmpty( effectiveCompilerArgument ) )
{
compilerConfiguration.addCompilerCustomArgument( effectiveCompilerArgument, null );
}
if ( compilerArgs != null )
{
for ( String arg : compilerArgs )
{
compilerConfiguration.addCompilerCustomArgument( arg, null );
}
}
}
// ----------------------------------------------------------------------
// Dump configuration
// ----------------------------------------------------------------------
if ( getLog().isDebugEnabled() )
{
getLog().debug( "Classpath:" );
for ( String s : getClasspathElements() )
{
getLog().debug( " " + s );
}
if ( !getModulepathElements().isEmpty() )
{