Gradle 任务

来源: http://www.yiibai.com/gradle/gradle_tasks.html


任务用于编译一些类,将类文件存储到单独的目标文件夹中,创建JAR,生成Javadoc或将一些归档发布到存储库。


义任务


 task
 是用于将任务定义到构建脚本( build.gradle )中的关键字。

 eg;


定义 hello 任务,用于打印“ hello world ”字符串。

//build.gradle
task hello {
    doLast{
        println 'hello world'
    }
}

可以通过为 doLast 语句指定快捷方式(表示符号 << )来简化此 hello 任务。简化后脚本为:

//build.gradle
task hello << {
    println 'hello world'
}

任务依赖关系 

可以声明依赖于其它任务的任务。

使用 dependsOn 声明依赖关系,可以由此定义任务执行顺序



    主要有3中声明方式:

    1) 在任务名后面声明

eg:


task hello << {
    println "hello world "
}
task intro(dependsOn: hello) << {
    println "I'm Gradle"
}
执行结果:


 2) 也可以在task的配置区来声明它的依赖:

eg:

task hello << {
    println "hello world"
}
task intro {
    dependsOn hello // 定义依赖,注意中间没有 ':'
    doLast {        //  这种方式 不能使用 doLast的快捷方式
                    //  实际上 '<<' 快捷方式已经在Gradle 5.0+ 版本上被舍弃
        println 'I'm Gradle'
    }
}
执行结果:

    3) 使用 dependsOn 函数调用

eg:

task hello << {
    println "hello world"
}
task intro << {
    println "I'm Gradle"
}
intro.dependsOn hello // 声明依赖
执行结果:

添加任务描述

可以向任务添加描述。


通过 description 关键字定义描述,执行Gradle任务时会显示此描述。   


eg:


task copy(type:Copy){
	description 'Copies the resource directory to the target directory.'
	from 'resources'
	into 'target'
	include('**/*.txt','**/*.xml','**/*.properties')
	println("description applied")
}

执行结果:



跳过任务 


通过抛出 StopExecutionException 异常跳过任务。



如果操作抛出异常,则会跳过此操作的进一步执行以及此任务的后续执行。构建继续执行下一个任务。

eg:


task compile << {
	println("I'm compiling...");
}
compile.doFirst {
	// here you would put arbitrary conditions in real life
	if(true) { throw new StopExecutionException() }
}
task myTask(dependsOn: 'compile'){
	println("I'm not affected");
}

执行结果:




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值