Iam trying to build a docker image for a Kotlin http4k backend but i cant get it quite working. I can't create a fat jar so my dependencies are missing when i try to run the image.
So i get a ClassNotFound
exception.
Here is my build.gradle
file:
buildscript {
ext.kotlinVersion = "1.4.31"
ext.http4kVersion = "4.5.0.1"
repositories {
mavenCentral()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlinVersion}"
}
}
repositories {
mavenCentral()
}
apply plugin: 'kotlin'
apply plugin: 'application'
compileKotlin.kotlinOptions.jvmTarget = "11"
compileTestKotlin.kotlinOptions.jvmTarget = "11"
mainClassName = 'com.scalangular.LauncherKt'
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
test {
useJUnitPlatform()
}
compileTestKotlin.kotlinOptions {
jvmTarget = "11"
}
dependencies {
implementation "org.http4k:http4k-contract:${http4kVersion}"
implementation "org.http4k:http4k-core:${http4kVersion}"
implementation "org.http4k:http4k-format-jackson:${http4kVersion}"
implementation "org.http4k:http4k-server-jetty:${http4kVersion}"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.4.30"
// testImplementation ...
}
jar {
manifest {
attributes 'Main-Class': mainClassName
}
}
And here is my Dockerfile
:
FROM gradle:latest as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build --no-daemon
FROM openjdk:latest
EXPOSE 9000
RUN mkdir /app
COPY --from=builder /home/gradle/src/build/libs/*.jar /app/ShoppingListApi.jar
CMD [ "java", "-jar", "-Djava.security.egd=file:/dev/./urandom", "/app/ShoppingListApi.jar" ]
I also tryed some gradle plugins which should enable me to build a fatjar but i didnt get it working.
You need to use the shadowjar plugin to create a FatJar. The easiest thing here is to use the http4k toolbox to generate a template project using Shadow and then just copy in the gradle magic from there: https://toolbox.http4k.org/