需求
给之前的 release 任务添加规则,如果task name 是 release 那么输出正在release
代码
tasks.addRule("do when calling release") {
String taskName ->
if (taskName.startsWith('release')) {
logger.quiet 'applying rule to the task'
}
}
执行
//gradlew release
> Task :makeReleaseVersion
do task action ... ...
releaseVersion-prefix
releaseVersion-prefix
> Task :compileJava UP-TO-DATE
applying rule to the task
> Task :release
releasing the project...
如果使用 -i 参数,那么可以观察到,我们定义的rule会在编译时就被调用:
> Task :compileJava UP-TO-DATE
Resolving global dependency management for project 'demo'
//看这里:
applying rule to the task
Excluding []
Excluding []
Caching disabled for task ':compileJava' because:
Build cache is disabled
Skipping task ':compileJava' as it is up-to-date.
:compileJava (Thread[Daemon worker Thread 4,5,main]) completed. Took 0.541 secs.
:processResources (Thread[Execution worker for ':',5,main]) started.
总结
我们可以运用rule,队代码进行一定的预处理,比如遇到参数 A 那么调用Task A,遇到参数 B 调用Task B
网友评论