构建 spring 引导项目到 docker

Building spring boot project to docker

我正在尝试使用 Kotlin 和 Gradle 构建示例服务注册服务器,当我使用 intelliJ 在本地部署它时一切正常,但是当我尝试将它打包到 docker 和启动容器我明白了:

Error: Could not find or load main class kjc.microservices.srs.ServiceRegistrationServerKt

Caused by: java.lang.ClassNotFoundException: kjc.microservices.srs.ServiceRegistrationServerKt

这是我的gradle.build.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "2.5.0"
    id("com.palantir.docker") version "0.26.0"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.10"
    kotlin("plugin.spring") version "1.5.10"
}

apply(plugin="kotlin")
apply(plugin="application")
group="demo"
version="0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}
docker {
    name ="demo/sd:0.0.1"
    copySpec.from("build").into("build")
    pull(true)
}

extra["springCloudVersion"] ="2020.0.3"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.springframework.cloud:spring-cloud-starter")
    implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget ="11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

tasks.withType<Jar> {
        manifest {
            attributes["Main-Class"] ="kjc.microservices.srs.ServiceRegistrationServerKt"
        }
    duplicatesStrategy = DuplicatesStrategy.INCLUDE
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })    }

能帮我定位问题吗?

本地启动的日志显示相同的 class 我指定为 main:

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.5.0)

2021-06-04 22:22:48.910  INFO 11248 --- [           main] k.m.srs.ServiceRegistrationServerKt      : Starting ServiceRegistrationServerKt using Java 14.0.1 
2021-06-04 22:22:48.911  INFO 11248 --- [           main] k.m.srs.ServiceRegistrationServerKt      : No active profile set, falling back to default profiles: default
2021-06-04 22:22:49.647  INFO 11248 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=7aeea164-42c7-35ca-a39f-9a5514dbc083

Docker 文件:

FROM openjdk:12-jdk-alpine
VOLUME /tmp
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

已修复:

docker文件中有错误的 jar,也已清除 build.gradle.kt:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    java
    id("org.springframework.boot") version "2.5.0"
    id("io.spring.dependency-management") version "1.0.11.RELEASE"
    kotlin("jvm") version "1.5.10"
    kotlin("plugin.spring") version "1.5.10"
    application
}
apply(plugin = "io.spring.dependency-management")
apply(plugin="kotlin")
group="demo/sd"
version="0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_11
java.targetCompatibility= JavaVersion.VERSION_11

repositories {
    mavenCentral()
}
extra["springCloudVersion"] ="2020.0.3"

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    implementation("org.springframework.cloud:spring-cloud-starter")
    implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-common")
}

dependencyManagement {
    imports {
        mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
    }
}
springBoot {
    mainClass.set("kjc.microservices.srs.ServiceRegistrationServerKt")
}
application {
    mainClass.set("kjc.microservices.srs.ServiceRegistrationServerKt")
}
tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget ="11"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}
tasks.withType<org.springframework.boot.gradle.tasks.bundling.BootJar> {
    manifest {
        attributes("Start-Class" to "kjc.microservices.srs.ServiceRegistrationServerKt")
    }
   }

Docker 文件:

FROM openjdk:12-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=build/libs/ds-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

dockerfile 构造不佳, 还为 build.gradl.kt

中的应用程序添加了 main-class 配置