def range = 0..4
def coll = ["Groovy", "Java", "Ruby"]
def hash = [name:"Andy", "VPN-#":45]
- def xxx = {paramters -> code} //或者
- def xxx = {无参数,纯code} 这种case不需要->符号
![](https://i-blog.csdnimg.cn/blog_migrate/63bb1e69dbb4122361220873e8c267a6.png)
- doLast({
- println'Hello world!'
- })
def acoll = ["Groovy", "Java", "Ruby"] acoll.each{ println it }
it
变量是一个关键字,指向被调用的外部集合的每个值 — 它是默认值,可以用传递给闭包的参数覆盖它。- println 'Groovy world!'
执行 groovyc-d classes test.groovy
groovyc是groovy的编译命令,-d classes用于将编译得到的class文件拷贝到classes文件夹下
1. gradle projects查看工程信息
到目前为止,我们了解了Gradle什么呢?
l 每一个Project都必须设置一个build.gradle文件。至于其内容,我们留到后面再说。
l 对于multi-projects build,需要在根目录下也放一个build.gradle,和一个settings.gradle。
l 一个Project是由若干tasks来组成的,当gradlexxx的时候,实际上是要求gradle执行xxx任务。这个任务就能完成具体的工作。
![](https://i-blog.csdnimg.cn/blog_migrate/3a327f72b6e4ba5c5d70d50593798266.png)
gradleproject-path:tasks 就行。注意,project-path是目录名,后面必须跟冒号。
对于Multi-project,在根目录中,需要指定你想看哪个poject的任务。不过你要是已经cd到某个Project的目录了,则不需指定Project-path。
![](https://i-blog.csdnimg.cn/blog_migrate/63bf731130e8c351b157842adda9afd7.png)
- l Gradle有一个初始化流程,这个时候settings.gradle会执行。
- l 在配置阶段,每个Project都会被解析,其内部的任务也会被添加到一个有向图里,用于解决执行过程中的依赖关系。
- l 然后才是执行阶段。你在gradle xxx中指定什么任务,gradle就会将这个xxx任务链上的所有任务全部按依赖顺序执行一遍!
![](https://i-blog.csdnimg.cn/blog_migrate/65b072594a81c2c15589e6269218d5cf.png)
(1)生命周期
eg:Single project build example
settings.gradle
println 'This is executed during the initialization phase.'
build.gradle
println 'This is executed during the configuration phase.' task configured { println 'This is also executed during the configuration phase.' } task test << { println 'This is executed during the execution phase.' } task testBoth { doFirst { println 'This is executed first during the execution phase.' } doLast { println 'This is executed last during the execution phase.' } println 'This is executed during the configuration phase as well.' }
![](https://i-blog.csdnimg.cn/blog_migrate/95afa4b58dd93430e32491abd819ee64.png)
include 'project1', 'project2:child', 'project3:child1'
includeFlat 'project3', 'project4'
(3)根据生命周期定制task
eg:Logging of start and end of each task execution
build.gradle
task ok task broken(dependsOn: ok) << { thrownew RuntimeException('broken') } gradle.taskGraph.beforeTask { Task task -> println "executing $task ..." } gradle.taskGraph.afterTask { Task task, TaskState state -> if (state.failure) { println "FAILED" } else { println "done" } }
![](https://i-blog.csdnimg.cn/blog_migrate/ef2424ff6b835e7e7f385c02f7739c18.png)
dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile project(':bugrpt') }远程库依赖
apply plugin: 'java' repositories { mavenCentral() } dependencies { compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final' testCompile group: 'junit', name: 'junit', version: '4.+' }标识project需要hibernate-core一起编译,project test需要junit的编译,另外一种写法group:name:version
dependencies {
compile 'org.hibernate:hibernate-core:3.6.7.Final'
}
![](https://i-blog.csdnimg.cn/blog_migrate/5e418e4f6c492f495245d4dc527f771a.png)
eg:Usage of a remote Maven repository
build.gradle
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}
eg:Usage of a local Ivy directory
build.gradle
repositories { ivy { // URL can refer to a local directory url "../local-repo" } }
uploadArchives { repositories { ivy { credentials { username "username" password "pw" } url "http://repo.mycompany.com" } } }
![](https://i-blog.csdnimg.cn/blog_migrate/5817d5bfbd31ae6d29bd35107d184ae4.png)
buildTypes {
debug {
applicationIdSuffix ".debug"
}
jnidebug {
initWith(buildTypes.debug)
packageNameSuffix ".jnidebug"
jniDebuggable true
}
}
}
对于每一种buildTypes 会创建相应的ssemble<BuildTypeName>任务,比如debug会自动创建assembleDebug任务
![](https://i-blog.csdnimg.cn/blog_migrate/13d19e2ea88e506877bdd78ffabd75d6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/99d1f13a5eecee224def5524719aa05e.png)
$USER_HOME/.gradle/wrapper/dists
.
task hello {
doLast {
println 'Hello world!'
}
}
这个脚本定义了一个叫做hello的task,并且添加了一个action,这个action实际上是由groovy语言编写的闭包,更简洁的写法
task hello << {
println 'Hello world!'
}
> gradle -q hello
Hello world!
project('projectA') { task taskX(dependsOn: ':projectB:taskY') << { println 'taskX' } } project('projectB') { task taskY << { println 'taskY' } }
4.times { counter -> task "task$counter" << { println "I'm task number $counter" } } task0.dependsOn task2, task3
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties << {
println myTask.myProperty
}
5 设置默认
defaultTasks 'clean', 'run' task clean << { println 'Default Cleaning!' } task run << { println 'Default Running!' } task other << { println "I'm not a default task!" }6 可以添加HOOK
task distribution << { println "We build the zip with version=$version" } task release(dependsOn: 'distribution') << { println 'We release now' } gradle.taskGraph.whenReady {taskGraph -> if (taskGraph.hasTask(release)) { version = '1.0' } else { version = '1.0-SNAPSHOT' } }
println hello.name
println tasks.hello.name
tasks.getByPath(':projectA:hello').path
Configuring a task - with closure
task copy(type: Copy) { description 'Copies the resource directory to the target directory.' from 'resources' into 'target' include('**/*.txt', '**/*.xml', '**/*.properties') }