Skip to content

Commit f2e59af

Browse files
committed
Use spring-boot-kafka if Boot 4.0.x is selected
If Kafka Streams are selected, a dependency on kafka is added if using Spring Boot 4.0.0+ See gh-1861
1 parent cdec5b0 commit f2e59af

File tree

6 files changed

+105
-4
lines changed

6 files changed

+105
-4
lines changed

start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.spring.initializr.generator.buildsystem.gradle.GradleBuildSystem;
2121
import io.spring.initializr.generator.buildsystem.maven.MavenBuildSystem;
2222
import io.spring.initializr.generator.condition.ConditionalOnBuildSystem;
23+
import io.spring.initializr.generator.condition.ConditionalOnPlatformVersion;
2324
import io.spring.initializr.generator.condition.ConditionalOnRequestedDependency;
2425
import io.spring.initializr.generator.project.ProjectDescription;
2526
import io.spring.initializr.generator.project.ProjectGenerationConfiguration;
@@ -73,6 +74,13 @@ ServiceConnectionsCustomizer kafkaServiceConnectionsCustomizer(ProjectDescriptio
7374
};
7475
}
7576

77+
@Bean
78+
@ConditionalOnRequestedDependency("kafka-streams")
79+
@ConditionalOnPlatformVersion("4.0.0-M1")
80+
SpringKafkaStreamsBuildCustomizer springKafkaStreamsBuildCustomizer() {
81+
return new SpringKafkaStreamsBuildCustomizer();
82+
}
83+
7684
@Bean
7785
@ConditionalOnRequestedDependency("kafka-streams")
7886
@ConditionalOnBuildSystem(MavenBuildSystem.ID)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springkafka;
18+
19+
import io.spring.initializr.generator.buildsystem.Build;
20+
import io.spring.initializr.generator.spring.build.BuildCustomizer;
21+
22+
/**
23+
* Adds a dependency to Kafka.
24+
*
25+
* @author Moritz Halbritter
26+
*/
27+
class SpringKafkaStreamsBuildCustomizer implements BuildCustomizer<Build> {
28+
29+
@Override
30+
public void customize(Build build) {
31+
build.dependencies().add("kafka");
32+
}
33+
34+
}

start-site/src/main/resources/application.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,12 @@ initializr:
788788
- name: Spring for Apache Kafka
789789
id: kafka
790790
description: Publish, subscribe, store, and process streams of records.
791-
groupId: org.springframework.kafka
792-
artifactId: spring-kafka
791+
mappings:
792+
- compatibilityRange: "[3.4.0,4.0.0-M1)"
793+
group-id: org.springframework.kafka
794+
artifact-id: spring-kafka
795+
groupId: org.springframework.boot
796+
artifactId: spring-boot-kafka
793797
starter: false
794798
links:
795799
- rel: reference

start-site/src/test/java/io/spring/start/site/SupportedBootVersion.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public enum SupportedBootVersion {
3030
/**
3131
* 3.5.0.
3232
*/
33-
V3_5("3.5.0");
33+
V3_5("3.5.0"),
34+
/**
35+
* 4.0.0.
36+
*/
37+
V4_0("4.0.0-SNAPSHOT");
3438

3539
private final String version;
3640

start-site/src/test/java/io/spring/start/site/extension/AbstractExtensionTests.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ protected InitializrMetadata getMetadata() {
7171
}
7272

7373
protected Dependency getDependency(String id) {
74-
return getMetadata().getDependencies().get(id);
74+
return getDependency(SupportedBootVersion.latest(), id);
75+
}
76+
77+
protected Dependency getDependency(SupportedBootVersion bootVersion, String id) {
78+
return getMetadata().getDependencies().get(id).resolve(Version.parse(bootVersion.getVersion()));
7579
}
7680

7781
protected BillOfMaterials getBom(String id, String version) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2012 - present the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.spring.start.site.extension.dependency.springkafka;
18+
19+
import io.spring.initializr.metadata.Dependency;
20+
import io.spring.initializr.web.project.ProjectRequest;
21+
import io.spring.start.site.SupportedBootVersion;
22+
import io.spring.start.site.extension.AbstractExtensionTests;
23+
import org.junit.jupiter.api.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
/**
28+
* Tests for {@link SpringKafkaStreamsBuildCustomizer}.
29+
*
30+
* @author Moritz Halbritter
31+
*/
32+
class SpringKafkaStreamsBuildCustomizerTests extends AbstractExtensionTests {
33+
34+
@Test
35+
void shouldAddDependencyToKafkaIfBoot40IsUsed() {
36+
ProjectRequest request = createProjectRequest(SupportedBootVersion.V4_0, "kafka-streams");
37+
assertThat(mavenPom(request)).hasDependency(getDependency(SupportedBootVersion.V4_0, "kafka"));
38+
}
39+
40+
@Test
41+
void shouldNotAddDependencyToKafkaIfBootBefore40IsUsed() {
42+
ProjectRequest request = createProjectRequest(SupportedBootVersion.V3_5, "kafka-streams");
43+
Dependency kafka = getDependency(SupportedBootVersion.V3_5, "kafka");
44+
assertThat(mavenPom(request)).doesNotHaveDependency(kafka.getGroupId(), kafka.getArtifactId());
45+
}
46+
47+
}

0 commit comments

Comments
 (0)