使用Kotlin多模块Spring项目创建Docker映像

时间:2020-08-25 18:04:22

标签: kotlin gradle gradle-kotlin-dsl jib

你好@stackoverflow社区,

我有一个具有以下结构的Kotlin Spring Boot应用程序

.
├── build.gradle.kts
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── pipeline.yml
├── services
│   └── environment
│       ├── build.gradle.kts
│       └── src
│           └── main
│               └── kotlin
│                   └── com
│                       └── demo
│                           └── app
│                               └── services
│                                   └── environment
│                                       ├── controller
│                                       │   └── EnvironmentController.kt
│                                       ├── model
│                                       │   └── Environment.kt
│                                       └── repository
│                                           └── EnvironmentRepository.kt
├── settings.gradle.kts
└── web
    ├── build.gradle.kts
    └── src
        └── main
            ├── kotlin
            │   └── com
            │       └── demo
            │           └── app
            │               └── DemoApplication.kt
            └── resources
                └── application.properties

build.gradle.kts

plugins {
    id("org.springframework.boot") version "2.3.3.RELEASE" apply false
    id("io.spring.dependency-management") version "1.0.10.RELEASE" apply false
    kotlin("jvm") version "1.4.0" apply false
    kotlin("plugin.spring") version "1.4.0" apply false
}

allprojects {
    group = "com.demo"
    version = "1.0.0-SNAPSHOT"

    tasks.withType<JavaCompile> {
        sourceCompatibility = "1.8"
        targetCompatibility = "1.8"
    }

    tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "14"
        }
    }
}

subprojects {
    repositories {
        mavenCentral()
    }

    apply {
        plugin("io.spring.dependency-management")
    }
}

services / environment / build.gradle.kts

plugins {
    id("org.springframework.boot")

    kotlin("jvm")
    kotlin("plugin.spring")
    application
}

dependencies {
    implementation(kotlin("reflect"))
    implementation(kotlin("stdlib-jdk8"))
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.postgresql:postgresql")
}

application {
    mainClassName = "com.demo.app.services.environment"
}

web / build.gradle.kts

plugins {
    id("org.springframework.boot")
    id("com.google.cloud.tools.jib") version "2.5.0"

    kotlin("jvm")
    kotlin("plugin.spring")
    application
}

dependencies {
    implementation(project(":services:environment"))

    implementation(kotlin("reflect"))
    implementation(kotlin("stdlib-jdk8"))
    implementation("org.springframework.boot:spring-boot-starter")
    implementation("org.springframework.boot:spring-boot-starter-web")
    implementation("org.springframework.boot:spring-boot-starter-actuator")
}

application {
    mainClassName = "com.demo.app"
}


jib {
    from {
        image = "openjdk:14-jdk-slim"
    }
    to {
        image = "demo/app"
    }
}

以某种方式,当我使用jibDockerBuild构建Docker映像时,docker容器内部正在运行的应用程序中缺少子模块“ services.environment”,并且我无法访问在此子模块中编写的REST API。

...我真的陷在这个问题上,无法进一步解决,我希望获得帮助。

谢谢, Andrioshe

1 个答案:

答案 0 :(得分:0)

我必须在:services:environment中的build.grade.kts中添加以下行

tasks {
    jar {
        enabled = true
    }
}

并将:web中的主类设置为“ de.demo.app.DemoApplication Kt