diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fd02c92 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.gradle +build +slides diff --git a/.github/workflows/native-image.yml b/.github/workflows/native-image.yml new file mode 100644 index 0000000..f15c569 --- /dev/null +++ b/.github/workflows/native-image.yml @@ -0,0 +1,65 @@ +name: Native image + +# build: unprivileged (contents: read) native build and Kamal label check, for PRs, manual runs and master. +# publish: only on push to master; loads the image built above and pushes : and :latest. +# It never checks out or runs repository code, so PR code never sees a packages:write token. +on: + push: + branches: [master] + pull_request: + workflow_dispatch: + +permissions: + contents: read + +env: + IMAGE: ghcr.io/${{ github.repository }} + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + persist-credentials: false + - uses: docker/setup-buildx-action@v3 + - uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64 + load: true + tags: ${{ env.IMAGE }}:${{ github.sha }} + cache-from: type=gha + cache-to: type=gha,mode=max + - name: Kamal service label + run: | + label=$(docker image inspect -f '{{index .Config.Labels "service"}}' "$IMAGE:${{ github.sha }}") + echo "service label: '$label'" + test "$label" = fullstack-with-spring + - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + run: docker save "$IMAGE:${{ github.sha }}" | gzip > image.tar.gz + - if: github.event_name == 'push' && github.ref == 'refs/heads/master' + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: image + path: image.tar.gz + retention-days: 1 + + publish: + needs: build + if: github.event_name == 'push' && github.ref == 'refs/heads/master' + runs-on: ubuntu-latest + permissions: + packages: write + steps: + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + name: image + - env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gunzip -c image.tar.gz | docker load + echo "$GH_TOKEN" | docker login ghcr.io -u "$GITHUB_ACTOR" --password-stdin + docker tag "$IMAGE:${{ github.sha }}" "$IMAGE:latest" + docker push "$IMAGE:${{ github.sha }}" + docker push "$IMAGE:latest" diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..fab50e5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +# Native image build: GraalVM 25 (required by Spring Boot 4 AOT/native). +FROM ghcr.io/graalvm/native-image-community:25 AS build +# gradlew needs xargs +RUN microdnf install -y findutils && microdnf clean all +WORKDIR /app +COPY . . +RUN --mount=type=cache,target=/root/.gradle ./gradlew nativeCompile --no-daemon + +FROM gcr.io/distroless/base-debian12:nonroot +# Kamal requires service= (deploy/apps/fullstack-with-spring/config/deploy.yml). +LABEL service="fullstack-with-spring" +COPY --from=build /app/build/native/nativeCompile/fullstack-demo /app/fullstack-demo +EXPOSE 8080 +ENTRYPOINT ["/app/fullstack-demo"] diff --git a/build.gradle.kts b/build.gradle.kts index 6cd5075..bae534d 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,52 +1,50 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - plugins { - id("org.springframework.boot") version "3.2.2" - id("io.spring.dependency-management") version "1.1.4" - java - kotlin("jvm") version "1.9.22" - kotlin("plugin.spring") version "1.9.22" + id("org.springframework.boot") version "4.0.9" + id("io.spring.dependency-management") version "1.1.7" + id("org.graalvm.buildtools.native") version "0.11.5" + kotlin("jvm") version "2.2.21" + kotlin("plugin.spring") version "2.2.21" } group = "de.tschuehly" version = "0.0.1-SNAPSHOT" -java.sourceCompatibility = JavaVersion.VERSION_17 + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(25) + } +} + +// Kotlin 2.2 cannot emit Java 25 bytecode; compile for 17, build/run on JDK 25 (GraalVM 25 for native). +tasks.withType { + options.release = 17 +} repositories { mavenCentral() - mavenLocal() - maven { - url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") - mavenContent { - snapshotsOnly() - } - } - maven("https://jitpack.io") } dependencies { implementation("org.springframework.boot:spring-boot-starter-thymeleaf") - implementation("org.thymeleaf.extras:thymeleaf-extras-java8time:3.0.4.RELEASE") - - implementation("org.springframework.boot:spring-boot-starter-web") - implementation("com.fasterxml.jackson.module:jackson-module-kotlin") - implementation("org.jetbrains.kotlin:kotlin-reflect") - implementation("org.thymeleaf.extras:thymeleaf-extras-springsecurity6") - implementation("org.springframework.boot:spring-boot-starter-aop") + implementation("org.springframework.boot:spring-boot-starter-webmvc") + implementation("org.springframework.boot:spring-boot-starter-aspectj") implementation("org.springframework.boot:spring-boot-starter-actuator") + implementation("tools.jackson.module:jackson-module-kotlin") + implementation("org.jetbrains.kotlin:kotlin-reflect") developmentOnly("org.springframework.boot:spring-boot-devtools") testImplementation("org.springframework.boot:spring-boot-starter-test") + testRuntimeOnly("org.junit.platform:junit-platform-launcher") - implementation("de.tschuehly:spring-view-component-thymeleaf:0.7.2") + implementation("de.tschuehly:spring-view-component-thymeleaf:0.9.1") implementation("org.webjars.npm:htmx.org:1.9.10") implementation("org.webjars.npm:alpinejs:3.13.3") } -tasks.withType { - kotlinOptions { - freeCompilerArgs = listOf("-Xjsr305=strict") - jvmTarget = "17" +kotlin { + compilerOptions { + freeCompilerArgs.addAll("-Xjsr305=strict") + jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17 } } @@ -63,3 +61,11 @@ sourceSets { } } +graalvmNative { + binaries { + named("main") { + // libc dynamic, everything else static: runs on gcr.io/distroless/base + buildArgs.add("-H:+StaticExecutableWithDynamicLibC") + } + } +} diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index d64cd49..8bdaf60 100644 Binary files a/gradle/wrapper/gradle-wrapper.jar and b/gradle/wrapper/gradle-wrapper.jar differ diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 1af9e09..2e11132 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-9.1.0-bin.zip networkTimeout=10000 validateDistributionUrl=true zipStoreBase=GRADLE_USER_HOME diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 index 1aa94a4..adff685 --- a/gradlew +++ b/gradlew @@ -1,7 +1,7 @@ #!/bin/sh # -# Copyright © 2015-2021 the original authors. +# Copyright © 2015 the original authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -15,6 +15,8 @@ # See the License for the specific language governing permissions and # limitations under the License. # +# SPDX-License-Identifier: Apache-2.0 +# ############################################################################## # @@ -55,7 +57,7 @@ # Darwin, MinGW, and NonStop. # # (3) This script is generated from the Groovy template -# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt +# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt # within the Gradle project. # # You can find Gradle at https://github.com/gradle/gradle/. @@ -84,7 +86,7 @@ done # shellcheck disable=SC2034 APP_BASE_NAME=${0##*/} # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) -APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit +APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit # Use the maximum available, or set MAX_FD != -1 to use that value. MAX_FD=maximum @@ -112,7 +114,6 @@ case "$( uname )" in #( NONSTOP* ) nonstop=true ;; esac -CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar # Determine the Java command to use to start the JVM. @@ -170,7 +171,6 @@ fi # For Cygwin or MSYS, switch paths to Windows format before running java if "$cygwin" || "$msys" ; then APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) - CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) JAVACMD=$( cygpath --unix "$JAVACMD" ) @@ -203,15 +203,14 @@ fi DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' # Collect all arguments for the java command: -# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, +# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, # and any embedded shellness will be escaped. # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be # treated as '${Hostname}' itself on the command line. set -- \ "-Dorg.gradle.appname=$APP_BASE_NAME" \ - -classpath "$CLASSPATH" \ - org.gradle.wrapper.GradleWrapperMain \ + -jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \ "$@" # Stop when "xargs" is not available. diff --git a/gradlew.bat b/gradlew.bat index 93e3f59..c4bdd3a 100644 --- a/gradlew.bat +++ b/gradlew.bat @@ -13,6 +13,8 @@ @rem See the License for the specific language governing permissions and @rem limitations under the License. @rem +@rem SPDX-License-Identifier: Apache-2.0 +@rem @if "%DEBUG%"=="" @echo off @rem ########################################################################## @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe %JAVA_EXE% -version >NUL 2>&1 if %ERRORLEVEL% equ 0 goto execute -echo. -echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail @@ -57,22 +59,21 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe if exist "%JAVA_EXE%" goto execute -echo. -echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% -echo. -echo Please set the JAVA_HOME variable in your environment to match the -echo location of your Java installation. +echo. 1>&2 +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 +echo. 1>&2 +echo Please set the JAVA_HOME variable in your environment to match the 1>&2 +echo location of your Java installation. 1>&2 goto fail :execute @rem Setup the command line -set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar @rem Execute Gradle -"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -jar "%APP_HOME%\gradle\wrapper\gradle-wrapper.jar" %* :end @rem End local scope for the variables with windows NT shell diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/common/NativeHints.kt b/src/main/kotlin/de/tschuehly/fullstackdemo/common/NativeHints.kt new file mode 100644 index 0000000..c13368d --- /dev/null +++ b/src/main/kotlin/de/tschuehly/fullstackdemo/common/NativeHints.kt @@ -0,0 +1,34 @@ +package de.tschuehly.fullstackdemo.common + +import de.tschuehly.fullstackdemo.web.component.person.form.PersonFormViewComponent +import de.tschuehly.spring.viewcomponent.thymeleaf.ViewContext +import org.springframework.aot.hint.BindingReflectionHintsRegistrar +import org.springframework.aot.hint.RuntimeHints +import org.springframework.aot.hint.RuntimeHintsRegistrar +import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider +import org.springframework.context.annotation.Configuration +import org.springframework.context.annotation.ImportRuntimeHints +import org.springframework.core.type.filter.AssignableTypeFilter + +/** + * GraalVM native hints (runs at AOT build time): + * - ViewComponent templates live next to the classes, outside classpath:/templates + * - Thymeleaf/SpEL reads ViewContext properties reflectively + * - the PUT form binds PersonFormDTO via its Kotlin constructor + */ +@Configuration(proxyBeanMethods = false) +@ImportRuntimeHints(NativeHints::class) +class NativeHints : RuntimeHintsRegistrar { + override fun registerHints(hints: RuntimeHints, classLoader: ClassLoader?) { + hints.resources().registerPattern("de/tschuehly/fullstackdemo/**/*.html") + + val scanner = ClassPathScanningCandidateComponentProvider(false).apply { + addIncludeFilter(AssignableTypeFilter(ViewContext::class.java)) + } + val contexts = scanner.findCandidateComponents("de.tschuehly.fullstackdemo") + .map { Class.forName(it.beanClassName, false, classLoader) } + BindingReflectionHintsRegistrar().registerReflectionHints( + hints.reflection(), *(contexts + PersonFormViewComponent.PersonFormDTO::class.java).toTypedArray() + ) + } +} diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.html b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.html deleted file mode 100644 index 78cbc2c..0000000 --- a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.html +++ /dev/null @@ -1,60 +0,0 @@ - - - - -
- -
- -
-
- -
-
- -
-
- -
- -
- -
-
- -
-
-
- \ No newline at end of file diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.kt b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.kt deleted file mode 100644 index 4dce56d..0000000 --- a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/person/action/PersonActionViewComponent.kt +++ /dev/null @@ -1,60 +0,0 @@ -package de.tschuehly.fullstackdemo.web.component.person.action - -import de.tschuehly.fullstackdemo.core.person.Person -import de.tschuehly.fullstackdemo.core.person.PersonService -import de.tschuehly.spring.viewcomponent.core.action.PutViewAction -import de.tschuehly.spring.viewcomponent.core.component.ViewComponent -import de.tschuehly.spring.viewcomponent.thymeleaf.ViewContext -import java.time.LocalDate - -@ViewComponent -class PersonActionViewComponent( - private val personService: PersonService -) { - fun render(personId: Int? = null) = PersonActionCtx( - getPerson(personId) - ) - - fun getPerson(personId: Int?): PersonFormDTO { - if (personId != null) { - personService.getPerson(personId).also { return PersonFormDTO(it) } - } - return PersonFormDTO(null, "", "", "", "", LocalDate.now()) - } - - @PutViewAction - fun savePerson(personFormDTO: PersonFormDTO): ViewContext { - val person = personService.savePerson(personFormDTO.convertToPerson()) - return render(person.id); - } - - data class PersonActionCtx(val person: PersonFormDTO) : ViewContext - - class PersonFormDTO( - val id: Int?, - val firstName: String, - val lastName: String, - val phoneNumber: String, - val email: String, - val birthDate: LocalDate - ) { - - constructor(person: Person) : this( - person.id, - person.firstName, - person.lastName, - person.phoneNumber, - person.email, - person.birthDate - ) - - fun convertToPerson() = Person( - this.id, - this.firstName, - this.lastName, - this.phoneNumber, - this.email, - this.birthDate - ) - } -} \ No newline at end of file diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/advancedTable/AdvancedTableViewComponent.html b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/advancedTable/AdvancedTableViewComponent.html index 26b3902..d853bb5 100644 --- a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/advancedTable/AdvancedTableViewComponent.html +++ b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/advancedTable/AdvancedTableViewComponent.html @@ -23,7 +23,7 @@ class="text-left py-3 px-4"> - diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/toast/ToastViewComponent.html b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/toast/ToastViewComponent.html index 3e5adee..d44771d 100644 --- a/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/toast/ToastViewComponent.html +++ b/src/main/kotlin/de/tschuehly/fullstackdemo/web/component/util/toast/ToastViewComponent.html @@ -9,12 +9,12 @@ } setTimeout(() => hideToast(), - [[${toastContext.duration}]] + [[${toastCtx.duration}]] )
- + diff --git a/src/main/kotlin/de/tschuehly/fullstackdemo/web/page/layout/LayoutViewComponent.kt b/src/main/kotlin/de/tschuehly/fullstackdemo/web/page/layout/LayoutViewComponent.kt index 43e5007..4b4180e 100644 --- a/src/main/kotlin/de/tschuehly/fullstackdemo/web/page/layout/LayoutViewComponent.kt +++ b/src/main/kotlin/de/tschuehly/fullstackdemo/web/page/layout/LayoutViewComponent.kt @@ -3,7 +3,6 @@ package de.tschuehly.fullstackdemo.web.page.layout import de.tschuehly.fullstackdemo.web.page.layout.header.HeaderViewComponent import de.tschuehly.fullstackdemo.web.page.layout.sidebar.SidebarViewComponent import de.tschuehly.spring.viewcomponent.core.component.ViewComponent -import de.tschuehly.spring.viewcomponent.core.toProperty import de.tschuehly.spring.viewcomponent.thymeleaf.ViewContext @ViewComponent