标签:
def range = 0..4
def coll = ["Groovy", "Java", "Ruby"]
def hash = [name:"Andy", "VPN-#":45]
def acoll = ["Groovy", "Java", "Ruby"]
acoll.each{
println it
}
it 变量是一个关键字,指向被调用的外部集合的每个值
— 它是默认值,可以用传递给闭包的参数覆盖它。执行 groovyc-d classes test.groovy
groovyc是groovy的编译命令,-d classes用于将编译得到的class文件拷贝到classes文件夹下
到目前为止,我们了解了Gradle什么呢?
l 每一个Project都必须设置一个build.gradle文件。至于其内容,我们留到后面再说。
l 对于multi-projects build,需要在根目录下也放一个build.gradle,和一个settings.gradle。
l 一个Project是由若干tasks来组成的,当gradlexxx的时候,实际上是要求gradle执行xxx任务。这个任务就能完成具体的工作。
gradleproject-path:tasks 就行。注意,project-path是目录名,后面必须跟冒号。
对于Multi-project,在根目录中,需要指定你想看哪个poject的任务。不过你要是已经cd到某个Project的目录了,则不需指定Project-path。
(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.‘ }
include ‘project1‘, ‘project2:child‘, ‘project3:child1‘
includeFlat ‘project3‘, ‘project4‘
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"
}
}
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‘
}
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"
}
}
}
buildTypes { debug { applicationIdSuffix ".debug" } jnidebug {
initWith(buildTypes.debug) packageNameSuffix ".jnidebug" jniDebuggable true } }}对于每一种buildTypes 会创建相应的ssemble<BuildTypeName>任务,比如debug会自动创建assembleDebug任务
$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‘)
}
标签:
原文地址:http://blog.csdn.net/asmcvc/article/details/51025807