盒子
盒子
Posts List
  1. 一. gradle运行环境配置
  2. 二.Gradle最简单的hello world
  3. 三.增加依赖
  4. 四.指定script脚本
  5. 五.执行出错不中断
  6. 六. 一些常见的命令

gradle实践系列:入门概览

一. gradle运行环境配置

下载gradle包,将GRADLE_HOME/bin添加到path中
运行

gradle -v

查看gradle是否配置成功,如成功,会显示当前gradle的版本,输入如下所示:

------------------------------------------------------------
Gradle 2.4
------------------------------------------------------------
Build time: 2015-05-05 08:09:24 UTC
Build number: none
Revision: 5c9c3bc20ca1c281ac7972643f1e2d190f2c943c
Groovy: 2.3.10
Ant: Apache Ant(TM) version 1.9.4 compiled on April 29 2014
JVM: 1.8.0_51 (Oracle Corporation 25.51-b03)
OS: Windows 8.1 6.3 amd64

二.Gradle最简单的hello world

task hello{
print 'Hello world'
}

将以上脚本保存到文件build.gradle中,注意文件名必须是这个,gradle会自动查找当前目录下的build.gradle脚本,并遍历其中的task。可以通过命令

gradle tasks

列出所有的可执行的任务,输入如下所示

hello world!!!: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 'Desktop'. [incubating]
dependencies - Displays all dependencies declared in root project 'Desktop'.
dependencyInsight - Displays the insight into a specific dependency in root project 'Desktop'.
help - Displays a help message.
model - Displays the configuration model of root project 'Desktop'. [incubating]
projects - Displays the sub-projects of root project 'Desktop'.
properties - Displays the properties of root project 'Desktop'.
tasks - Displays the tasks runnable from root project 'Desktop'.
Other tasks
-----------
hello
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL

可以看到在查找tasks时,会默认执行task hello中的输出,我理解,类似于java中的静态函数,在类加载过程中就会被执行。因此输出了

hello world!!!:tasks

对脚本稍作修改

task hello{
doLast{
print 'Hello world'
}
}

再次执行 gradle tasks,输出如下:

: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 'Desktop'. [incubating]
dependencies - Displays all dependencies declared in root project 'Desktop'.
dependencyInsight - Displays the insight into a specific dependency in root project 'Desktop'.
help - Displays a help message.
model - Displays the configuration model of root project 'Desktop'. [incubating]
projects - Displays the sub-projects of root project 'Desktop'.
properties - Displays the properties of root project 'Desktop'.
tasks - Displays the tasks runnable from root project 'Desktop'.
Other tasks
-----------
hello
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL

hello world没有再被输出,验证了我的想法。可以看到other tasks栏目,有列出我们定义的任务hello,执行一下任务

gradle -q hello

输出如下:

hello world!!!

如果执行 不加参数 -q ,输出如下

:hello
hello world!!!
BUILD SUCCESSFUL

其中doLast为 task中的方法,其他的方法还包括

  • dependsOn
  • doFirst
  • configure

我们再多定义一个任务

task hello{
doLast{
print 'hello world!!!'
}
}
task test{
doLast{
String name = 'hch'
print 'my name is ' + name
}
}

执行

gradle tasks

输出如下:

: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 'Desktop'. [incubating]
dependencies - Displays all dependencies declared in root project 'Desktop'.
dependencyInsight - Displays the insight into a specific dependency in root project 'Desktop'.
help - Displays a help message.
model - Displays the configuration model of root project 'Desktop'. [incubating]
projects - Displays the sub-projects of root project 'Desktop'.
properties - Displays the properties of root project 'Desktop'.
tasks - Displays the tasks runnable from root project 'Desktop'.
Other tasks
-----------
hello
test
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task <task>
BUILD SUCCESSFUL

执行test任务

gradle -q test

输出如下:

my name is hch

三.增加依赖

增加依赖

task hello{
doLast{
println 'hello world!!!'
}
}
task test{
dependsOn{
hello
}
doLast{
String name = 'hch'
println 'my name is ' + name
}
}
task test2(dependsOn:test){
doLast{
println 'execute task test2 '
}
}

执行

gradle -q test2

输出如下

:hello
hello world!!!
:test
my name is hch
:test2
execute task test2
BUILD SUCCESSFUL

可以定义多个依赖

task hello{
doLast{
println 'hello world!!!'
}
}
task test {
doLast{
String name = 'hch'
println 'my name is ' + name
}
}
task test2(dependsOn:[hello,test]){
doLast{
println 'execute task test2 '
}
}

四.指定script脚本

可以指定build脚本 ,命令形式为

gradle -b scriptpath/scriptname taskname

五.执行出错不中断

gradle --continue hello

增加continue参数,则gradle在构建中途出错,不会停止构建。

六. 一些常见的命令

备注一下
gradle -q tasks – 查看所有可执行任务
gradle -q help –task hello – 查看任务帮助


下一篇

gradle实践系列:插件与依赖

支持一下
扫一扫,支持牛头码农