How to move gradle function from build.gradle to plugin?

I currently have several utility functions defined in the top level build.gradle in multi-project setup, for example:

def utilityMethod() {
    doSomethingWith(project) // project is magically defined
}

I would like to move this code to a plugin that will make the Method utility available as part of a project that uses the plugin. How should I do it? This is a project. Expansion?

+4
source share
3 answers

Plugins are not intended to provide general methods, but tasks.

When it comes to extensions, they should be used to collect input for application plugins:

Most plugins should get some configuration from the build script. One way to do this is to use extension objects.

.

Peter , , ext, , .

0

, :

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
    }
}

build.gradle :

task HelloTask << {
    foo.sayHello("DOM")
}

c:\plugintest>gradle -q HelloTask
Hello DOM

https://docs.gradle.org/current/userguide/custom_plugins.html

+2

, Github.

target.ext.utilityMethod = SomeClass.&utilityMethod

:
- , , .

23290820.

+1

All Articles