Currently, I have a few utility functions defined in the top level build.gradle in a multi-project setup, for example like this:
def utilityMethod() {
doSomethingWith(project) // project is magically defined
}
I would like to move this code into a plugin, which will make the utilityMethod available within a project that applies the plugin. How do I do that? Is it a project.extension?
To add a Gradle plugin with dependencies, you can use code similar to the following: Plugin DSL GA versions. Plugin DSL non GA versions. Legacy Plugin Application.
You can simply execute the task named init in the directory where you would like to create the Gradle build. There is no need to create a “stub” build. gradle file in order to apply the plugin. The Build Init plugin also uses the wrapper task to generate the Gradle Wrapper files for the build.
Migrate one file at a timeRename the file to settings. gradle. kts and convert the file's contents to KTS. Make sure that your project still compiles after the migration of each build file.
Applying a plugin to a project allows the plugin to extend the project's capabilities. It can do things such as: Extend the Gradle model (e.g. add new DSL elements that can be configured) Configure the project according to conventions (e.g. add new tasks or configure sensible defaults)
This seems to work using:
import org.gradle.api.Plugin
import org.gradle.api.Project
class FooPlugin implements Plugin<Project> {
void apply(Project target) {
target.extensions.create("foo", FooExtension)
target.task('sometask', type: GreetingTask)
}
}
class FooExtension{
def sayHello(String text) {
println "Hello " + text
}
}
Then in the client build.gradle file you can do this:
task HelloTask << {
foo.sayHello("DOM")
}
c:\plugintest>gradle -q HelloTask
Hello DOM
https://docs.gradle.org/current/userguide/custom_plugins.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With