No1:
多种方式创建任务
def Task ex41CreateTask1 = task(ex41CreateTask1) ex41CreateTask1.doLast{ println "创建方法原型为:Task task(String name)throws InvalidUserDataException" } def Task ex41CreateTask2 = task(ex41CreateTask2,group:BasePlugin.BUILD_GROUP) ex41CreateTask2.doLast{ println "创建方法原型为:Task task(Map<String,?> args,String name)throws InvalidUserDataException" println "任务分组:${ex41CreateTask2.group}" } task ex41CreateTask3{ description ‘演示任务创建‘ doLast{ println "创建方法原型为:Task task(String name,Closure configureClosure)" println "任务描述:${description}" } } tasks.create(‘ex41CreateTask4‘){ description ‘演示任务创建‘ doLast{ println "创建方法原型为:Task create(String name,Closure configureClosure) thorws InvalidUserDataException" println "任务描述:${description}" } }
结果
No2:
Task参数中Map的可用配置
No3:
多种方式访问任务
task ex42AccessTask1 ex42AccessTask1.doLast{ println ‘ex42AccessTask1.doLast‘ } task ex42AccessTask2 tasks[‘ex42AccessTask2‘].doLast{ println ‘ex42AccessTask2.doLast‘ } task ex42AccessTask3 tasks[‘ex42AccessTask3‘].doLast{ println tasks.findByPath(‘:example42:ex42AccessTask3‘) //println tasks.getByPath(‘:example42:ex42AccessTask3‘) println tasks.findByPath(‘:example42:asdfasdfasdf‘) } task ex42AccessTask4 tasks[‘ex42AccessTask4‘].doLast{ println tasks.findByName(‘ex42AccessTask4‘) println tasks.getByName(‘ex42AccessTask4‘) println tasks.findByName(‘asdfasdfasdf‘) }
结果
注:
任务都是通过TaskContainer创建的,其实TaskContainer就是我们创建任务的集合,在Project中我们可以通过tasks属性访问TaskContainer,所以我们就可以以访问集合元素的方式访问我们创建的任务
通过路径或者名称访问都有两种方式,get或find。区别在于get的时候如果找不到该任务就会抛出UnknownTaskException异常,而find会返回null
通过路径访问的时候,参数值可以是任务路径也可以是任务的名字。但通过名字访问的时候,参数值只能是任务的名字不能为路径
No4:
任务分组和描述
def Task myTask = task ex43GroupTask myTask.group = BasePlugin.BUILD_GROUP myTask.description=‘这是一个构建的引导任务‘ myTask.doLast{ println "group:${group},description:${description}" }
通过gradlew tasks查看
No5:
<<代替了doLast
task(ex44DoLast)<<{ println "ex44DoLast" } task(ex44DoLast).doLast{ println "ex44DoLast" }
No6:
doLast和leftShift源码比较
public Task doLast(final Closure action){ hasCustomActions = true; if(action==null){ throw new InvalidUserDataException("Action must not be null!"); } taskMutator.mutate("Task.doLast(Closure)",new Runnable(){ public void run(){ actions.add(convertClosureToAction(action)) } }); return this; } public Task leftShift(final Closure action){ hasCustomActions = true; if(action==null){ throw new InvalidUserDataException("Action must not be null!"); } taskMutator.mutate("Task.leftShift(Closure)",new Runnable(){ public void run(){ actions.add(taskMutator.leftShift(convertClosureToAction(action))); } }); return this; }
注:actions.add()是把我们配置的操作转换为Action放在actions这个List里,是直接放在List的末尾
No7:
执行一个Task的时候,其实就是执行其拥有的actions列表,这个列表保存在Task对象实例中的actions成员变量中,其类型是一个List:
private List<ContextAwareTaskAction> actions = new ArrayList<ContextAwareTaskAction>();
No8:
任务的执行分析
def Task myTask = task ex45CustomTask(type:CustomTask) myTask.doFirst{ println ‘Task执行之前执行in foFirst‘ } myTask.doLast{ println ‘Task执行之后执行in doLast‘ } class CustomTask extends DefaultTask{ @TaskAction def doSelf(){ println ‘Task自己本身在执行in doSelf‘ } }
结果
注:TaskAction注解标注表示该方法就是Task本身执行要执行的方法
No9:
doFirst和doLast源码
public Task doFirst(final Closure action){ hasCustomActions = true; if(action == null){ throw new InvalidUserDataException("Action must not be null!"); } taskMutator.mutate("Task.doFirst(Closure)",new Runnable(){ public void run(){ actions.add(0,convertClosureToAction(action)); } }); return this; } public Task doLast(final Closure action){ hasCustomActions = true; if(action == null){ throw new InvalidUserDataException("Action must not be null!"); } taskMutator.mutate("Task.doLast(Closure)",new Runnable(){ public void run(){ actions.add(convertClosureToAction(action)); } }); return this; }
doFirst永远都是在actions List第一位添加;doLast永远都是在actions List元素的最后面
No10:
任务排序
task ex46OrderTask1<<{ println ‘ex46OrderTask1‘ } task ex46OrderTask2<<{ println ‘ex46OrderTask2‘ } ex46OrderTask1.mustRunAfter ex46OrderTask2
结果
shouldRunAfter是应该而不是必须,所以有可能任务顺序并不会按预设的执行
mustRunAfter这个规则就比较严格
No11:
任务的启用和禁用
task ex47DisenabledTask<<{ println ‘ex47DisenabledTask‘ } ex47DisenabledTask.enabled = false
结果
No12:
任务的onlyIf断言
final String BUILD_APPS_ALL="all"; final String BUILD_APPS_SHOUFA="shoufa"; final String BUILD_APPS_EXCLUDE_SHOUFA="exclude_shoufa"; task ex48QQRelease<<{ println "打应用宝的包" } task ex48BaiduRelease<<{ println "打百度的包" } task ex48HuaweiRelease<<{ println "打华为的包" } task ex48MiuiRelease<<{ println "打MiUi的包" } task build{ group BasePlugin.BUILD_GROUP description "打渠道包" } build.dependsOn ex48QQRelease,ex48BaiduRelease,ex48HuaweiRelease,ex48MiuiRelease ex48QQRelease.onlyIf{ def execute = false; if(project.hasProperty("build_apps")){ Object buildApps = project.property("build_apps") if(BUILD_APPS_SHOUFA.equals(buildApps) || BUILD_APPS_ALL.equals(buildApps)){ execute = true; }else{ execute = false; } }else{ execute = true; } execute } ex48BaiduRelease.onlyIf{ def execute = false; if(project.hasProperty("build_apps")){ Object buildApps = project.property("build_apps") if(BUILD_APPS_SHOUFA.equals(buildApps) || BUILD_APPS_ALL.equals(buildApps)){ execute = true; }else{ execute = false; } }else{ execute = true; } execute } ex48HuaweiRelease.onlyIf{ def execute = false; if(project.hasProperty("build_apps")){ Object buildApps = project.property("build_apps") if(BUILD_APPS_EXCLUDE_SHOUFA.equals(buildApps) || BUILD_APPS_ALL.equals(buildApps)){ execute = true; }else{ execute = false; } }else{ execute = true; } execute } ex48MiuiRelease.onlyIf{ def execute = false; if(project.hasProperty("build_apps")){ Object buildApps = project.property("build_apps") if(BUILD_APPS_EXCLUDE_SHOUFA.equals(buildApps) || BUILD_APPS_ALL.equals(buildApps)){ execute = true; }else{ execute = false; } }else{ execute = true; } execute }
结果
No13:
任务规则
tasks.addRule("对该规则的一个描述,便于调试、查看等"){ String taskName->task(taskName)<<{ println "该${taskName}任务不存在,请查证后再执行" } } task ex49RuleTask{ dependsOn missTask }
结果
注:我们可以使用规则制作成,当执行、依赖不存在的任务时,不会执行失败,而是打印提示信息,提示该任务不存在