I have a dozen or so Kotlin build scripts that all have repeated code in them that belongs in a custom plugin. I searched for, found, then lost, a guide on how to do this and was able to migrate a configuration block to the new, standalone custom plugin. For example, I have a “signing” configuration block in all dozen or so project build scripts that looks like this:
which I was able to successfully move to the new custom plugin. The next step is to move the “signing” plugin declaration to the new custom plugin but I do not know how to do that yet and am looking for help. Bonus points for a link to the article I found and then lost, much to my chagrin.
Depends on how you implement your plugin.
If it is a precompiled script plugin, the syntax should be identical in your case.
If it is a regular Kotlin or Java plugin, you use the apply method.
@Vampire thanks for replying. I am using a standalone Kotlin custom plugin that looks like:
class IsaacPlugin : Plugin<Project> {
override fun apply(project: Project) {
val publishing: PublishingExtension = project.extensions.getByType(PublishingExtension::class.java)
// What goes here to indicate signing? Is it:
project.plugins.apply("signing") // which seems to compile ok.
project.configureSigning(publishing)
}
}
I guess I am asking: what is the correct syntax to apply the signing plugin in the IsaacPlugin code?
As for the article, I seem to recall it was a Medium article not a Gradle doc. It used an approach of gradually adding code to the custom plugin starting with the configuration blocks then moving on to applying the plugin and then finally to adding tasks. Best I can remember.
@Vampire I don’t suppose you can show me what the … should be for my case of the Signing plugin. Intellij tells me this:
None of the following functions can be called with the arguments supplied.
apply((Closure<Any!>..Closure<*>?)) defined in org.gradle.api.Project
apply((Mutable)Map<String!, *>!) defined in org.gradle.api.Project
apply(Action<in ObjectConfigurationAction!>!) defined in org.gradle.api.Project
and unfortunately I have not learned how to grok that message yet. I also could not find the JavaDoc you suggested. Can you give me a link to it please? Thanks.
@Vampire Thanks for the link. That worked but project.apply(plugin = "signing") gives me the error shown in my previous post. What did work was project.pluginManager.apply("signing").
The error just tells you that the method you try to use is not found and shows you which methods are found.
The apply I mentioned is a Kotlin DSL specific method, you need to import it into your Kotlin file.
Your IDE should tell you that. import org.gradle.kotlin.dsl.apply
@Vampire I’m not seeing the choices you cited. I’m guessing I need to add a Kotlin DSL plugin to build.gradle.kts for the plugin. Sure enough, when I added ‘kotlin-dsl’ to the plugins declaration, I got the recommended apply. Good to know.