Skip to content

Commit

Permalink
Explicitly enable the Lombok annotation processor on Java 23
Browse files Browse the repository at this point in the history
Closes gh-1654
  • Loading branch information
mhalbritter committed Dec 2, 2024
1 parent 94fbfd3 commit 4ff4c15
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<MavenBuild> {

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);
}

}
Original file line number Diff line number Diff line change
@@ -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("""
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>""");
}

@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("<annotationProcessorPaths>");
}

@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("<annotationProcessorPaths>");
}

}

0 comments on commit 4ff4c15

Please sign in to comment.