From 8a982d89f3c0ffc5aa8bf2c53208e971425b0525 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Tue, 14 Jan 2025 13:30:51 +0100 Subject: [PATCH] Customize the Buildpacks builder when using kafka-streams If kafka-streams is selected, the builder is set to 'paketobuildpacks/builder-jammy-base:latest'. Closes gh-1695 --- ...ngKafkaProjectGenerationConfiguration.java | 27 +++++++++++- ...ringKafkaStreamsGradleBuildCustomizer.java | 44 +++++++++++++++++++ ...pringKafkaStreamsMavenBuildCustomizer.java | 38 ++++++++++++++++ ...kaProjectGenerationConfigurationTests.java | 39 +++++++++++++++- 4 files changed, 146 insertions(+), 2 deletions(-) create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsGradleBuildCustomizer.java create mode 100644 start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsMavenBuildCustomizer.java diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java index 3431c1bf67..32f57f6580 100644 --- a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2024 the original author or authors. + * Copyright 2012-2025 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. @@ -17,6 +17,9 @@ package io.spring.start.site.extension.dependency.springkafka; import io.spring.initializr.generator.buildsystem.Build; +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.project.ProjectDescription; import io.spring.initializr.generator.project.ProjectGenerationConfiguration; @@ -33,6 +36,7 @@ * * @author Stephane Nicoll * @author EddĂș MelĂ©ndez + * @author Moritz Halbritter */ @ProjectGenerationConfiguration class SpringKafkaProjectGenerationConfiguration { @@ -73,6 +77,27 @@ ServiceConnectionsCustomizer kafkaServiceConnectionsCustomizer(Build build, Dock }; } + @Bean + @ConditionalOnRequestedDependency("kafka-streams") + @ConditionalOnBuildSystem(MavenBuildSystem.ID) + SpringKafkaStreamsMavenBuildCustomizer springKafkaStreamsMavenBuildCustomizer() { + return new SpringKafkaStreamsMavenBuildCustomizer(); + } + + @Bean + @ConditionalOnRequestedDependency("kafka-streams") + @ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_GROOVY) + SpringKafkaStreamsGradleBuildCustomizer springKafkaStreamsGradleBuildCustomizer() { + return new SpringKafkaStreamsGradleBuildCustomizer('\''); + } + + @Bean + @ConditionalOnRequestedDependency("kafka-streams") + @ConditionalOnBuildSystem(id = GradleBuildSystem.ID, dialect = GradleBuildSystem.DIALECT_KOTLIN) + SpringKafkaStreamsGradleBuildCustomizer springKafkaStreamsGradleKotlinBuildCustomizer() { + return new SpringKafkaStreamsGradleBuildCustomizer('\"'); + } + private boolean isKafkaEnabled(Build build) { return build.dependencies().has("kafka") || build.dependencies().has("kafka-streams"); } diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsGradleBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsGradleBuildCustomizer.java new file mode 100644 index 0000000000..0436f0d87f --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsGradleBuildCustomizer.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2025 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.springkafka; + +import io.spring.initializr.generator.buildsystem.gradle.GradleBuild; +import io.spring.initializr.generator.spring.build.BuildCustomizer; + +/** + * {@link BuildCustomizer} for Gradle builds to configure the buildpack builder. + * + * @author Moritz Halbritter + */ +class SpringKafkaStreamsGradleBuildCustomizer implements BuildCustomizer { + + private static final String BUILDER = "paketobuildpacks/builder-jammy-base:latest"; + + private final char quote; + + SpringKafkaStreamsGradleBuildCustomizer(char quote) { + this.quote = quote; + } + + @Override + public void customize(GradleBuild build) { + build.tasks() + .customize("bootBuildImage", + (bootBuildImage) -> bootBuildImage.attribute("builder", this.quote + BUILDER + this.quote)); + } + +} diff --git a/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsMavenBuildCustomizer.java b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsMavenBuildCustomizer.java new file mode 100644 index 0000000000..1e04863486 --- /dev/null +++ b/start-site/src/main/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaStreamsMavenBuildCustomizer.java @@ -0,0 +1,38 @@ +/* + * Copyright 2012-2025 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.springkafka; + +import io.spring.initializr.generator.buildsystem.maven.MavenBuild; +import io.spring.initializr.generator.spring.build.BuildCustomizer; + +/** + * {@link BuildCustomizer} for Maven builds to configure the buildpack builder. + * + * @author Moritz Halbritter + */ +class SpringKafkaStreamsMavenBuildCustomizer implements BuildCustomizer { + + private static final String BUILDER = "paketobuildpacks/builder-jammy-base:latest"; + + @Override + public void customize(MavenBuild build) { + build.plugins() + .add("org.springframework.boot", "spring-boot-maven-plugin", (plugin) -> plugin.configuration( + (configuration) -> configuration.configure("image", (image) -> image.add("builder", BUILDER)))); + } + +} diff --git a/start-site/src/test/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfigurationTests.java b/start-site/src/test/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfigurationTests.java index cbf3adb08c..8642bda14e 100644 --- a/start-site/src/test/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfigurationTests.java +++ b/start-site/src/test/java/io/spring/start/site/extension/dependency/springkafka/SpringKafkaProjectGenerationConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -28,6 +28,7 @@ * * @author Wonwoo Lee * @author Stephane Nicoll + * @author Moritz Halbritter */ class SpringKafkaProjectGenerationConfigurationTests extends AbstractExtensionTests { @@ -49,4 +50,40 @@ void springKafkaTestIsNotAddedWithoutKafka() { .hasDependenciesSize(2); } + @Test + void customizesBuildpacksBuilderWhenUsingMavenAndKafkaStreams() { + ProjectRequest request = createProjectRequest("kafka-streams"); + assertThat(mavenPom(request)).containsIgnoringWhitespaces(""" + + org.springframework.boot + spring-boot-maven-plugin + + + paketobuildpacks/builder-jammy-base:latest + + + + """); + } + + @Test + void customizesBuildpacksBuilderWhenUsingGradleGroovyAndKafkaStreams() { + ProjectRequest request = createProjectRequest("kafka-streams"); + assertThat(gradleBuild(request)).containsIgnoringWhitespaces(""" + tasks.named('bootBuildImage') { + builder = 'paketobuildpacks/builder-jammy-base:latest' + } + """); + } + + @Test + void customizesBuildpacksBuilderWhenUsingGradleKotlinAndKafkaStreams() { + ProjectRequest request = createProjectRequest("kafka-streams"); + assertThat(gradleKotlinDslBuild(request)).containsIgnoringWhitespaces(""" + tasks.bootBuildImage { + builder = "paketobuildpacks/builder-jammy-base:latest" + } + """); + } + }