Skip to content

Commit

Permalink
Customize the Buildpacks builder when using kafka-streams
Browse files Browse the repository at this point in the history
If kafka-streams is selected, the builder is set to 'paketobuildpacks/builder-jammy-base:latest'.

Closes gh-1695
  • Loading branch information
mhalbritter committed Jan 14, 2025
1 parent bcf8956 commit 8a982d8
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -33,6 +36,7 @@
*
* @author Stephane Nicoll
* @author Eddú Meléndez
* @author Moritz Halbritter
*/
@ProjectGenerationConfiguration
class SpringKafkaProjectGenerationConfiguration {
Expand Down Expand Up @@ -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");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GradleBuild> {

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));
}

}
Original file line number Diff line number Diff line change
@@ -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<MavenBuild> {

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))));
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -28,6 +28,7 @@
*
* @author Wonwoo Lee
* @author Stephane Nicoll
* @author Moritz Halbritter
*/
class SpringKafkaProjectGenerationConfigurationTests extends AbstractExtensionTests {

Expand All @@ -49,4 +50,40 @@ void springKafkaTestIsNotAddedWithoutKafka() {
.hasDependenciesSize(2);
}

@Test
void customizesBuildpacksBuilderWhenUsingMavenAndKafkaStreams() {
ProjectRequest request = createProjectRequest("kafka-streams");
assertThat(mavenPom(request)).containsIgnoringWhitespaces("""
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
""");
}

@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"
}
""");
}

}

0 comments on commit 8a982d8

Please sign in to comment.