美文网首页项目构建
Gradle Task 入门 7 给task添加规则rule

Gradle Task 入门 7 给task添加规则rule

作者: 这个世界是虚拟的 | 来源:发表于2020-08-07 14:11 被阅读0次

    需求

    给之前的 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

    相关文章

      网友评论

        本文标题:Gradle Task 入门 7 给task添加规则rule

        本文链接:https://www.haomeiwen.com/subject/wxferktx.html