Gradle:Gradle入门
2015-12-28 23:59
204 查看
一、安装Gradle 1.首先确保你安装的JDK1.5或以上版本;
2.从官方网站下载相应的版本,将下载文件解压到某个目录;
二、开始使用Gradle 1.每个Gradle构建都是一个脚本开始的,构建默认的脚本名称是build.gradle。当执行gradle命令的时候,Gradle会去寻找名字为build.gradle的文件。如果找不到,就会显示一个帮助信息;
2.创建build.gradle文件,创建task叫做helloWorld,文件如下。并运行该任务:gradle -q helloWord; build.gradle文件
运行gradle构建,执行命令gralde groupTherapy。gradle task执行顺序:startSession->yayGradle0->yayGradle1->yayGradle2->groupTherapy;
三、使用Gradle的命令行 1.gradle提供了一个叫做tasks的帮助任务来帮助你查看构建脚本和显示每个可以使用的task,包含描述该task作用的信息;
2.查看构建脚本中定义的其他的task;
2.可以使用驼峰式的缩写在命令行上运行任务,任务名字的缩写必须是唯一的,Gradle才能找到相应的任务; 正确执行
错误执行
build.gradle文件
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 secs3.在执行任务时排除一个任务,-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
相关文章推荐
- 浅谈CSRF攻击方式
- C++ map的基本操作和使用 (2009-09-23 14:58:21)
- 如何利用JConsole观察分析Java程序的运行并进行排错调优
- AutoLayout经常用到的一些布局(含StackView)
- Android Studio中AIDL的使用详解
- Oracle VM VirtualBox 打开复制虚机报错
- 高斯消元模板
- [android]_[绘图]
- 后台编译linux内核代码的方法
- 2015年12月28日 Java基础系列(六)流
- 【后缀数组】poj1743Musical Theme
- 【LVL1_6_c】我对(一维数组名、二维数组名、&数组名 )的初步认识和理解。
- 蓝懿 ios技术交流和心得分享12.28
- 数组类型基础
- OpenCV学习资料(PNG格式处理)
- C#实现电影院购票管理系统
- C# 中的异常捕捉(try)和异常处理(catch)
- Android viewpager+fragment实现动态加载碎片(fragment重复使用)
- java基础笔记--Map
- JVM垃圾回收示例