-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Explicitly enable the Lombok annotation processor on Java 23
Closes gh-1654
- Loading branch information
1 parent
94fbfd3
commit 4ff4c15
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...ain/java/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...ava/io/spring/start/site/extension/dependency/lombok/LombokMavenBuildCustomizerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>"); | ||
} | ||
|
||
} |