Gradle 学习笔记(三)

在看之前,先浏览一下[学习笔记(二)](http://blog.youkuaiyun.com/zouchengxufei/article/details/50117195,一些东西需要引用学习笔记(二)里的!

任务一:tutorial\dynamicDepends

代码:

4.times { counter ->
    task "task$counter" << {
        println "I'm task number $counter"
    }
}
task0.dependsOn task2, task3

分析,这段代码只有一行是新增的东西。“task0.dependsOn task2, task3”,因为任务是动态生成的,所以使用具体的任务时,要指定到某个任务,task0依赖task2和task3。

任务二:tutorial\projectApi

代码:

println name
println project.name

task check << {
}

分析:上述代码就只做了一件事,调用Project的API属性,该API在什么位置呢?位于:file:///D:/Java/Gradle/gradle-2.8/docs/dsl/org.gradle.api.Project.html#N141C9
修改一下你的sdk目录就可以看到,关于Project的属性,在Project里,它的属性时直接可以使用的,例如:
上面代码中调用的project,就是返回这个工程!
project
Returns this project. This method is useful in build files to explicitly access project properties and methods. For example, using project.name can express your intent better than using name. This method also allows you to access project properties from a scope where the property may be hidden, such as, for example, from a method or closure.

任务三:\tutorial\localVariables

代码:

def dest = "dest"

task copy(type: Copy) {
    from "source"
    into dest
}

在运行这个脚本之前,现在该目录下建立个文件夹为source的目录,然后在执行!结果是将目录source复制到dest目录下,如果不存在dest目录,先创建再复制!
分析:使用def 进行字符串的定义

任务四:tutorial\replaceTask

代码:

task copy(type: Copy)

task copy(overwrite: true) << {
    println('I am the new one.')
}

代码分析:overwrite 这个东西在哪?api有提供,gradle-2.8/docs/dsl/org.gradle.api.Project.html(或者这里:https://docs.gradle.org/current/dsl/org.gradle.api.Project.html),可以从这里找到关于overwrite的内容!
脚本第一行定义了一个类型为Copy的task,该任务的名字为copy。第二行用了task的可选参数,重写上面已经定义的任务。如果把overwrite的属性变为false,则会提示:


FAILURE: Build failed with an exception.

* Where:
Build file 'D:\StudySpaces\Gradle_study\studying\tutorial\replaceTask\build.gradle' line: 3

* What went wrong:
A problem occurred evaluating root project 'replaceTask'.
> Cannot add task ':copy' as a task with that name already exists.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.225 secs

意思就是:已经存在名为copy的task了,不能再添加这个task了。

任务五:tutorial\taskOnlyIf

代码:

task hello << {
    println 'hello world'
}

hello.onlyIf { !project.hasProperty('skipHello') }

分析:上述代码定义了一个普通的task任务,然后调用了Task的一个方法:onlyIf,查找Task的api(文档所在位置:gradle-2.8/docs/dsl/org.gradle.api.Task.html#N16CF8),找到这个方法,
onlyIf(onlyIfClosure)
Execute the task only if the given closure returns true. The closure will be evaluated at task execution time, not during configuration. The closure will be passed a single parameter, this task. If the closure returns false, the task will be skipped.
onlyIf(onlyIfSpec)
Execute the task only if the given spec is satisfied. The spec will be evaluated at task execution time, not during configuration. If the Spec is not satisfied, the task will be skipped.
就是说,如果条件语句为true,任务会执行。而条件语句:!project.hasProperty('skipHello') ,调用了Project的是否有某个属性的方法,Project文档里并没有这个属性,所以 !fasle,即true,会执行该task任务!

任务六:tutorial\upper

代码:

task upper << {
    String someString = 'mY_nAmE'
    println "Original: " + someString 
    println "Upper case: " + someString.toUpperCase()
}

分析:上述任务的功能很简单,其实就是将字符串由小写全部变成大写!由于gradle是基于groovy的,上述代码String,完全是基于Java的,调用的Java方法进行大小写转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值