Skip to content

Commit 70e7582

Browse files
committed
Fix Jackson module groupId added to dependencies
Starting at Spring Boot 4, the Jackson Kotlin module added to the dependencies of the project should use the `tools.jackson.module` groupId rather than `com.fasterxml.jackson.module`. Signed-off-by: jnizet <jb@ninja-squad.com>
1 parent 1de397c commit 70e7582

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

initializr-generator-spring/src/main/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJacksonBuildCustomizer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.spring.initializr.generator.project.ProjectDescription;
2323
import io.spring.initializr.generator.spring.build.BuildCustomizer;
2424
import io.spring.initializr.generator.spring.build.BuildMetadataResolver;
25+
import io.spring.initializr.generator.version.VersionParser;
26+
import io.spring.initializr.generator.version.VersionRange;
2527
import io.spring.initializr.metadata.InitializrMetadata;
2628

2729
import org.springframework.util.ClassUtils;
@@ -39,6 +41,8 @@ public class KotlinJacksonBuildCustomizer implements BuildCustomizer<Build> {
3941

4042
private final ProjectDescription description;
4143

44+
private static final VersionRange SPRING_BOOT_4_OR_LATER = VersionParser.DEFAULT.parseRange("4.0.0");
45+
4246
public KotlinJacksonBuildCustomizer(InitializrMetadata metadata, ProjectDescription description) {
4347
this.buildMetadataResolver = new BuildMetadataResolver(metadata, description.getPlatformVersion());
4448
this.description = description;
@@ -48,9 +52,10 @@ public KotlinJacksonBuildCustomizer(InitializrMetadata metadata, ProjectDescript
4852
public void customize(Build build) {
4953
boolean isKotlin = ClassUtils.isAssignableValue(KotlinLanguage.class, this.description.getLanguage());
5054
if (this.buildMetadataResolver.hasFacet(build, "json") && isKotlin) {
55+
String groupId = SPRING_BOOT_4_OR_LATER.match(this.description.getPlatformVersion())
56+
? "tools.jackson.module" : "com.fasterxml.jackson.module";
5157
build.dependencies()
52-
.add("jackson-module-kotlin", "com.fasterxml.jackson.module", "jackson-module-kotlin",
53-
DependencyScope.COMPILE);
58+
.add("jackson-module-kotlin", groupId, "jackson-module-kotlin", DependencyScope.COMPILE);
5459
}
5560
}
5661

initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinJacksonBuildCustomizerTests.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,33 @@
4141
class KotlinJacksonBuildCustomizerTests {
4242

4343
@Test
44-
void customizeWhenJsonFacetPresentShouldAddJacksonKotlinModule() {
44+
void customizeWhenJsonFacetPresentShouldAddJackson2KotlinModuleBeforeSpringBoot4() {
4545
Dependency dependency = Dependency.withId("foo");
4646
dependency.setFacets(Collections.singletonList("json"));
4747
MutableProjectDescription description = new MutableProjectDescription();
4848
description.setLanguage(new KotlinLanguage());
49+
description.setPlatformVersion(Version.parse("3.5.2"));
4950
MavenBuild build = getCustomizedBuild(dependency, description);
5051
io.spring.initializr.generator.buildsystem.Dependency jacksonKotlin = build.dependencies()
5152
.get("jackson-module-kotlin");
5253
assertThat(jacksonKotlin.getArtifactId()).isEqualTo("jackson-module-kotlin");
5354
assertThat(jacksonKotlin.getGroupId()).isEqualTo("com.fasterxml.jackson.module");
5455
}
5556

57+
@Test
58+
void customizeWhenJsonFacetPresentShouldAddJackson3KotlinModuleStrtingAtSpringBoot4() {
59+
Dependency dependency = Dependency.withId("foo");
60+
dependency.setFacets(Collections.singletonList("json"));
61+
MutableProjectDescription description = new MutableProjectDescription();
62+
description.setLanguage(new KotlinLanguage());
63+
description.setPlatformVersion(Version.parse("4.0.0"));
64+
MavenBuild build = getCustomizedBuild(dependency, description);
65+
io.spring.initializr.generator.buildsystem.Dependency jacksonKotlin = build.dependencies()
66+
.get("jackson-module-kotlin");
67+
assertThat(jacksonKotlin.getArtifactId()).isEqualTo("jackson-module-kotlin");
68+
assertThat(jacksonKotlin.getGroupId()).isEqualTo("tools.jackson.module");
69+
}
70+
5671
@Test
5772
void jacksonModuleKotlinIsNotAddedWithoutKotlin() {
5873
Dependency dependency = Dependency.withId("foo");

initializr-generator-spring/src/test/java/io/spring/initializr/generator/spring/code/kotlin/KotlinProjectGenerationConfigurationTests.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,27 @@ void servletInitializerIsContributedWhenGeneratingProjectThatUsesWarPackaging()
122122
}
123123

124124
@Test
125-
void jacksonKotlinModuleShouldBeAddedWhenJsonFacetPresent() {
125+
void jackson2KotlinModuleShouldBeAddedWhenJsonFacetPresentBeforeSpringBoot4() {
126126
MutableProjectDescription description = new MutableProjectDescription();
127+
description.setPlatformVersion(Version.parse("3.5.2"));
127128
description.addDependency("foo", Dependency.withCoordinates("com.example", "foo"));
128129
ProjectStructure project = this.projectTester.generate(description);
129130
assertThat(project).textFile("pom.xml")
130131
.contains(" <dependency>", " <groupId>com.fasterxml.jackson.module</groupId>",
131132
" <artifactId>jackson-module-kotlin</artifactId>", " </dependency>");
132133
}
133134

135+
@Test
136+
void jackson3KotlinModuleShouldBeAddedWhenJsonFacetPresentStartingAtSpringBoot4() {
137+
MutableProjectDescription description = new MutableProjectDescription();
138+
description.setPlatformVersion(Version.parse("4.0.0"));
139+
description.addDependency("foo", Dependency.withCoordinates("com.example", "foo"));
140+
ProjectStructure project = this.projectTester.generate(description);
141+
assertThat(project).textFile("pom.xml")
142+
.contains(" <dependency>", " <groupId>tools.jackson.module</groupId>",
143+
" <artifactId>jackson-module-kotlin</artifactId>", " </dependency>");
144+
}
145+
134146
@Test
135147
void addsKotlinGradlePluginGitIgnoreEntry() {
136148
MutableProjectDescription description = new MutableProjectDescription();

0 commit comments

Comments
 (0)