kotlin-dsl-samples icon indicating copy to clipboard operation
kotlin-dsl-samples copied to clipboard

How to use closures?

Open antonalechnovic opened this issue 5 years ago • 2 comments
trafficstars

I am trying to convert the following groovy snippet to Kotlin:

class CustomPlugin implements Plugin<Project> {
    void apply(Project target) {
        target.apply plugin: 'com.jfrog.artifactory'
        target.artifactory {
            contextUrl = "http://some/artifactory/"
        }

}

How to set target.artifactory.contextUrl and remaining parameters?

I want to configure simillarly to build.gradle :

artifactory {
   contextUrl ="someUrl"
    publish {
        repository {
            repoKey = "some"
            username = "someUsername"
            password = "somePassword"
        }
}

antonalechnovic avatar Jun 14 '20 00:06 antonalechnovic

You can apply the plugin with the PluginManager. You can get an instance from it from the Project.

The "closure" is an Gradle extension. Get an instance of it with project.extensions.getByType(ClassOfTheExtension::java:class). You can simply set the properties on it.

The class of the extension can be find somewhere in the source code of the applied plugin.

StefMa avatar Jun 14 '20 04:06 StefMa

There are also some examples here: https://docs.gradle.org/current/userguide/kotlin_dsl.html#groovy_closures_from_kotlin

JLLeitschuh avatar Jun 15 '20 20:06 JLLeitschuh