简体   繁体   English

如何使用 Gradle Kotlin DSL Dockerize 一个 Spring Boot 应用程序

[英]How to Dockerize a Spring Boot application with Gradle Kotlin DSL

I am following this guide to Dockerize a Spring Boot application .我正在按照本指南对Spring Boot 应用程序进行Dockerize The point is I am using Gradle's Kotlin DSL and I'm having trouble converting the original Groovy syntax to Kotlin.关键是我正在使用 Gradle 的 Kotlin DSL,但在将原始 Groovy 语法转换为 Kotlin 时遇到了问题。

This is the original Groovy from the guide:这是指南中的原始 Groovy:

task unpack(type: Copy) {
    dependsOn bootJar
    from(zipTree(tasks.bootJar.outputs.files.singleFile))
    into("build/dependency")
}

docker {
    name "${project.group}/${bootJar.baseName}"
    copySpec.from(tasks.unpack.outputs).into("dependency")
    buildArgs(['DEPENDENCY': "dependency"])
}

This is the Kotlin I have got so far:这是我目前得到的 Kotlin:

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

plugins {
    id("org.springframework.boot") version "2.2.0.M4"
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
    id("com.palantir.docker") version "0.22.1"
    kotlin("jvm") version "1.3.31"
    kotlin("plugin.spring") version "1.3.31"
}

group = "com.something"
version = "1.0.0-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-webflux")
    implementation("org.jetbrains.kotlin:kotlin-reflect")
    implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
    // ...
}

I haven't actually tried the below yet so it might need a bit of tweaking, but I think what you need is roughly this:我还没有真正尝试过以下内容,因此可能需要进行一些调整,但我认为您需要的大致如下:

task<Copy>("unpack") {
    val bootJar = tasks.getByName<BootJar>("bootJar")
    dependsOn(bootJar)
    from(zipTree(bootJar.outputs.files))
    into("build/dependency")
}

docker {
    name = "${project.group}/${tasks.getByName<BootJar>("bootJar").archiveBaseName}"
    copySpec.from(tasks.getByName<Copy>("unpack").outputs).into("dependency")
    buildArgs(mapOf("DEPENDENCY" to "dependency"))
}

The answer from Yoni Gibbs provides a good start, but this is what worked for me: Yoni Gibbs 的回答提供了一个良好的开端,但这对我有用:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.springframework.boot.gradle.tasks.bundling.BootJar

plugins {
    id("org.springframework.boot") version "2.1.6.RELEASE"
    id("io.spring.dependency-management") version "1.0.7.RELEASE"
    kotlin("jvm") version "1.2.71"
    kotlin("plugin.spring") version "1.2.71"
    id("com.palantir.docker") version "0.22.1"
}

task<Copy>("unpack") {
    val bootJar = tasks.getByName<BootJar>("bootJar")
    dependsOn(bootJar)
    from(zipTree(bootJar.outputs.files.singleFile))
    into("build/dependency")
}

docker {
    val archiveBaseName = tasks.getByName<BootJar>("bootJar").archiveBaseName.get()
    name = "${project.group}/$archiveBaseName"
    copySpec.from(tasks.getByName<Copy>("unpack").outputs).into("dependency")
    buildArgs(mapOf("DEPENDENCY" to "dependency"))
}

You shouldn't do the copy if you know the path of the jar...如果您知道 jar 的路径,则不应进行复制...

configure<com.palantir.gradle.docker.DockerExtension> {
    dependsOn(tasks.findByPath("build"))
    name = "${project.name}:${version}"
    files("build/libs/${project.name}-${version}.jar")
    buildArgs(mapOf("JAR_FILE" to "${project.name}-${version}.jar"))
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM