Gradle学习笔记(一)

这里开始写Gradle 的学习笔记,其实并不是现在才开始看这些内容,之前零零散散的看过Gradle的Demo,但是并没有系统的对学习的内容进行整理或者学习进度进行标记!现在开始做一些学习笔记的整理,开始系统的学习这个内容!
说句题外话,没接触过Ant,又没跟上Maven的步伐,Gradle说什么也得学习学习!哈哈……

其实官网的学习资料已经不少,推荐呃书籍,还有参加的课程!其实个人感觉一是书本,二是官方提供的api和Demo。
现在先从最简单的Demo中学习,如果有需要,会引用响应的API进行佐证!

示例一:hello

代码位置:..\gradle-2.8\samples\userguide\tutorial\hello\build.gradle

task 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 不知道怎么翻译):

  1. 调用任务的doFirst()方法
  2. 调用任务的doLast()方法
  3. 使用左(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

由此可见:单引号只做字符串拼写,双引号做字符串值的处理!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值