本文共 9168 字,大约阅读时间需要 30 分钟。
多种方式创建任务
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(Mapargs, 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) throws InvalidUserDataException" println "任务描述:${description}" }}
liuhailongdeAir:testdradle liuhailong$ gradle ex41CreateTask1 ex41CreateTask2 ex41CreateTask3 ex41CreateTask4> Task :ex41CreateTask1创建方法原型为:Task task(String name) throws InvalidUserDataException> Task :ex41CreateTask2创建方法原型为:Task task(Mapargs, String name) throws InvalidUserDataException任务分组:build> Task :ex41CreateTask3创建方法原型为:Task task(String name, Closure configureClosure)任务描述:演示任务创建> Task :ex41CreateTask4创建方法原型为:Task create(String name, Closure configureClosure) throws InvalidUserDataException任务描述:演示任务创建BUILD SUCCESSFUL in 2s4 actionable tasks: 4 executedliuhailongdeAir:testdradle liuhailong$
多种方式访问任务
task ex42AccessTask1ex42AccessTask1.doLast { println 'ex42AccessTask1.doLast'}task ex42AccessTask2tasks['ex42AccessTask2'].doLast { println 'ex42AccessTask2.doLast'}task ex42AccessTask3tasks['ex42AccessTask3'].doLast { println '集合方式' println tasks.findByPath(':example42:ex42AccessTask3') println tasks.getByPath(':example42:ex42AccessTask3') println tasks.findByPath(':example42:asdfasdfasdf')}task ex42AccessTask4tasks['ex42AccessTask4'].doLast { println tasks.findByName('ex42AccessTask4') println tasks.getByName('ex42AccessTask4') println tasks.findByName('asdfasdfasdf')}
liuhailongdeAir:testdradle liuhailong$ gradle ex42AccessTask1 ex42AccessTask2 ex42AccessTask3 ex42AccessTask4> Task :ex42AccessTask1ex42AccessTask1.doLast> Task :ex42AccessTask2ex42AccessTask2.doLast> Task :ex42AccessTask3 FAILED集合方式nullFAILURE: Build failed with an exception.* Where:Build file '/Users/liuhailong/Desktop/testdradle/build.gradle' line: 15* What went wrong:Execution failed for task ':ex42AccessTask3'.> Task with path ':example42:ex42AccessTask3' not found in root project 'testdradle'.* Try:Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.* Get more help at https://help.gradle.orgBUILD FAILED in 0s3 actionable tasks: 3 executedliuhailongdeAir:testdradle liuhailong$
任务分组和描述
def Task myTask = task ex43GroupTaskmyTask.group = BasePlugin.BUILD_GROUPmyTask.description = '这是一个构建的引导任务'myTask.doLast { println "group:${group},description:${description}"}
liuhailongdeAir:testdradle liuhailong$ gradle ex43GroupTask> Task :ex43GroupTaskgroup:build,description:这是一个构建的引导任务BUILD SUCCESSFUL in 0s1 actionable task: 1 executedliuhailongdeAir:testdradle liuhailong$
<<操作符其实为 doLast 方法的短标记形式 相当你简写
任务的执行分析
def Task myTask = task ex45CustomTask(type: CustomTask)myTask.doFirst{ println 'Task执行之前执行 in doFirst'}myTask.doLast{ println 'Task执行之后执行 in doLast'}class CustomTask extends DefaultTask { @TaskAction def doSelf() { println 'Task自己本身在执行 in doSelf' }}
liuhailongdeAir:testdradle liuhailong$ gradle ex45CustomTask> Task :ex45CustomTaskTask执行之前执行 in doFirstTask自己本身在执行 in doSelfTask执行之后执行 in doLastBUILD SUCCESSFUL in 0s1 actionable task: 1 executedliuhailongdeAir:testdradle liuhailong$
任务顺序与启用 禁用
task ex46OrderTask1 << { println 'ex46OrderTask1'}task ex46OrderTask2 << { println 'ex46OrderTask2'}ex46OrderTask1.mustRunAfter ex46OrderTask2task ex47DisenabledTask << {println 'ex47DisenabledTask'}//ex47DisenabledTask.enabled =falseex47DisenabledTask.enabled =false
liuhailongdeAir:testdradle liuhailong$ gradle ex46OrderTask1 ex47DisenabledTask> Task :ex46OrderTask1ex46OrderTask1> Task :ex47DisenabledTaskex47DisenabledTaskDeprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.Use '--warning-mode all' to show the individual deprecation warnings.See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s2 actionable tasks: 2 executedliuhailongdeAir:testdradle liuhailong$ gradle ex46OrderTask1 ex47DisenabledTask> Task :ex46OrderTask1ex46OrderTask1Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.Use '--warning-mode all' to show the individual deprecation warnings.See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s1 actionable task: 1 executedliuhailongdeAir:testdradle liuhailong$
断言
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,ex48MiuiReleaseex48QQRelease.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}
liuhailongdeAir:testdradle liuhailong$ gradle ex48QQRelease ex48BaiduRelease ex48HuaweiRelease ex48MiuiRelease> Task :ex48QQRelease打应用宝的包> Task :ex48BaiduRelease打百度的包> Task :ex48HuaweiRelease打华为的包> Task :ex48MiuiRelease打Miui的包Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.Use '--warning-mode all' to show the individual deprecation warnings.See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s4 actionable tasks: 4 executedliuhailongdeAir:testdradle liuhailong$
任务规则
tasks.addRule("对该规则的一个描述,便于调试、查看等") { String taskName -> task(taskName) << { println "该${taskName}任务不存在,请查证后再执行" }}task ex49RuleTask { dependsOn missTask}
liuhailongdeAir:testdradle liuhailong$ gradle ex49RuleTask> Task :missTask该missTask任务不存在,请查证后再执行Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.Use '--warning-mode all' to show the individual deprecation warnings.See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s1 actionable task: 1 executedliuhailongdeAir:testdradle liuhailong$
插件
//version.gradleext { versionName = '1.0.0' versionCode = 1}
build.gradle
plugins { id 'java'}apply plugin:'java'apply plugin:org.gradle.api.plugins.JavaPluginapply plugin:JavaPluginapply { plugin 'java' println "java"}apply(new Action() { @Override void execute(ObjectConfigurationAction objectConfigurationAction) { objectConfigurationAction.plugin('java') }})apply from:'version.gradle'task ex52PrintlnTask << { println "App版本是:${versionName},版本号是:${versionCode}"}
liuhailongdeAir:testdradle liuhailong$ gradle ex52PrintlnTask> Configure project :java> Task :ex52PrintlnTaskApp版本是:1.0.0,版本号是:1Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.Use '--warning-mode all' to show the individual deprecation warnings.See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warningsBUILD SUCCESSFUL in 0s1 actionable task: 1 executedliuhailongdeAir:testdradle liuhailong$
转载于:https://blog.51cto.com/haidragon/2173999