Skip to content

Commit e79c132

Browse files
committed
Remove annotation processors from compile classpath
Lombok is an exception to this, it stays on the compile classpath, because it's both an annotation processor and a library. Closes gh-1989
1 parent bf08d81 commit e79c132

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

start-site/src/main/java/io/spring/start/site/extension/build/maven/RegisterAnnotationProcessorsBuildCustomizer.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package io.spring.start.site.extension.build.maven;
1818

19-
import java.util.List;
19+
import java.util.LinkedHashMap;
20+
import java.util.Map;
21+
import java.util.Set;
2022

2123
import io.spring.initializr.generator.buildsystem.Dependency;
2224
import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
@@ -32,6 +34,13 @@
3234
*/
3335
class RegisterAnnotationProcessorsBuildCustomizer implements BuildCustomizer<MavenBuild> {
3436

37+
/**
38+
* Some annotation processors must stay on the compile classpath, so that users can
39+
* use classes from them. Those dependencies are both annotation processors and
40+
* libraries.
41+
*/
42+
private static final Set<String> ANNOTATION_PROCESSORS_ON_COMPILE_CLASSPATH = Set.of("lombok");
43+
3544
private final InitializrMetadata metadata;
3645

3746
private final ProjectDescription projectDescription;
@@ -46,18 +55,28 @@ public void customize(MavenBuild build) {
4655
if (!isJava()) {
4756
return;
4857
}
49-
List<Dependency> annotationProcessors = build.dependencies()
50-
.ids()
51-
.filter(this::isAnnotationProcessor)
52-
.map((id) -> build.dependencies().get(id))
53-
.toList();
58+
Map<String, Dependency> annotationProcessors = getAnnotationProcessors(build);
5459
if (annotationProcessors.isEmpty()) {
5560
return;
5661
}
62+
configureOnMavenCompilerPlugin(build, annotationProcessors);
63+
removeFromDependencies(build, annotationProcessors);
64+
}
65+
66+
private Map<String, Dependency> getAnnotationProcessors(MavenBuild build) {
67+
Map<String, Dependency> annotationProcessors = new LinkedHashMap<>();
68+
build.dependencies()
69+
.ids()
70+
.filter(this::isAnnotationProcessor)
71+
.forEach((id) -> annotationProcessors.put(id, build.dependencies().get(id)));
72+
return annotationProcessors;
73+
}
74+
75+
private void configureOnMavenCompilerPlugin(MavenBuild build, Map<String, Dependency> annotationProcessors) {
5776
build.plugins().add("org.apache.maven.plugins", "maven-compiler-plugin", (plugin) -> {
5877
plugin.configuration((configuration) -> {
5978
configuration.add("annotationProcessorPaths", (annotationProcessorPaths) -> {
60-
for (Dependency annotationProcessor : annotationProcessors) {
79+
for (Dependency annotationProcessor : annotationProcessors.values()) {
6180
annotationProcessorPaths.add("path", (path) -> {
6281
path.add("groupId", annotationProcessor.getGroupId());
6382
path.add("artifactId", annotationProcessor.getArtifactId());
@@ -67,7 +86,14 @@ public void customize(MavenBuild build) {
6786
});
6887
});
6988
});
89+
}
7090

91+
private void removeFromDependencies(MavenBuild build, Map<String, Dependency> annotationProcessors) {
92+
annotationProcessors.keySet().forEach((id) -> {
93+
if (!ANNOTATION_PROCESSORS_ON_COMPILE_CLASSPATH.contains(id)) {
94+
build.dependencies().remove(id);
95+
}
96+
});
7197
}
7298

7399
private boolean isAnnotationProcessor(String id) {

start-site/src/test/java/io/spring/start/site/extension/build/maven/RegisterAnnotationProcessorsBuildCustomizerTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void shouldRegisterLombok() {
5252
</path>
5353
</annotationProcessorPaths>
5454
</configuration>
55-
</plugin>""");
55+
</plugin>""").hasDependency("org.projectlombok", "lombok");
5656
}
5757

5858
@Test
@@ -70,7 +70,7 @@ void shouldRegisterSpringBootConfigurationProcessor() {
7070
</path>
7171
</annotationProcessorPaths>
7272
</configuration>
73-
</plugin>""");
73+
</plugin>""").doesNotHaveDependency("org.springframework.boot", "spring-boot-configuration-processor");
7474
}
7575

7676
@Test
@@ -92,7 +92,9 @@ void shouldRegisterMultiple() {
9292
</path>
9393
</annotationProcessorPaths>
9494
</configuration>
95-
</plugin>""");
95+
</plugin>""")
96+
.hasDependency("org.projectlombok", "lombok")
97+
.doesNotHaveDependency("org.springframework.boot", "spring-boot-configuration-processor");
9698
}
9799

98100
@ParameterizedTest

0 commit comments

Comments
 (0)