博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
gradle任务
阅读量:6847 次
发布时间:2019-06-26

本文共 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(Map
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) throws InvalidUserDataException" println "任务描述:${description}" }}
liuhailongdeAir:testdradle liuhailong$ gradle ex41CreateTask1  ex41CreateTask2  ex41CreateTask3 ex41CreateTask4> Task :ex41CreateTask1创建方法原型为:Task task(String name) throws InvalidUserDataException> Task :ex41CreateTask2创建方法原型为:Task task(Map
args, 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.gradle

ext {    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

你可能感兴趣的文章
eclipse 快捷键
查看>>
基于php&mysql实现聊天室功能的代码实例分享
查看>>
使用Qt实现简单的图片预览效果
查看>>
学点儿c#语言wpf开发
查看>>
microsoft office 2013 完全 卸载 工具 来自微软官方
查看>>
轻松安装IP地址位置查询工具nali
查看>>
LNMP编译安装配置+discuz
查看>>
我的友情链接
查看>>
让数据恢复工程师蛋疼的盘
查看>>
log4j日志文件在Servlet 容器中文件位置的通用写法
查看>>
SOAP消息架构
查看>>
使用DOM4J遍历文档
查看>>
ldconfig共享库
查看>>
我的友情链接
查看>>
Oracle中Clob类型处理解析
查看>>
研究与研发
查看>>
为php安装memcached扩展笔记
查看>>
静态代码分析工具
查看>>
在linux下,在校园网上网(经验分享)
查看>>
exchange 2016 EMS控制台报错Winrm HTTP 403
查看>>