diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index c150449..ff960b9 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -55,4 +55,4 @@ jobs: uses: docker/build-push-action@v3 with: context: . - file: ./jvm.Dockerfile + file: ./unpacked.Dockerfile diff --git a/backoffice-app/build.gradle.kts b/backoffice-app/build.gradle.kts index 90c38e4..eb6abe5 100644 --- a/backoffice-app/build.gradle.kts +++ b/backoffice-app/build.gradle.kts @@ -3,7 +3,8 @@ plugins { id("org.springframework.boot") id("io.spring.dependency-management") id("dev.vlaship.lombok") - alias(libs.plugins.git.properties) + id("dev.vlaship.unpack") + id("dev.vlaship.git-properties") } val jwtVersion = "0.12.3" // "0.11.5" works with graalvm @@ -12,10 +13,14 @@ val mapstructVersion = "1.5.5.Final" val preLiquibaseVersion = "1.5.0" val openTelemetryVersion = "2.0.0" val micrometerVersion = "1.2.2" +val findbugsVersion = "3.0.2" dependencies { implementation(project(":backoffice-api")) + // avoid warnings + compileOnly("com.google.code.findbugs:jsr305:$findbugsVersion") + // security implementation("org.springframework.boot:spring-boot-starter-security") implementation("io.jsonwebtoken:jjwt-api:$jwtVersion") @@ -23,8 +28,8 @@ dependencies { implementation("io.jsonwebtoken:jjwt-jackson:$jwtVersion") // web - implementation("org.springframework.boot:spring-boot-starter-web") implementation(libs.springdoc) + implementation("org.springframework.boot:spring-boot-starter-web") implementation("org.springframework.boot:spring-boot-starter-validation") // mapper @@ -47,19 +52,6 @@ dependencies { testImplementation("org.springframework.security:spring-security-test") } -gitProperties { - dateFormat = "yyyy-MM-dd HH:mm:ssZ" - dateFormatTimeZone = "GMT" - keys = arrayListOf("git.branch", "git.commit.id", "git.commit.id.abbrev", "git.commit.time", "git.tags") -} - tasks.named("test") { useJUnitPlatform() } - -// task unpack -task("unpack", type = Copy::class) { - dependsOn("build") - from(zipTree(tasks.bootJar.get().archiveFile.get())) - into("build/unpacked") -} diff --git a/docker-compose.yaml b/docker-compose.yaml index ce7cca5..346ed0d 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -36,7 +36,7 @@ services: jvm-api: build: context: . - dockerfile: jvm.Dockerfile + dockerfile: gradle.Dockerfile restart: unless-stopped ports: - "18080:18080" diff --git a/gradle-plugins/build.gradle.kts b/gradle-plugins/build.gradle.kts index 75c0eb2..4c17e3b 100644 --- a/gradle-plugins/build.gradle.kts +++ b/gradle-plugins/build.gradle.kts @@ -4,4 +4,7 @@ plugins { dependencies { implementation(libs.kotlin.gradle.plugin) + implementation(libs.graalvm.gradle.plugin) + implementation(libs.spring.boot.gradle.plugin) + implementation(libs.git.properties.gradle.plugin) } diff --git a/gradle-plugins/src/main/kotlin/dev.vlaship.git-properties.gradle.kts b/gradle-plugins/src/main/kotlin/dev.vlaship.git-properties.gradle.kts new file mode 100644 index 0000000..7788870 --- /dev/null +++ b/gradle-plugins/src/main/kotlin/dev.vlaship.git-properties.gradle.kts @@ -0,0 +1,9 @@ +plugins { + id("com.gorylenko.gradle-git-properties") +} + +gitProperties { + dateFormat = "yyyy-MM-dd HH:mm:ssZ" + dateFormatTimeZone = "GMT" + keys = arrayListOf("git.branch", "git.commit.id", "git.commit.id.abbrev", "git.commit.time", "git.tags") +} diff --git a/gradle-plugins/src/main/kotlin/dev.vlaship.graalvm.gradle.kts b/gradle-plugins/src/main/kotlin/dev.vlaship.graalvm.gradle.kts index 2c76a9f..15f9cb9 100644 --- a/gradle-plugins/src/main/kotlin/dev.vlaship.graalvm.gradle.kts +++ b/gradle-plugins/src/main/kotlin/dev.vlaship.graalvm.gradle.kts @@ -1,19 +1,18 @@ plugins { - id("java-library") -// id("org.graalvm.buildtools.native") + id("org.graalvm.buildtools.native") } -//graalvmNative { -// binaries { -// named("main") { -// imageName.set("app") -// useFatJar.set(true) -// } -// } -// binaries.all { -// buildArgs.add("--verbose") -// resources.autodetect() -// } -// -// toolchainDetection.set(true) -//} +graalvmNative { + binaries { + named("main") { + imageName.set("app") + useFatJar.set(true) + } + } + binaries.all { + buildArgs.add("--verbose") + resources.autodetect() + } + + toolchainDetection.set(true) +} diff --git a/gradle-plugins/src/main/kotlin/dev.vlaship.java-lib.gradle.kts b/gradle-plugins/src/main/kotlin/dev.vlaship.java-lib.gradle.kts index e4810fe..d98507d 100644 --- a/gradle-plugins/src/main/kotlin/dev.vlaship.java-lib.gradle.kts +++ b/gradle-plugins/src/main/kotlin/dev.vlaship.java-lib.gradle.kts @@ -10,7 +10,3 @@ java { targetCompatibility = javaVersion toolchain { languageVersion.set(JavaLanguageVersion.of(javaVersion.majorVersion)) } } - -tasks.withType { - useJUnitPlatform() -} diff --git a/gradle-plugins/src/main/kotlin/dev.vlaship.unpack.gradle.kts b/gradle-plugins/src/main/kotlin/dev.vlaship.unpack.gradle.kts new file mode 100644 index 0000000..c25049a --- /dev/null +++ b/gradle-plugins/src/main/kotlin/dev.vlaship.unpack.gradle.kts @@ -0,0 +1,14 @@ +import org.gradle.api.tasks.Copy +import org.gradle.kotlin.dsl.task + +plugins { + id("java-library") + id("org.springframework.boot") +} + +// task unpack +task("unpack", type = Copy::class) { + dependsOn("build") + from(zipTree(tasks.bootJar.get().archiveFile.get())) + into("build/unpacked") +} diff --git a/jvm.Dockerfile b/gradle.Dockerfile similarity index 93% rename from jvm.Dockerfile rename to gradle.Dockerfile index b9b44ab..74b0eca 100644 --- a/jvm.Dockerfile +++ b/gradle.Dockerfile @@ -3,7 +3,7 @@ FROM azul/zulu-openjdk-alpine:21 AS builder # Set the working directory inside the container -WORKDIR /upacked +WORKDIR / # Copy the source code into the container COPY . . @@ -12,7 +12,7 @@ COPY . . RUN ./gradlew clean backoffice-app:unpack -x test --no-daemon # Unpack the jar -ARG UNPACKED=backoffice-app/build/unpacked +ARG UNPACKED=/backoffice-app/build/unpacked COPY ${UNPACKED}/BOOT-INF/lib /upacked/app/lib COPY ${UNPACKED}/META-INF /upacked/app/META-INF diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0740ea8..04865b2 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,12 +3,16 @@ openApiVersion = "2.3.0" springBootVersion = "3.2.2" springDepManVersion = "1.1.4" kotlinGradlePluginVersion = "1.9.22" +graalvmGradlePluginVersion = "0.9.28" +gitPropertiesGradlePluginVersion = "2.4.1" [libraries] springdoc = { module = "org.springdoc:springdoc-openapi-starter-webmvc-ui", version.ref = "openApiVersion" } kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinGradlePluginVersion" } +graalvm-gradle-plugin = { module = "org.graalvm.buildtools.native:org.graalvm.buildtools.native.gradle.plugin", version.ref = "graalvmGradlePluginVersion" } +spring-boot-gradle-plugin = { module = "org.springframework.boot:spring-boot-gradle-plugin", version.ref = "springBootVersion" } +git-properties-gradle-plugin = { module = "com.gorylenko.gradle-git-properties:gradle-git-properties", version.ref = "gitPropertiesGradlePluginVersion" } [plugins] spring-boot = { id = "org.springframework.boot", version.ref = "springBootVersion" } spring-dependency-management = { id = "io.spring.dependency-management", version.ref = "springDepManVersion" } -git-properties = { id = "com.gorylenko.gradle-git-properties", version = "2.4.1" } diff --git a/unpacked.Dockerfile b/unpacked.Dockerfile new file mode 100644 index 0000000..9a50fe9 --- /dev/null +++ b/unpacked.Dockerfile @@ -0,0 +1,33 @@ +### Build stage +# Builder +FROM alpine:latest AS builder + +# Set the working directory inside the container +WORKDIR / + +# Copy unpacked into the container +COPY . . + +# Unpack the jar +ARG UNPACKED=/backoffice-app/build/unpacked + +COPY ${UNPACKED}/BOOT-INF/lib /upacked/app/lib +COPY ${UNPACKED}/META-INF /upacked/app/META-INF +COPY ${UNPACKED}/BOOT-INF/classes /upacked/app + +### Run stage +# Create a minimal production image +FROM azul/zulu-openjdk-alpine:21-jre-headless + +# Set the working directory inside the container +WORKDIR /app + +# Avoid running code as a root user +RUN addgroup -S appgroup && adduser -S appuser -G appgroup +USER appuser + +# Copy only the necessary dirs and files from the builder stage +COPY --from=builder /upacked/app /app + +# Run the binary when the container starts +ENTRYPOINT ["java", "-cp", "/app:/app/lib/*", "dev.vlaship.backoffice.App"]