Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.git
.gradle
build
slides
65 changes: 65 additions & 0 deletions .github/workflows/native-image.yml
Original file line number Diff line number Diff line change
@@ -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 :<sha> 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"
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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=<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"]
62 changes: 34 additions & 28 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<JavaCompile> {
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<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "17"
kotlin {
compilerOptions {
freeCompilerArgs.addAll("-Xjsr305=strict")
jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
}
}

Expand All @@ -63,3 +61,11 @@ sourceSets {
}
}

graalvmNative {
binaries {
named("main") {
// libc dynamic, everything else static: runs on gcr.io/distroless/base
buildArgs.add("-H:+StaticExecutableWithDynamicLibC")
}
}
}
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down
15 changes: 7 additions & 8 deletions gradlew
100644 → 100755

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 13 additions & 12 deletions gradlew.bat

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions src/main/kotlin/de/tschuehly/fullstackdemo/common/NativeHints.kt
Original file line number Diff line number Diff line change
@@ -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()
)
}
}
Loading
Loading