首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将spring boot项目构建到docker

将spring boot项目构建到docker
EN

Stack Overflow用户
提问于 2021-06-05 04:36:41
回答 1查看 302关注 0票数 0

我正在尝试使用Kotlin & Gradle构建示例服务注册服务器,当我使用intelliJ在本地部署它时,一切工作正常,但当我尝试将其打包到docker并启动容器时,我得到了以下结果:

代码语言:javascript
复制
Error: Could not find or load main class kjc.microservices.srs.ServiceRegistrationServerKt

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

这是我的gradle.build.kts

代码语言:javascript
复制
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) }
    })    }

你能帮我找出问题所在吗?

本地启动的日志显示我指定为main的同一个类:

代码语言:javascript
复制
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: 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

Dockerfile:

代码语言:javascript
复制
FROM openjdk:12-jdk-alpine
VOLUME /tmp
COPY build/libs/*.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]

已修复:

dockerfile中有错误的jar,也清除了build.gradle.kt:

代码语言:javascript
复制
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")
    }
   }

Dockerfile:

代码语言:javascript
复制
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"]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-06-06 20:35:38

dockerfile构造不佳,还为build.gradl.kt中的应用程序添加了主类配置

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67843674

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档