- Gradle基本簡介
Gradle構建腳本的書寫沒有基於傳統的XML文件,而是基於Groovy的領域專用語言,Gradle有約定優於配置的原則,相比Maven更容易上手。
- Groovy基本語法
Groovy是一種基於java虛擬機的動態語言
#定義變量
def a = '123'
#定義集合
def testList = ['123','456']
def testMap = ['key1':'123', 'key2':'456']
#集合添加元素
testList << '789'
testMap.key3 = '789'
#集合遍歷(it是個關鍵字代表每個元素)
testList.each{ println("${it}") }
testMap.each{ println it.getValue() }
#閉包方法的兩種寫法
def method1 = {
closure ->
closure()
}
def method4(Closure closure){
closure("測試")
}
def method2 = {
println('aaaaaaa')
}
def method3 = {
value ->
println("${value}")
}
method1 (method2)
method4 (method3)
- gradle基本使用
Gradle每次構建都包括至少一個項目,每個項目又包括一個或多個任務,每個build.gradle文件都代表一個項目任務定義在該文件的構建腳本里
#聲明插件,一版插件中會帶來很多已經寫好的任務
apply plugin: 'java'
#定義倉庫
repositories {
mavenCentral()
mavenLocal()
ivy {
credentials {
username "username"
password "pw"
}
url "http://repo.mycompany.com"
}
}
#定義依賴
dependencies {
#添加文件依賴
compile fileTree(dir: "libs", include: ["*.jar"])
#編譯階段
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
#測試階段
testCompile group: 'junit', name: 'junit', version: '4.+'
#測試和運行
runtime group: 'commons-codec', name: 'commons-codec', version: '1.+'
#測試運行時
testRuntime 'org.jmock:jmock-legacy:2.11.0'
#添加項目模塊作爲依賴
compile project(':library')
}
#定義project帶不進來的額外屬性
ext {
springVersion = "3.1.0.RELEASE"
emailNotification = "[email protected]"
}
#定義任務
task('hello') <<
{
println "hello"
}
#訪問任務
task hello
#重寫任務
task hello(overwrite: true) << {
println('I am the new one.')
}
#定義一個拷貝任務
task copyTask(type: Copy) {
from 'src/main/webapp'
into 'build/explodedWar'
}
- gradle基本命令行調用語法
gradle tasks //查看所有任務
gradle hello //調用hello任務
gradle在安卓中的使用,快速樓梯點我