Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Configuration for Kotlin's All-Open Plugin for JPA Entities #1576

Closed
Closed
Show file tree
Hide file tree
Changes from 20 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*
* @author Stephane Nicoll
* @author Moritz Halbritter
* @author Sijun Yang
*/
public class BuildMetadataResolver {

Expand Down Expand Up @@ -65,4 +66,16 @@ public boolean hasFacet(Build build, String facet) {
return dependencies(build).anyMatch((dependency) -> dependency.getFacets().contains(facet));
}

/**
* Checks if the given {@link Build} contains dependencies with the given
* {@code groupId}.
* @param build the build to query
* @param groupId the groupId to query
* @return {@code true} if this build defines at least a dependency with that groupId,
* {@code false} otherwise
*/
public boolean hasGroupId(Build build, String groupId) {
return dependencies(build).anyMatch((dependency) -> dependency.getGroupId().equals(groupId));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* related dependency is present.
*
* @author Madhura Bhave
* @author Sijun Yang
*/
public class KotlinJpaGradleBuildCustomizer implements BuildCustomizer<GradleBuild> {

Expand All @@ -46,7 +47,29 @@ public void customize(GradleBuild build) {
if (this.buildMetadataResolver.hasFacet(build, "jpa")) {
build.plugins()
.add("org.jetbrains.kotlin.plugin.jpa", (plugin) -> plugin.setVersion(this.settings.getVersion()));
if (this.buildMetadataResolver.hasGroupId(build, "jakarta.persistence")) {
customizeAllOpenWithJakarta(build);
}
else if (this.buildMetadataResolver.hasGroupId(build, "javax.persistence")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support JavaEE anymore. Our baseline is Spring Boot 3.2, which already uses JakartaEE.

customizeAllOpenWithJavax(build);
}
}
}

private void customizeAllOpenWithJakarta(GradleBuild build) {
build.extensions().customize("allOpen", (allOpen) -> {
allOpen.invoke("annotation", "jakarta.persistence.Entity");
allOpen.invoke("annotation", "jakarta.persistence.MappedSuperclass");
allOpen.invoke("annotation", "jakarta.persistence.Embeddable");
});
}

private void customizeAllOpenWithJavax(GradleBuild build) {
build.extensions().customize("allOpen", (allOpen) -> {
allOpen.invoke("annotation", "javax.persistence.Entity");
allOpen.invoke("annotation", "javax.persistence.MappedSuperclass");
allOpen.invoke("annotation", "javax.persistence.Embeddable");
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.spring.initializr.generator.spring.code.kotlin;

import io.spring.initializr.generator.buildsystem.maven.MavenBuild;
import io.spring.initializr.generator.buildsystem.maven.MavenPlugin;
import io.spring.initializr.generator.project.ProjectDescription;
import io.spring.initializr.generator.spring.build.BuildCustomizer;
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
Expand All @@ -28,6 +29,7 @@
*
* @author Madhura Bhave
* @author Sebastien Deleuze
* @author Sijun Yang
*/
public class KotlinJpaMavenBuildCustomizer implements BuildCustomizer<MavenBuild> {

Expand All @@ -41,11 +43,31 @@ public KotlinJpaMavenBuildCustomizer(InitializrMetadata metadata, ProjectDescrip
public void customize(MavenBuild build) {
if (this.buildMetadataResolver.hasFacet(build, "jpa")) {
build.plugins().add("org.jetbrains.kotlin", "kotlin-maven-plugin", (kotlinPlugin) -> {
kotlinPlugin.configuration((configuration) -> configuration.configure("compilerPlugins",
(compilerPlugins) -> compilerPlugins.add("plugin", "jpa")));
kotlinPlugin.configuration((configuration) -> {
configuration.configure("compilerPlugins",
(compilerPlugins) -> compilerPlugins.add("plugin", "jpa"));
if (this.buildMetadataResolver.hasGroupId(build, "jakarta.persistence")) {
customizeAllOpenWithJakarta(configuration);
}
else if (this.buildMetadataResolver.hasGroupId(build, "javax.persistence")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support JavaEE anymore.

customizeAllOpenWithJavax(configuration);
}
});
kotlinPlugin.dependency("org.jetbrains.kotlin", "kotlin-maven-noarg", "${kotlin.version}");
});
}
}

private void customizeAllOpenWithJakarta(MavenPlugin.ConfigurationBuilder option) {
option.add("option", "all-open:annotation=jakarta.persistence.Entity");
option.add("option", "all-open:annotation=jakarta.persistence.MappedSuperclass");
option.add("option", "all-open:annotation=jakarta.persistence.Embeddable");
}

private void customizeAllOpenWithJavax(MavenPlugin.ConfigurationBuilder option) {
option.add("option", "all-open:annotation=javax.persistence.Entity");
option.add("option", "all-open:annotation=javax.persistence.MappedSuperclass");
option.add("option", "all-open:annotation=javax.persistence.Embeddable");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.spring.initializr.generator.spring.code.kotlin;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import io.spring.initializr.generator.buildsystem.gradle.GradleBuild;
import io.spring.initializr.generator.buildsystem.gradle.GradlePlugin;
Expand All @@ -35,6 +37,7 @@
* Tests for {@link KotlinJpaGradleBuildCustomizer}.
*
* @author Madhura Bhave
* @author Sijun Yang
*/
class KotlinJpaGradleBuildCustomizerTests {

Expand All @@ -58,6 +61,41 @@ void customizeWhenJpaFacetAbsentShouldNotAddKotlinJpaPlugin() {
assertThat(build.plugins().values()).isEmpty();
}

@Test
void customizeWhenJakartaPersistencePresentShouldCustomizeAllOpenWithJakarta() {
Dependency dependency = Dependency.withId("foo", "jakarta.persistence", "jakarta.persistence-api");
dependency.setFacets(Collections.singletonList("jpa"));
GradleBuild build = getCustomizedBuild(dependency);
assertThat(build.plugins().values()).singleElement()
.satisfies((plugin) -> assertThat(plugin.getId()).isEqualTo("org.jetbrains.kotlin.plugin.jpa"));
assertThat(build.extensions().values()).singleElement().satisfies((extension) -> {
assertThat(extension.getName()).isEqualTo("allOpen");
assertThat(extension.getInvocations())
.filteredOn((invocation) -> Objects.equals(invocation.getTarget(), "annotation"))
.extracting("arguments")
.containsExactlyInAnyOrder(List.of("jakarta.persistence.Entity"),
List.of("jakarta.persistence.MappedSuperclass"), List.of("jakarta.persistence.Embeddable"));
});
}

@Test
void customizeWhenJavaxPersistencePresentShouldCustomizeAllOpenWithJavax() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to support JavaEE anymore.

Dependency dependency = Dependency.withId("foo", "javax.persistence", "javax.persistence-api");
dependency.setFacets(Collections.singletonList("jpa"));
GradleBuild build = getCustomizedBuild(dependency);
assertThat(build.plugins().values()).singleElement()
.satisfies((plugin) -> assertThat(plugin.getId()).isEqualTo("org.jetbrains.kotlin.plugin.jpa"));
assertThat(build.extensions().values()).singleElement().satisfies((extension) -> {
assertThat(extension.getName()).isEqualTo("allOpen");
assertThat(extension.getInvocations())
.filteredOn((invocation) -> Objects.equals(invocation.getTarget(), "annotation"))
.extracting("arguments")
.containsExactlyInAnyOrder(List.of("javax.persistence.Entity"),
List.of("javax.persistence.MappedSuperclass"), List.of("javax.persistence.Embeddable"));
});

}

private GradleBuild getCustomizedBuild(Dependency dependency) {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("test", dependency)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2023 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.
Expand Down Expand Up @@ -31,12 +31,6 @@

import static org.assertj.core.api.Assertions.assertThat;

/**
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove this?

* Tests for {@link KotlinJpaMavenBuildCustomizer}.
*
* @author Madhura Bhave
* @author Sebastien Deleuze
*/
class KotlinJpaMavenBuildCustomizerTests {

@Test
Expand Down Expand Up @@ -67,6 +61,48 @@ void customizeWhenJpaFacetAbsentShouldNotAddKotlinJpaPlugin() {
assertThat(build.plugins().isEmpty()).isTrue();
}

@Test
void customizeWhenJakartaPersistencePresentShouldCustomizeAllOpenWithJakarta() {
Dependency dependency = Dependency.withId("foo", "jakarta.persistence", "jakarta.persistence-api");
dependency.setFacets(Collections.singletonList("jpa"));
MavenBuild build = getCustomizedBuild(dependency);
assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> {
assertThat(plugin.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(plugin.getArtifactId()).isEqualTo("kotlin-maven-plugin");
});
assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> {
MavenPlugin.Configuration configuration = plugin.getConfiguration();
assertThat(configuration.getSettings()
.stream()
.filter((setting) -> "option".equals(setting.getName()))
.map(MavenPlugin.Setting::getValue))
.containsExactlyInAnyOrder("all-open:annotation=jakarta.persistence.Entity",
"all-open:annotation=jakarta.persistence.MappedSuperclass",
"all-open:annotation=jakarta.persistence.Embeddable");
});
}

@Test
void customizeWhenJavaxPersistencePresentShouldCustomizeAllOpenWithJavax() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to support JavaEE anymore.

Dependency dependency = Dependency.withId("foo", "javax.persistence", "javax.persistence-api");
dependency.setFacets(Collections.singletonList("jpa"));
MavenBuild build = getCustomizedBuild(dependency);
assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> {
assertThat(plugin.getGroupId()).isEqualTo("org.jetbrains.kotlin");
assertThat(plugin.getArtifactId()).isEqualTo("kotlin-maven-plugin");
});
assertThat(build.plugins().values()).singleElement().satisfies((plugin) -> {
MavenPlugin.Configuration configuration = plugin.getConfiguration();
assertThat(configuration.getSettings()
.stream()
.filter((setting) -> "option".equals(setting.getName()))
.map(MavenPlugin.Setting::getValue))
.containsExactlyInAnyOrder("all-open:annotation=javax.persistence.Entity",
"all-open:annotation=javax.persistence.MappedSuperclass",
"all-open:annotation=javax.persistence.Embeddable");
});
}

private MavenBuild getCustomizedBuild(Dependency dependency) {
InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults()
.addDependencyGroup("test", dependency)
Expand Down