Gradle学习笔记(一)
2015-12-17 16:46
549 查看
这里开始写Gradle 的学习笔记,其实并不是现在才开始看这些内容,之前零零散散的看过Gradle的Demo,但是并没有系统的对学习的内容进行整理或者学习进度进行标记!现在开始做一些学习笔记的整理,开始系统的学习这个内容!
说句题外话,没接触过Ant,又没跟上Maven的步伐,Gradle说什么也得学习学习!哈哈……
其实官网的学习资料已经不少,推荐呃书籍,还有参加的课程!其实个人感觉一是书本,二是官方提供的api和Demo。
现在先从最简单的Demo中学习,如果有需要,会引用响应的API进行佐证!
分析:gradle 是以任务task进行划分的,怎么知道一个build.gradle里有几个task呢?
执行命令:
该命令列出的任务如下:
其中文件里自定义的任务是:hello!知道了任务,现在就开始运行任务了:
运行结果:
也可以在命令行里加参数:
q即quiet的简写,输出结果:
这个代码使用了一个符号:<<,这个符号什么意思呢(下面会指出)?我从文档(../gradle-2.8/docs/dsl/org.gradle.api.Task.html)里找到了这么一句话:
Groovy closures can also be used to provide a task action. When the action is executed, the closure is called with the task as parameter. You can add action closures to a task by calling Task.doFirst() or Task.doLast() or using the left-shift << operator.
我的理解是:Groovy closures也可以被用于提供一个任务动作。当执行这个动作时,the closure 可以当成一个任务task的参数来调用。可以用三种方式给一个任务task添加动作闭包(action closure 不知道怎么翻译):
调用任务的doFirst()方法
调用任务的doLast()方法
使用左(left-shift)操作符
PDF文档里也给出了类似的说法:
“Again,this defines a task called hello with a single closure to execute. ”用了一个单独的闭包来执行这个任务!
文档里有这么一段话来解释该代码:
The calls doFirst and doLast can be executed multiple times.
They add an action to the beginning or the end of the task’s actions list. When the task executes, the actions in the action list are executed in order. The << operator is simply an alias for doLast感觉加粗的部分是比较重要的。
个人理解是这么样的:给这个task添加了一个开始的动作和结束的动作到这个任务的动作列表里。当任务执行时,任务的动作列表是按顺序执行的。操作符号<<是doLast动作的简单别名!!
分析:代码中使用了
延伸问题:Task还有其他属性吗?见文档:../gradle-2.8/docs/dsl/org.gradle.api.Task.html
注意:上述代码使用了$,进行字符串或值的替换,但是println调用方法是使用了单引号和双引号,这有什么区别呢?
gradle中” 和”“的区别,可以从以下代码中看出来,不过里面的内容,需要到下一节会提到,仅作预览,看两个符号的区别!
代码:
运行结果:
由此可见:单引号只做字符串拼写,双引号做字符串值的处理!
说句题外话,没接触过Ant,又没跟上Maven的步伐,Gradle说什么也得学习学习!哈哈……
其实官网的学习资料已经不少,推荐呃书籍,还有参加的课程!其实个人感觉一是书本,二是官方提供的api和Demo。
现在先从最简单的Demo中学习,如果有需要,会引用响应的API进行佐证!
示例一:hello
代码位置:..\gradle-2.8\samples\userguide\tutorial\hello\build.gradletask hello { doLast { println 'Hello world!' } }
分析:gradle 是以任务task进行划分的,怎么知道一个build.gradle里有几个task呢?
执行命令:
gradle tasks --all
该命令列出的任务如下:
:tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] Help tasks ---------- components - Displays the components produced by root project 'hello'. [incubating] dependencies - Displays all dependencies declared in root project 'hello'. dependencyInsight - Displays the insight into a specific dependency in root project 'hello'. help - Displays a help message. model - Displays the configuration model of root project 'hello'. [incubating] projects - Displays the sub-projects of root project 'hello'. properties - Displays the properties of root project 'hello'. tasks - Displays the tasks runnable from root project 'hello'. Other tasks ----------- hello BUILD SUCCESSFUL Total time: 3.378 secs
其中文件里自定义的任务是:hello!知道了任务,现在就开始运行任务了:
gradle hello
运行结果:
:hello Hello world! BUILD SUCCESSFUL Total time: 2.863 secs
也可以在命令行里加参数:
gradle -q hello
q即quiet的简写,输出结果:
Hello world!
示例二:helloShortcut
代码:task hello << { println 'Hello world!' }
这个代码使用了一个符号:<<,这个符号什么意思呢(下面会指出)?我从文档(../gradle-2.8/docs/dsl/org.gradle.api.Task.html)里找到了这么一句话:
Groovy closures can also be used to provide a task action. When the action is executed, the closure is called with the task as parameter. You can add action closures to a task by calling Task.doFirst() or Task.doLast() or using the left-shift << operator.
我的理解是:Groovy closures也可以被用于提供一个任务动作。当执行这个动作时,the closure 可以当成一个任务task的参数来调用。可以用三种方式给一个任务task添加动作闭包(action closure 不知道怎么翻译):
调用任务的doFirst()方法
调用任务的doLast()方法
使用左(left-shift)操作符
<<操作
PDF文档里也给出了类似的说法:
“Again,this defines a task called hello with a single closure to execute. ”用了一个单独的闭包来执行这个任务!
示例三:helloEnhanced
代码:task hello << { println 'Hello Earth' } hello.doFirst { println 'Hello Venus' } hello.doLast { println 'Hello Mars' } hello << { println 'Hello Jupiter' }
文档里有这么一段话来解释该代码:
The calls doFirst and doLast can be executed multiple times.
They add an action to the beginning or the end of the task’s actions list. When the task executes, the actions in the action list are executed in order. The << operator is simply an alias for doLast感觉加粗的部分是比较重要的。
个人理解是这么样的:给这个task添加了一个开始的动作和结束的动作到这个任务的动作列表里。当任务执行时,任务的动作列表是按顺序执行的。操作符号<<是doLast动作的简单别名!!
示例四:helloWithShortCut
代码:task hello << { println 'Hello world!' }hello.doLast {
println "Greetings from the $hello.name task."
}
分析:代码中使用了
$hello.name,即使用了任务hello的属性值。
延伸问题:Task还有其他属性吗?见文档:../gradle-2.8/docs/dsl/org.gradle.api.Task.html
注意:上述代码使用了$,进行字符串或值的替换,但是println调用方法是使用了单引号和双引号,这有什么区别呢?
gradle中” 和”“的区别,可以从以下代码中看出来,不过里面的内容,需要到下一节会提到,仅作预览,看两个符号的区别!
代码:
task ss << { 5.times{ counter -> println 'currentNumber\':$counter' println "currentNumber\":$counter" } }
运行结果:
:ss currentNumber':$counter currentNumber":0 currentNumber':$counter currentNumber":1 currentNumber':$counter currentNumber":2 currentNumber':$counter currentNumber":3 currentNumber':$counter currentNumber":4
由此可见:单引号只做字符串拼写,双引号做字符串值的处理!
相关文章推荐
- 开发必会的 Linux & Mac 命令
- nginx如何响应客户端
- jquery中attr和prop的区别—判断复选框选中状态
- HTML - 键盘事件
- 使用sigsuspend函数捕捉到信号改变变量的值。
- Win8在哪里设置图片的密码?
- CocoaPods安装Alamofire Xcode7.1
- EJB面试题
- 3192: [JLOI2013]删除物品
- LZW压缩算法
- Linux指令--mv
- redis初探
- Spring 配置log4j和简单介绍Log4J的使用
- Linux指令--mv
- Cocos2dx 3.0 交流篇
- 浏览器下载文件
- 代码:cookie(一个广告展示例子)
- mysql 去除重复记录
- 欢迎使用CSDN-markdown编辑器
- 图片验证码生成