Search code examples
gradlegradle-kotlin-dsl

How to reference "ext" property provided by a Groovy script plugin in my Kotlin Gradle script?


I am trying to migrate a build.gradle to build.gradle.kts and am stuck with this error:

  Line 036: ext.pluginInfo.licenses = ["Apache-2.0"]
                ^ Unresolved reference: pluginInfo

The original build.gradle "includes" another script and then references pluginInfo from that script:

apply from: "rubyUtils.gradle"
pluginInfo.licenses = ["Apache-2.0"]

This is the (shortened) part from rubyUtils.gradle that provides pluginInfo (I guess):

ext {
    pluginInfo = new PluginInfo()
}

Here is what I have come up with in Kotlin. This causes the error mentioned at the beginning:

apply(from = "rubyUtils.gradle")
ext.pluginInfo.licenses = ["Apache-2.0"]

Questions:

  1. Is it possible at all to "import" a Groovy Gradle script in a Kotlin script (apply a Groovy script plugin in a Kotlin script)?
  2. If it is possible: How do I correctly refer to pluginInfo?
  3. And what is that whole "ext" thing anyway? :-)

Solution

    1. Yes, you can import Groovy scripts in Kotlin and vice-versa. Unfortunately, you might encounter class loading issues.
    2. val pluginInfo by extra is the notation you are looking for according to the documentation
    3. In short, they are an extension system that has been in Gradle for a long time and is to be used with care. Again, have a look at the documentation linked above for more details.