一、安装Gradle
1.首先确保你安装的JDK1.5或以上版本号。
C:\Users\chengxiang.peng.QUNARSERVERS>java -version java version "1.8.0_65" Java(TM) SE Runtime Environment (build 1.8.0_65-b17) Java HotSpot(TM) 64-Bit Server VM (build 25.65-b01, mixed mode)
2.从官方站点下载对应的版本号,将下载文件解压到某个文件夹;
C:\Users\chengxiang.peng.QUNARSERVERS>gradle -v ------------------------------------------------------------ Gradle 2.2.1 ------------------------------------------------------------ Build time: 2014-11-24 09:45:35 UTC Build number: none Revision: 6fcb59c06f43a4e6b1bcb401f7686a8601a1fb4a Groovy: 2.3.6 Ant: Apache Ant(TM) version 1.9.3 compiled on December 23 2013 JVM: 1.8.0_45 (Oracle Corporation 25.45-b02) OS: Windows 7 6.1 amd64
二、開始使用Gradle
1.每一个Gradle构建都是一个脚本開始的,构建默认的脚本名称是build.gradle。
当运行gradle命令的时候。Gradle会去寻找名字为build.gradle的文件。
假设找不到,就会显示一个帮助信息。
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle :help Welcome to Gradle 2.2.1. To run a build, run gradle <task> ... To see a list of available tasks, run gradle tasks To see a list of command-line options, run gradle --help BUILD SUCCESSFUL Total time: 3.132 secs
2.创建build.gradle文件,创建task叫做helloWorld,文件例如以下。并运行该任务:gradle -q helloWord;
build.gradle文件
task helloWorld{ //task运行的最后一个目标 doLast { println 'Hello world!' } }
运行task任务helloWord,通过-q定义可选命令行选项quiet。
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle -q helloWorld Hello world!
3.创建新的build.gradle文件,演示gradle的task依赖dependsOn来说明task之间的依赖,Gradle和Ant非常好的集成,定义动态task,运行时指定它们的名字;
build.gradle文件
task startSession << { chant() } def chant() { //隐含对Ant任务的使用 ant.echo(message: 'Repeat after me ...') } //动态任务的定义 3.times { task "yayGradle$it" << { println 'gradle tocks' } } //依赖任务 yayGradle0.dependsOn startSession yayGradle2.dependsOn yayGradle1, yayGradle0 task groupTherapy (dependsOn: yayGradle2)
运行gradle构建,运行命令gralde groupTherapy。gradle task运行顺序:startSession->yayGradle0->yayGradle1->yayGradle2->groupTherapy;
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle groupTherapy :startSession [ant:echo] Repeat after me ... :yayGradle0 gradle tocks :yayGradle1 gradle tocks :yayGradle2 gradle tocks :groupTherapy BUILD SUCCESSFUL Total time: 3.83 secs
三、使用Gradle的命令行
1.gradle提供了一个叫做tasks的帮助任务来帮助你查看构建脚本和显示每一个能够使用的task。包括描写叙述该task作用的信息;
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle -q tasks ------------------------------------------------------------ All tasks runnable from root project ------------------------------------------------------------ //任务组Build Setup,看做是多个task的集群 Build Setup tasks ----------------- init - Initializes a new Gradle build. [incubating] wrapper - Generates Gradle wrapper files. [incubating] //Help任务组。列出了任务名字和它们的描写叙述 Help tasks ---------- components - Displays the components produced by root project 'HelloWorld'. [incubating] dependencies - Displays all dependencies declared in root project 'HelloWorld'. dependencyInsight - Displays the insight into a specific dependency in root project 'HelloWorld'. help - Displays a help message. projects - Displays the sub-projects of root project 'HelloWorld'. properties - Displays the properties of root project 'HelloWorld'. tasks - Displays the tasks runnable from root project 'HelloWorld'. //假设某个task不属于不论什么一个任务组。那么它就会显示在Other tasks中 Other tasks ----------- groupTherapy To see all tasks and more detail, run with --all.
2.查看构建脚本中定义的其它的task;
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle -q tasks - -all … … Other tasks ----------- //依赖关系图的根task,以运行顺序用缩进的方式列出依赖任务的名字 groupTherapy startSession yayGradle0 yayGradle1 yayGradle2
四、任务运行
1.通过在命令行中通过多个參数,一次运行多个任务。
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle yayGradle0 groupTherapy :startSession [ant:echo] Repeat after me ... :yayGradle0 gradle tocks :yayGradle1 gradle tocks :yayGradle2 gradle tocks :groupTherapy BUILD SUCCESSFUL Total time: 3.826 secs
2.能够使用驼峰式的缩写在命令行上运行任务,任务名字的缩写必须是唯一的,Gradle才干找到对应的任务;
正确运行
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle yG0 gT :startSession [ant:echo] Repeat after me ... :yayGradle0 gradle tocks :yayGradle1 gradle tocks :yayGradle2 gradle tocks :groupTherapy BUILD SUCCESSFUL Total time: 3.8 secs
错误运行
build.gradle文件
task groupTherapy << { } task generateTests << { }
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle yG0 gT FAILURE: Build failed with an exception. * What went wrong: Task 'yG0' not found in root project 'HelloWorld'. * Try: Run gradle tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. BUILD FAILED Total time: 4.423 secs
3.在运行任务时排除一个任务,-x參数来实现;
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle groupTherapy -x //gradle排除了和它依赖的任务yayGradle0和startSession yayGradle0 :yayGradle1 gradle tocks :yayGradle2 gradle tocks :groupTherapy BUILD SUCCESSFUL Total time: 3.511 secs
五、命令行选项
1.-?,h,- -help:打印全部可用的命令行选项。包括描写叙述信息。
2.-b,--build-file:运行一个特定名字的构建脚本。
3.-i,--info:将Gradle的日志级别的改变到INFO以获得很多其它信息。
4.-s,--stacktrace:构建在运行出现错误,有异常抛出时会打印出简短的堆栈跟踪信息。
5.-q,--quiet:降低构建出错时打印出来的错误日志信息。
六、Gradle守护进程
1.守护进程以后台进程方式运行Gradle。一旦启动。gradle命令就会在兴许的构建中重用之前创建的守护进行,以避免启动时造成的开销;
2.--daemon选项,守护进行仅仅会被创建一次。即时你在命令加了--daemon选项。
3.守护进行会在3个小时空暇时之后自己主动活期。
4.--no-daemon,手动停止守护进程;
C:\Users\chengxiang.peng.QUNARSERVERS\GradleSources\HelloWorld>gradle groupTherapy --daemon :startSession [ant:echo] Repeat after me ... :yayGradle0 gradle tocks :yayGradle1 gradle tocks :yayGradle2 gradle tocks :groupTherapy BUILD SUCCESSFUL Total time: 2.171 secs