Skip to content

Commit c7535b5

Browse files
committed
More conservative default value of the <incrementalCompilation> parameter if an annotation processor is present.
1 parent 7bac7a5 commit c7535b5

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

src/main/java/org/apache/maven/plugin/compiler/AbstractCompilerMojo.java

+76-11
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ final Charset charset() {
289289
* </ul>
290290
*
291291
* Prior Java 21, {@code full} was the default.
292-
* Starting with JDK 21, this option must be set explicitly.
292+
* Starting with Java 21, the default is {@code none} unless another processor option is used.
293293
*
294294
* @see #annotationProcessors
295295
* @see <a href="https://docs.oracle.com/en/java/javase/17/docs/specs/man/javac.html#option-proc">javac -proc</a>
@@ -363,7 +363,12 @@ final Charset charset() {
363363
* </p>
364364
*
365365
* @since 3.12.0
366+
*
367+
* @deprecated This flag is ignored.
368+
* Replaced by ordinary dependencies with {@code <type>} element set to
369+
* {@code processor}, {@code classpath-processor} or {@code modular-processor}.
366370
*/
371+
@Deprecated(since = "4.0.0")
367372
@Parameter(defaultValue = "false")
368373
protected boolean annotationProcessorPathsUseDepMgmt;
369374

@@ -458,7 +463,7 @@ final Charset charset() {
458463

459464
/**
460465
* Whether to provide more details about why a module is rebuilt.
461-
* This is used only if {@link #incrementalCompilation} is {@code "inputTreeChanges"}.
466+
* This is used only if {@link #incrementalCompilation} is set to something else than {@code "none"}.
462467
*
463468
* @see #incrementalCompilation
464469
*/
@@ -586,13 +591,23 @@ final Charset charset() {
586591
* In all cases, the current compiler-plugin does not detect structural changes other than file addition or removal.
587592
* For example, the plugin does not detect whether a method has been removed in a class.
588593
*
594+
* <h4>Default value</h4>
595+
* The default value depends on the context.
596+
* If there is no annotation processor, then the default is {@code "options,dependencies,sources"}.
597+
* It means that a full rebuild will be done if the compiler options or the dependencies changed,
598+
* or if a source file has been deleted. Otherwise, only the modified source files will be recompiled.
599+
*
600+
* <p>If an annotation processor is present (e.g., {@link #proc} set to a value other than {@code "none"}),
601+
* then the default value is same as above with the addition of {@code "rebuild-on-add,rebuild-on-change"}.
602+
* It means that a full rebuild will be done if any kind of change is detected.</p>
603+
*
589604
* @see #staleMillis
590605
* @see #fileExtensions
591606
* @see #showCompilationChanges
592607
* @see #createMissingPackageInfoClass
593608
* @since 4.0.0
594609
*/
595-
@Parameter(defaultValue = "options,dependencies,sources")
610+
@Parameter // The default values are implemented in `incrementalCompilationConfiguration()`.
596611
protected String incrementalCompilation;
597612

598613
/**
@@ -610,18 +625,30 @@ final Charset charset() {
610625

611626
/**
612627
* Returns the configuration of the incremental compilation.
613-
* This method may be removed in a future version if the deprecated parameter is removed.
628+
* If the argument is null or blank, then this method applies
629+
* the default values documented in {@link #incrementalCompilation} javadoc.
614630
*
615631
* @throws MojoException if a value is not recognized, or if mutually exclusive values are specified
616632
*/
617633
final EnumSet<IncrementalBuild.Aspect> incrementalCompilationConfiguration() {
618-
if (useIncrementalCompilation != null) {
619-
return useIncrementalCompilation
620-
? EnumSet.of(
621-
IncrementalBuild.Aspect.DEPENDENCIES,
622-
IncrementalBuild.Aspect.SOURCES,
623-
IncrementalBuild.Aspect.REBUILD_ON_ADD)
624-
: EnumSet.of(IncrementalBuild.Aspect.CLASSES);
634+
if (incrementalCompilation == null || incrementalCompilation.isBlank()) {
635+
if (useIncrementalCompilation != null) {
636+
return useIncrementalCompilation
637+
? EnumSet.of(
638+
IncrementalBuild.Aspect.DEPENDENCIES,
639+
IncrementalBuild.Aspect.SOURCES,
640+
IncrementalBuild.Aspect.REBUILD_ON_ADD)
641+
: EnumSet.of(IncrementalBuild.Aspect.CLASSES);
642+
}
643+
var aspects = EnumSet.of(
644+
IncrementalBuild.Aspect.OPTIONS,
645+
IncrementalBuild.Aspect.DEPENDENCIES,
646+
IncrementalBuild.Aspect.SOURCES);
647+
if (hasAnnotationProcessor()) {
648+
aspects.add(IncrementalBuild.Aspect.REBUILD_ON_ADD);
649+
aspects.add(IncrementalBuild.Aspect.REBUILD_ON_CHANGE);
650+
}
651+
return aspects;
625652
} else {
626653
return IncrementalBuild.Aspect.parse(incrementalCompilation);
627654
}
@@ -1502,6 +1529,44 @@ final void resolveProcessorPathEntries(Map<PathType, List<Path>> addTo) throws M
15021529
}
15031530
}
15041531

1532+
/**
1533+
* {@return whether an annotation processor seems to be present}.
1534+
* This method is invoked if the user did not specified explicit incremental compilation options.
1535+
*
1536+
* @see #incrementalCompilation
1537+
*/
1538+
private boolean hasAnnotationProcessor() {
1539+
if ("none".equalsIgnoreCase(proc)) {
1540+
return false;
1541+
}
1542+
if (proc == null || proc.isBlank()) {
1543+
/*
1544+
* If the `proc` parameter was not specified, its default value depends on the Java version.
1545+
* It was "full" prior Java 21 and become "none if no other processor option" since Java 21.
1546+
* Since even the full" case may do nothing, always check if a processor is declared.
1547+
*/
1548+
if (annotationProcessors == null || annotationProcessors.length == 0) {
1549+
if (annotationProcessorPaths == null || annotationProcessorPaths.isEmpty()) {
1550+
DependencyResolver resolver = session.getService(DependencyResolver.class);
1551+
if (resolver == null) { // Null value happen during tests, depending on the mock used.
1552+
return false;
1553+
}
1554+
var allowedTypes = EnumSet.of(JavaPathType.PROCESSOR_CLASSES, JavaPathType.PROCESSOR_MODULES);
1555+
DependencyResolverResult dependencies = resolver.resolve(DependencyResolverRequest.builder()
1556+
.session(session)
1557+
.project(project)
1558+
.requestType(DependencyResolverRequest.RequestType.COLLECT)
1559+
.pathScope(compileScope)
1560+
.pathTypeFilter(allowedTypes)
1561+
.build());
1562+
1563+
return !dependencies.getDependencies().isEmpty();
1564+
}
1565+
}
1566+
}
1567+
return true;
1568+
}
1569+
15051570
/**
15061571
* Ensures that the directory for generated sources exists, and adds it to the list of source directories
15071572
* known to the project manager. This is used for adding the output of annotation processor.

src/main/java/org/apache/maven/plugin/compiler/IncrementalBuild.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public String toString() {
163163
* Parses a comma-separated list of aspects.
164164
*
165165
* @param values the plugin parameter to parse as a comma-separated list
166-
* @return the aspect
166+
* @return the aspects which, when modified, should cause a partial or full rebuild
167167
* @throws MojoException if a value is not recognized, or if mutually exclusive values are specified
168168
*/
169169
static EnumSet<Aspect> parse(final String values) {

0 commit comments

Comments
 (0)