Skip to content

Commit 5992b77

Browse files
authored
Build fails when annotation processor list is empty (but present) (#1077)
Fixes #892
1 parent acccef7 commit 5992b77

4 files changed

Lines changed: 95 additions & 1 deletion

File tree

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,7 +884,7 @@ private void executeReal() throws MojoExecutionException, CompilationFailureExce
884884

885885
compilerConfiguration.setSourceLocations(compileSourceRoots);
886886

887-
compilerConfiguration.setAnnotationProcessors(annotationProcessors);
887+
compilerConfiguration.setAnnotationProcessors(normalizeAnnotationProcessors(annotationProcessors));
888888

889889
compilerConfiguration.setProcessorPathEntries(resolveProcessorPathEntries());
890890

@@ -1631,6 +1631,21 @@ private static List<String> removeEmptyCompileSourceRoots(List<String> compileSo
16311631
return newCompileSourceRootsList;
16321632
}
16331633

1634+
/**
1635+
* Removes blank annotation processor names.
1636+
*
1637+
* @param processors the configured annotation processor names, or {@code null}.
1638+
* @return the non-blank annotation processor names, or {@code null} if none remain.
1639+
*/
1640+
static String[] normalizeAnnotationProcessors(String[] processors) {
1641+
if (processors != null) {
1642+
processors = Arrays.stream(processors)
1643+
.filter(processor -> !StringUtils.isBlank(processor))
1644+
.toArray(String[]::new);
1645+
}
1646+
return processors == null || processors.length == 0 ? null : processors;
1647+
}
1648+
16341649
/**
16351650
* We just compare the timestamps of all local dependency files (inter-module dependency classpath) and the own
16361651
* generated classes and if we got a file which is &gt;= the build-started timestamp, then we caught a file which

src/test/java/org/apache/maven/plugin/compiler/CompilerMojoTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import static org.apache.maven.api.plugin.testing.MojoExtension.setVariableValueToObject;
3636
import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenProject;
3737
import static org.apache.maven.plugin.compiler.MojoTestUtils.getMockMavenSession;
38+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertFalse;
4041
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -80,6 +81,27 @@ void testCompilerBasic(CompilerMojo compilerMojo) throws Exception {
8081
assertTrue(testClass::exists);
8182
}
8283

84+
@Test
85+
@InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-empty-annotation-processors-test/plugin-config.xml")
86+
void testCompilerEmptyAnnotationProcessors(CompilerMojo compilerMojo) throws Exception {
87+
setUpCompilerMojoTestEnv(compilerMojo);
88+
89+
compilerMojo.execute();
90+
91+
File testClass = new File(compilerMojo.getOutputDirectory(), "TestCompile.class");
92+
assertTrue(testClass::exists);
93+
}
94+
95+
@Test
96+
void testNormalizeAnnotationProcessors() {
97+
assertNull(AbstractCompilerMojo.normalizeAnnotationProcessors(null));
98+
assertNull(AbstractCompilerMojo.normalizeAnnotationProcessors(new String[] {"", " "}));
99+
assertArrayEquals(
100+
new String[] {"com.example.First", "com.example.Second"},
101+
AbstractCompilerMojo.normalizeAnnotationProcessors(
102+
new String[] {"", "com.example.First", " ", "com.example.Second"}));
103+
}
104+
83105
@Test
84106
@InjectMojo(goal = COMPILE, pom = "classpath:/unit/compiler-basic-sourcetarget/plugin-config.xml")
85107
void testCompilerBasicSourceTarget(CompilerMojo compilerMojo) throws Exception {
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!--
2+
~ Licensed to the Apache Software Foundation (ASF) under one
3+
~ or more contributor license agreements. See the NOTICE file
4+
~ distributed with this work for additional information
5+
~ regarding copyright ownership. The ASF licenses this file
6+
~ to you under the Apache License, Version 2.0 (the
7+
~ "License"); you may not use this file except in compliance
8+
~ with the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing,
13+
~ software distributed under the License is distributed on an
14+
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
~ KIND, either express or implied. See the License for the
16+
~ specific language governing permissions and limitations
17+
~ under the License.
18+
-->
19+
20+
<project>
21+
<build>
22+
<plugins>
23+
<plugin>
24+
<artifactId>maven-compiler-plugin</artifactId>
25+
<configuration>
26+
<compileSourceRoots>
27+
<compileSourceRoot>${basedir}/target/test-classes/unit/compiler-empty-annotation-processors-test/src/main/java</compileSourceRoot>
28+
</compileSourceRoots>
29+
<annotationProcessors>
30+
</annotationProcessors>
31+
<compilerId>javac</compilerId>
32+
<outputDirectory>${basedir}/target/test/unit/compiler-empty-annotation-processors-test/target/classes</outputDirectory>
33+
<buildDirectory>${basedir}/target/test/unit/compiler-empty-annotation-processors-test/target</buildDirectory>
34+
</configuration>
35+
</plugin>
36+
</plugins>
37+
</build>
38+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
public class TestCompile {}

0 commit comments

Comments
 (0)