为了简化我的构建,我想从“build.gradle”中提取自定义插件并将它们放入单独的 gradle 文件中。
这是我能想到的最简单的例子。
1. 在 build.gradle (WORKS) 中定义一个插件
以下“build.gradle”:
apply plugin: GreetingPlugin
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello') << {
println "Hello, World"
}
}
}
当你运行时:
gradle hello
产生:
:hello
Hello, World
BUILD SUCCESSFUL
2. 从 build.gradle 中提取插件 (DOESN'T WORK)
我想将插件定义移动到另一个文件“hello.gradle”中:
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello') << {
println "Hello, World"
}
}
}
并将“build.gradle”更改为:
apply from: 'hello.gradle'
apply plugin: GreetingPlugin
现在当我运行时:
gradle hello
它产生:
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
我相信这一定是可能的,那么你是怎么做到的呢?