我正在尝试为 Kotlin http4k 后端构建一个 docker 映像,但我无法让它正常工作。我无法创建一个胖 jar,所以当我尝试运行图像时我的依赖项丢失了。
所以我得到一个ClassNotFound
例外。
这是我的build.gradle
文件:
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
}
}
这是我的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" ]
我还尝试了一些 gradle 插件,它们应该使我能够构建一个 fatjar,但我没有让它工作。