I want to create a gradle plugin that exports a fu...
# getting-started
j
I want to create a gradle plugin that exports a function to the build.gradle.kts file. The function will be in the scope of
org.gradle.api.artifacts.dsl.RepositoryHandler
. Is there any documentation that explains how to do that?
v
The best would be to add an extension to said repository handler, then it also works the same for both DSLs. But that is more a question for the Gradle Slack, as it is more Gradle related and actually not really Kotlin related when done properly. 😄
If you for example have in the plugin
Copy code
abstract class Foo {
    fun bar() {
        println("bar")
    }
}

(repositories as ExtensionAware).extensions.create<Foo>("foo")
then you can do downstream for example:
Copy code
repositories {
    foo.bar()
}
or
Copy code
repositories {
    foo {
        bar()
    }
}
and in both, Groovy DSL and Kotlin DSL
j
How would I create a function for the
org.gradle.api.artifacts.dsl.RepositoryHandler
scope? So I could do
Copy code
repositories {
  foo("bar", "baz")
}
v
You shouldn't
I mean, if you don't care about Groovy DSL you can of course simply make an extension function on
RepositoryHandler
👍 1
j
You shouldn't
Why?
v
Because that is not the idiomatic Gradle way. And it is good that it is clear where this comes from by having an additional namespace. And it works in all supported DSLs.
1
j
I see! That's a good explanation, thank you! 😄
v
Also by using an extension you can control exactly on which this is supported, instead of all of that type