如何使用Gradle Kotlin DSL对Spring Boot应用程序进行Docker

时间:2019-07-18 09:43:03

标签: spring-boot docker gradle kotlin gradle-kotlin-dsl

我正在按照Dockerize a Spring Boot application的指南进行操作。关键是我正在使用Gradle的Kotlin DSL,但在将原始Groovy语法转换为Kotlin时遇到问题。

这是指南中的原始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"])
}

这是我到目前为止获得的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")
    // ...
}

3 个答案:

答案 0 :(得分:0)

我实际上还没有尝试下面的方法,因此可能需要一些调整,但是我认为您需要的大致是这样:

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"))
}

答案 1 :(得分:0)

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"))
}

答案 2 :(得分:0)

如果您知道罐子的路径,就不应该进行复制...

using System.Collections;

public static void myMethod(IDictionary myDictionary)
{
    foreach (DictionaryEntry item in myDictionary) {
        Console.WriteLine(item.Value.ToString()); // sample code
    }

}