美文网首页
Gralde任务

Gralde任务

作者: ag4kd | 来源:发表于2020-03-09 16:25 被阅读0次

Gralde任务的使用

准备工作

  • IDE:AndroidStudio 或者 IntellijIdea
  • Gradle 插件版本3.6.1

源码

Task

简单说一下,task就是一个操作,能够为完成某件事情。

创建任务的几种方式

直接以一个任务名的方式创建

/**
 * <p>Creates a {@link Task} with the given name and adds it to this project. Calling this method is equivalent to
 * calling {@link #task(java.util.Map, String)} with an empty options map.</p>
 *
 * <p>After the task is added to the project, it is made available as a property of the project, so that you can
 * reference the task by name in your build file.  See <a href="#properties">here</a> for more details</p>
 *
 * <p>If a task with the given name already exists in this project, an exception is thrown.</p>
 *
 * @param name The name of the task to be created
 * @return The newly created task object
 * @throws InvalidUserDataException If a task with the given name already exists in this project.
 */
Task task(String name) throws InvalidUserDataException;
  • nametask 的名字,在构建文件中可以通过这个名字来使用该任务。如果同名task已经存在,则会报异常。

该方法返回一个任务。

def Task task1 = task('task1')

或者

def task1 = task('task1')

或者,创一个任务对象并调

task task1 

任务名+闭包配置的方式

    /**
     * <p>Creates a {@link Task} with the given name and adds it to this project. Before the task is returned, the given
     * closure is executed to configure the task.</p> <p>After the task is added to the project, it is made
     * available as a property of the project, so that you can reference the task by name in your build file.  See <a
     * href="#properties">here</a> for more details</p>
     *
     * @param name The name of the task to be created
     * @param configureClosure The closure to use to configure the created task.
     * @return The newly created task object
     * @throws InvalidUserDataException If a task with the given name already exists in this project.
     */
    Task task(String name, Closure configureClosure);
task task2() {
    println("Before the task is returned, the given closure is executed to configure the task")
}
task2.doFirst {
    println('task 就像是一个 任务的 产生器1')
}
task2.doLast {
    println('task 就像是一个 任务的 产生器2')
}

任务名+任务配置的Map对象

def task3 = task(group: BasePlugin.BUILD_GROUP, "task3")
task3.doLast { peintln '任务名+任务配置的Map对象' }

/**
 不管哪种方式,任务的名字和任务变量是有区别的
 下面的task5是变量,而任务其实是 task7,task5只是用来配置task7的,而不是真正的任务
 */
def Task task5 = task('task7')

task5.doLast {
    println 'task7'
}

禁用某个任务

task5.enabled(false)

断言

/**
 * 任务的断言
 *
 * Task 有一个 onlyIf方法,该方法接受一个闭包作为参数
 * 如果该闭包返回true,则执行改任务,否则跳过该任务.
 *
 * 比如,程序在控制什么时候打什么包,什么时候执行单元测试等
 *
 * */

final String BUILD_APPS_ALL = 'all'
final String BUILD_APPS_SHOUFA = 'shoufa'
final String BUILD_APPS_EXCLUDE_SHOUFA = 'exclude_shoufa'

task(ex48QQRelease).doLast {
    println '打应用宝的包'
}

task(ex48HuaweiRelease).doLast {
    println '打华为的包'
}

task(ex48BaiduRealese).doLast {
    println '打百度的包'
}
task(ex48MiuiRelease).doLast {
    println '打小米的包'
}
task builder {
    group BasePlugin.BUILD_GROUP
    description("大打渠道包")
}

builder.dependsOn(ex48QQRelease, ex48BaiduRealese, ex48HuaweiRelease, ex48MiuiRelease)

ex48QQRelease.onlyIf {
    def execute = false
    if (project.hasProperty('build_apps')) {
        def buildApps = project.property('build_apps')
        if (BUILD_APPS_SHOUFA == buildApps || BUILD_APPS_ALL == buildApps) {
            execute = true
        } else {
            execute = false
        }
    } else {
        execute = true
    }
    execute
}
ex48BaiduRealese.onlyIf {
    def execute = false
    if (project.hasProperty('build_apps')) {
        def buildApps = project.property('build_apps')
        if (BUILD_APPS_SHOUFA == buildApps || BUILD_APPS_ALL == buildApps) {
            execute = true
        } else {
            execute = false
        }
    } else {
        execute = true
    }
    execute
}
ex48HuaweiRelease.onlyIf {
    def execute = false
    if (project.hasProperty('build_apps')) {
        def buildApps = project.property('build_apps')
        if (BUILD_APPS_EXCLUDE_SHOUFA == buildApps || BUILD_APPS_ALL == buildApps) {
            execute = true
        } else {
            execute = false
        }
    } else {
        execute = true
    }
    execute
}
ex48MiuiRelease.onlyIf {
    def execute = false
    if (project.hasProperty('build_apps')) {
        def buildApps = project.property('build_apps')
        if (BUILD_APPS_EXCLUDE_SHOUFA == buildApps || BUILD_APPS_ALL == buildApps) {
            execute = true
        } else {
            execute = false
        }
    } else {
        execute = true
    }
    execute
}
/**
 * 打所有包  gradle -Pbuild_apps=all builder
 * 打首发包  gradle -Pbuild_apps=shoufa builder
 * 打非首发包  gradle -Pbuild_apps=exclude_shoufa builder
 *
 * 命令中 -P的意思是为 Project 指定K-V格式的属性键值对,使用格式为 -PK=V
 * */

规则

/**
 * 任务规则
 * */

tasks.addRule('app_name') {
    String name ->
        task(name).doLast {
            println "该 ${name} 任务不存在,请查证后再执行"
        }
}
task ruleTest {
    //任务初始化
    println('任务规则')
    dependsOn missTask
}

相关文章

网友评论

      本文标题:Gralde任务

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