To simplify my build, I want to extract custom plugins from "build.gradle" and put them into separate gradle files.
Here's the simplest example I can contrive.
The following "build.gradle":
apply plugin: GreetingPlugin
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello') << {
println "Hello, World"
}
}
}
When you run:
gradle hello
Produces:
:hello
Hello, World
BUILD SUCCESSFUL
I want to move the plugin definition into another file "hello.gradle":
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello') << {
println "Hello, World"
}
}
}
And change the "build.gradle" to be:
apply from: 'hello.gradle'
apply plugin: GreetingPlugin
Now when I run:
gradle hello
It produces:
FAILURE: Build failed with an exception.
* Where:
Build file 'build.gradle' line: 2
* What went wrong:
A problem occurred evaluating root project 'gradle-problem'.
> Could not find property 'GreetingPlugin' on root project 'gradle-problem'.
BUILD FAILED
Total time: 1.723 secs
I'm sure this must be possible, so how do you do it?
I've found two reasonable solutions:
hello.gradle:
apply plugin: GreetingPlugin
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello').doLast {
println "Hello, World"
}
}
}
build.gradle:
apply from: 'hello.gradle'
ext propertyhello.gradle:
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello').doLast {
println "Hello, World"
}
}
}
ext.GreetingPlugin = GreetingPlugin
build.gradle:
apply from: 'hello.gradle'
apply plugin: GreetingPlugin
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