如何在 buildSrc kotlin 文件中配置 Jib 扩展?

How can I configure the Jib Extension inside a buildSrc kotlin file?

我正在尝试模块化我的 build.gradle.kts。有人建议我创建一个 buildSrc 文件夹。 经过一番研究和询问后,我找到了这篇文章 I hated Gradle!... 所以这是我的尝试:

buildSrc 树:

buildSrc/
├── build.gradle.kts
├── settings.gradle.kts
└── src
    └── main
        ├── kotlin
        │   ├── Docker.kt
        │   ├── MyProjectExtensions.kt
        │   └── Versions.kt
        └── resources
            └── META-INF
                └── gradle-plugins
                    └── pt.branden.brandenportal.properties

我的build.gradle.kts:

plugins {
    `kotlin-dsl`
    id("com.google.cloud.tools.jib") version Versions.jib
}

repositories {
    mavenCentral()
    google()
    jcenter()
}

dependencies {
    implementation("gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:${Versions.jib}")
    implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50")
    implementation(gradleApi())
    implementation(localGroovy())
}

最后 Docker.kt:

import org.gradle.api.Plugin
import org.gradle.api.Project


open class JibConfigPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        //configureJib()
        TODO("not implemented")
    }

}

//internal fun Project.configureJib() = this.extensions.getByType<JibExtension>().run {}
internal fun Project.configureJib() = project.configure<JibExtension>() {
  TODO("not implemented")
}

我的问题是找不到 JibExtension,所以当我尝试实施和配置 Jib 时它不起作用,但在 build.gradle.kts 中一切正常。

My problem is that I can't find the JibExtension

可以以多种不同的方式应用插件或扩展。您可以"react"使用PluginManagerwithPlugin方法应用到正在应用的插件:

class JibConfigPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        project.pluginManager.withPlugin("com.google.cloud.tools.jib") {
            // Configuration happens inside this Action block.
        }
    }
}

使用此方法,您可以确定已应用插件 exists/has,而无需强制 user/project 使用该插件。

Jib 插件提供单一扩展和多种任务。

配置扩展可以通过以下方式完成:

class JibConfigPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        project.pluginManager.withPlugin("com.google.cloud.tools.jib") {
           project.extensions.configure<JibExtension> {
                // Example configuring the `container`
                container {
                    creationTime = "USE_CURRENT_TIMESTAMP"
                }
           }
        }
    }
}

正在查看 the source of the Gradle plugin for Jib, the authors used lazy configuration 任务,因此最好也使用相同的方法来配置这些任务。

例如,要配置 jib 任务:

class JibConfigPlugin : Plugin<Project> {

    override fun apply(project: Project) {
        project.pluginManager.withPlugin("com.google.cloud.tools.jib") {
            project.tasks.named<BuildImageTask>("jib") {
                to {
                    setTargetImage("my_acr_name.azurecr.io/my-app")
                }
            }
        }
    }
}

上面使用了named method which returns a TaskProvider

然后按照此处的说明简单地应用您的插件:https://guides.gradle.org/writing-gradle-plugins/#apply_the_plugin_to_the_host_project


我用来测试的 build.gradle.kts 来源:

plugins {
    `kotlin-dsl`
}

repositories {
    gradlePluginPortal()
}

dependencies {
    implementation("gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:1.7.0")
}