From e30745da35665fcdb921f198fa84cf268fb5be7f Mon Sep 17 00:00:00 2001 From: Forbiddem Date: Sun, 17 May 2026 15:36:04 +0000 Subject: [PATCH] Add Spring Boot Maven sample Adds a third sample under samples/spring-boot-maven/ that mirrors the existing Gradle Spring Boot sample but builds with Maven. The sample runs Pkl's Java code generator during the generate-sources phase via exec-maven-plugin, adds the generated directory as an extra source root via build-helper-maven-plugin, and starts a Spring Boot application that reads its configuration from application.pkl. This addresses the requests in #2 for a Maven-based example. Verified end to end with Maven 3.6.3 and JDK 17: * mvn package then java -jar target/spring-boot-maven-1.0.0-SNAPSHOT.jar * mvn spring-boot:run Both produced the expected Server bean output bound from application.pkl. --- samples/spring-boot-maven/README.adoc | 25 ++++ samples/spring-boot-maven/pom.xml | 130 ++++++++++++++++++ .../java/samples/boot/maven/Application.java | 40 ++++++ .../main/java/samples/boot/maven/Server.java | 31 +++++ .../src/main/resources/AppConfig.pkl | 14 ++ .../src/main/resources/application.pkl | 14 ++ 6 files changed, 254 insertions(+) create mode 100644 samples/spring-boot-maven/README.adoc create mode 100644 samples/spring-boot-maven/pom.xml create mode 100644 samples/spring-boot-maven/src/main/java/samples/boot/maven/Application.java create mode 100644 samples/spring-boot-maven/src/main/java/samples/boot/maven/Server.java create mode 100644 samples/spring-boot-maven/src/main/resources/AppConfig.pkl create mode 100644 samples/spring-boot-maven/src/main/resources/application.pkl diff --git a/samples/spring-boot-maven/README.adoc b/samples/spring-boot-maven/README.adoc new file mode 100644 index 0000000..cfc1443 --- /dev/null +++ b/samples/spring-boot-maven/README.adoc @@ -0,0 +1,25 @@ += Spring Boot Example (Maven) +:uri-docs: https://pkl-lang.org/spring/current/spring-boot.html + +This example demonstrates how to configure a Spring Boot application with Pkl using Maven. +For a walkthrough of the underlying integration, see the link:{uri-docs}[documentation]. + +The build invokes Pkl's Java code generator during the `generate-sources` phase via +`exec-maven-plugin`, then registers the generated directory as an additional source root +via `build-helper-maven-plugin`. The generated `AppConfig` class is annotated with +`@ConfigurationProperties` and picked up by Spring Boot at startup. + +== Run + +[source,shell] +---- +mvn spring-boot:run +---- + +To package the application as an executable jar: + +[source,shell] +---- +mvn package +java -jar target/spring-boot-maven-1.0.0-SNAPSHOT.jar +---- diff --git a/samples/spring-boot-maven/pom.xml b/samples/spring-boot-maven/pom.xml new file mode 100644 index 0000000..ec9dcfd --- /dev/null +++ b/samples/spring-boot-maven/pom.xml @@ -0,0 +1,130 @@ + + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.5.6 + + + + org.pkl-lang.samples + spring-boot-maven + 1.0.0-SNAPSHOT + + + 17 + 0.31.1 + 0.18.0 + ${project.build.directory}/generated-sources/pkl + ${generated.root.directory}/java + ${generated.root.directory}/resources + + + + + org.springframework.boot + spring-boot-starter + + + org.pkl-lang + pkl-spring + ${pkl-spring.version} + + + + + + + org.codehaus.mojo + exec-maven-plugin + 3.5.0 + + + pkl-codegen + generate-sources + + java + + + org.pkl.codegen.java.Main + + --output-dir + ${generated.root.directory} + --generate-getters + --generate-spring-boot + ${project.basedir}/src/main/resources/AppConfig.pkl + + true + false + + + + + + org.pkl-lang + pkl-tools + ${pkl.version} + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.6.0 + + + add-pkl-generated-sources + generate-sources + + add-source + + + + ${generated.sources.directory} + + + + + add-pkl-generated-resources + generate-resources + + add-resource + + + + + ${generated.resources.directory} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/samples/spring-boot-maven/src/main/java/samples/boot/maven/Application.java b/samples/spring-boot-maven/src/main/java/samples/boot/maven/Application.java new file mode 100644 index 0000000..ff7dd3a --- /dev/null +++ b/samples/spring-boot-maven/src/main/java/samples/boot/maven/Application.java @@ -0,0 +1,40 @@ +/** + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. + * + * 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 samples.boot.maven; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +@SpringBootApplication +@ConfigurationPropertiesScan +public class Application { + public static void main(String... args) { + new SpringApplication(Application.class).run(args); + } + + @Bean + @SuppressWarnings("unused") + public CommandLineRunner commandLineRunner(ApplicationContext ctx) { + return args -> { + var server = ctx.getBean(Server.class); + System.out.println(server.getConfig()); + }; + } +} diff --git a/samples/spring-boot-maven/src/main/java/samples/boot/maven/Server.java b/samples/spring-boot-maven/src/main/java/samples/boot/maven/Server.java new file mode 100644 index 0000000..9514447 --- /dev/null +++ b/samples/spring-boot-maven/src/main/java/samples/boot/maven/Server.java @@ -0,0 +1,31 @@ +/** + * Copyright © 2024-2026 Apple Inc. and the Pkl project authors. All rights reserved. + * + * 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 samples.boot.maven; + +import org.springframework.stereotype.Service; + +@Service +public class Server { + private final AppConfig.Server config; + + public Server(AppConfig.Server config) { + this.config = config; + } + + public AppConfig.Server getConfig() { + return config; + } +} diff --git a/samples/spring-boot-maven/src/main/resources/AppConfig.pkl b/samples/spring-boot-maven/src/main/resources/AppConfig.pkl new file mode 100644 index 0000000..88621ea --- /dev/null +++ b/samples/spring-boot-maven/src/main/resources/AppConfig.pkl @@ -0,0 +1,14 @@ +// this module name determines the package and +// class name of the generated Java config class +module samples.boot.maven.AppConfig + +server: Server + +class Server { + endpoints: Listing +} + +class Endpoint { + name: String + port: UInt16 +} diff --git a/samples/spring-boot-maven/src/main/resources/application.pkl b/samples/spring-boot-maven/src/main/resources/application.pkl new file mode 100644 index 0000000..1e4353b --- /dev/null +++ b/samples/spring-boot-maven/src/main/resources/application.pkl @@ -0,0 +1,14 @@ +amends "AppConfig.pkl" + +server { + endpoints { + new { + name = "endpoint1" + port = 1234 + } + new { + name = "endpoint2" + port = 5678 + } + } +}