I'm trying to modularize my build.gradle.kts
. It was suggested to me to create a buildSrc
folder.
After some research and som asking I found this article I hated Gradle!... so this was my try:
buildSrc
tree:
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
My 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())
}
And finally 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")
}
My problem is that I can't find the JibExtension
, so when I try to implement and configure the Jib it doesn't work but in the build.gradle.kts
everything works.
My problem is that I can't find the
JibExtension
Plugins or extensions can be applied in a variety of different ways. You can "react" to the plugin being applied by using the withPlugin method of PluginManager
:
class JibConfigPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.pluginManager.withPlugin("com.google.cloud.tools.jib") {
// Configuration happens inside this Action block.
}
}
}
Using this method you can be certain that a plugin exists/has been applied without forcing a user/project to use the plugin.
The Jib plugin offers a single extension and a variety of tasks.
Configuring the extension can be done with the following:
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"
}
}
}
}
}
Looking over the source of the Gradle plugin for Jib, the authors used lazy configuration for the tasks, so it is best to also use the same method to configure those tasks.
For example, to configure the jib
task:
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")
}
}
}
}
}
The above uses the named
method which returns a TaskProvider
Then simply apply your plugin as documented here: https://guides.gradle.org/writing-gradle-plugins/#apply_the_plugin_to_the_host_project
Source of the build.gradle.kts
I used to test:
plugins {
`kotlin-dsl`
}
repositories {
gradlePluginPortal()
}
dependencies {
implementation("gradle.plugin.com.google.cloud.tools:jib-gradle-plugin:1.7.0")
}