From 4ff4c15448a89eb04e36ecb4041eb0151dc69eaa Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 2 Dec 2024 10:53:35 +0100 Subject: [PATCH] Explicitly enable the Lombok annotation processor on Java 23 Closes gh-1654 --- ...endencyProjectGenerationConfiguration.java | 9 ++ .../lombok/LombokMavenBuildCustomizer.java | 84 +++++++++++++++++++ .../LombokMavenBuildCustomizerTests.java | 72 ++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizer.java create mode 100644 start-site/src/test/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizerTests.java diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/DependencyProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/DependencyProjectGenerationConfiguration.java index bc152085c6c..d0119c0d147 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/DependencyProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/DependencyProjectGenerationConfiguration.java @@ -17,6 +17,7 @@ package io.spring.start.site.extension.dependency; import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem; +import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem; import io.spring.initializr.generator.condition.ConditionalOnBuildSystem; import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency; import io.spring.initializr.generator.io.template.MustacheTemplateRenderer; @@ -25,6 +26,7 @@ import io.spring.initializr.metadata.InitializrMetadata; import io.spring.start.site.extension.dependency.liquibase.LiquibaseProjectContributor; import io.spring.start.site.extension.dependency.lombok.LombokGradleBuildCustomizer; +import io.spring.start.site.extension.dependency.lombok.LombokMavenBuildCustomizer; import io.spring.start.site.extension.dependency.mybatis.MyBatisTestBuildCustomizer; import io.spring.start.site.extension.dependency.okta.OktaHelpDocumentCustomizer; import io.spring.start.site.extension.dependency.reactor.ReactorTestBuildCustomizer; @@ -92,6 +94,13 @@ public LombokGradleBuildCustomizer lombokGradleBuildCustomizer() { return new LombokGradleBuildCustomizer(this.metadata); } + @Bean + @ConditionalOnBuildSystem(MavenBuildSystem.ID) + @ConditionalOnRequestedDependency("lombok") + public LombokMavenBuildCustomizer lombokMavenBuildCustomizer(ProjectDescription projectDescription) { + return new LombokMavenBuildCustomizer(this.metadata, projectDescription); + } + @Bean @ConditionalOnRequestedDependency("session") public SpringSessionBuildCustomizer springSessionBuildCustomizer() { diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizer.java new file mode 100644 index 00000000000..53ae2d211ab --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizer.java @@ -0,0 +1,84 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * 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 + * + * https://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 io.spring.start.site.extension.dependency.lombok; + +import io.spring.initializr.generator.buildsystem.maven.MavenBuild; +import io.spring.initializr.generator.language.java.JavaLanguage; +import io.spring.initializr.generator.project.ProjectDescription; +import io.spring.initializr.generator.spring.build.BuildCustomizer; +import io.spring.initializr.metadata.Dependency; +import io.spring.initializr.metadata.InitializrMetadata; + +/** + * {@link BuildCustomizer} for Lombok when using Maven. + * + * @author Moritz Halbritter + */ +public class LombokMavenBuildCustomizer implements BuildCustomizer { + + private final InitializrMetadata metadata; + + private final ProjectDescription projectDescription; + + public LombokMavenBuildCustomizer(InitializrMetadata metadata, ProjectDescription projectDescription) { + this.metadata = metadata; + this.projectDescription = projectDescription; + } + + @Override + public void customize(MavenBuild build) { + if (isAtLeastJava(23)) { + configureAnnotationProcessor(build); + } + } + + private void configureAnnotationProcessor(MavenBuild build) { + Dependency lombok = this.metadata.getDependencies().get("lombok"); + build.plugins().add("org.apache.maven.plugins", "maven-compiler-plugin", (plugin) -> { + plugin.configuration((configuration) -> { + configuration.add("annotationProcessorPaths", (annotationProcessorPaths) -> { + annotationProcessorPaths.add("path", (path) -> { + path.add("groupId", lombok.getGroupId()); + path.add("artifactId", lombok.getArtifactId()); + }); + }); + }); + }); + } + + private boolean isAtLeastJava(int version) { + if (!isJava()) { + return false; + } + return getJavaVersion() >= version; + } + + private int getJavaVersion() { + String javaVersion = this.projectDescription.getLanguage().jvmVersion(); + try { + return Integer.parseInt(javaVersion); + } + catch (NumberFormatException ex) { + return -1; + } + } + + private boolean isJava() { + return this.projectDescription.getLanguage().id().equals(JavaLanguage.ID); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizerTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizerTests.java new file mode 100644 index 00000000000..78e33c100a4 --- /dev/null +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizerTests.java @@ -0,0 +1,72 @@ +/* + * Copyright 2012-2024 the original author or authors. + * + * 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 + * + * https://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 io.spring.start.site.extension.dependency.lombok; + +import io.spring.initializr.generator.language.groovy.GroovyLanguage; +import io.spring.initializr.generator.language.kotlin.KotlinLanguage; +import io.spring.initializr.web.project.ProjectRequest; +import io.spring.start.site.extension.AbstractExtensionTests; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Tests for {@link LombokMavenBuildCustomizer}. + * + * @author Moritz Halbritter + */ +class LombokMavenBuildCustomizerTests extends AbstractExtensionTests { + + @ParameterizedTest + @ValueSource(ints = { 23, 24 }) + void shouldAddAnnotationProcessorsOnJava23AndUp(int javaVersion) { + ProjectRequest request = createProjectRequest("lombok"); + request.setJavaVersion(Integer.toString(javaVersion)); + assertThat(mavenPom(request)).containsIgnoringWhitespaces(""" + + org.apache.maven.plugins + maven-compiler-plugin + + + + org.projectlombok + lombok + + + + """); + } + + @ParameterizedTest + @ValueSource(ints = { 17, 18, 19, 20, 21, 22 }) + void shouldNotAddAnnotationProcessorsOnJavaBelow23(int javaVersion) { + ProjectRequest request = createProjectRequest("lombok"); + request.setJavaVersion(Integer.toString(javaVersion)); + assertThat(mavenPom(request)).doesNotContain(""); + } + + @ParameterizedTest + @ValueSource(strings = { GroovyLanguage.ID, KotlinLanguage.ID }) + void shouldNotAddAnnotationProcessorsOnNonJavaProjects(String language) { + ProjectRequest request = createProjectRequest("lombok"); + request.setJavaVersion("23"); + request.setLanguage(language); + assertThat(mavenPom(request)).doesNotContain(""); + } + +}