Gradle筆記
6.1
每個構建包含一個或多個 "Project"
每個project包含一個或多個 "Task",每個 task 都是一個原子操作,或是編譯一些檔案,或是打jar包 ,或是生成javadoc
6.2
你可以用gradle命令來呼叫當前目錄下的build.gradle檔案,build.gradle通常稱之為構建指令碼;構建指令碼中定義了一個專案和包含的任務
hello world:第一個構建指令碼
在D盤根目錄建立build.gradle的檔案,內容如下:
task hello {
doLast {
println 'Hello world!'
}
}
在cmd下輸入
d:
gradle -q hello
上面的例項定義了一個叫hello的task;當執行gradle hello時便會返回所提供的方法的結果
6.3任務依賴
如下指令碼
task hello << {
println 'Hello world'
}
task intro(dependsOn: hello)<<{
println 'Depends'
}
--------------------
執行
>gradle -q intro
Hello world!
I'm Gradle
6.6 動態任務
4.times { counter ->
task "task$counter" << {
println "I'm task number $counter"
}
}
-----------------------------------
Output of gradle -q task1
> gradle -q task1
I'm task number 1
6.7 處理當前任務
4.times { counter ->
task "task$counter" << {
println "I'm task number $counter"
}
}
task0.dependsOn task2, task3
----------------------------
Output of gradle -q task0
> gradle -q task0
I'm task number 2
I'm task number 3
I'm task number 0
task hello << {
println 'Hello Earth'
}
hello.doFirst {
println 'Hello Venus'
}
hello.doLast {
println 'Hello Mars'
}
hello << {
println 'Hello Jupiter'
}
do.first和do.last可被執行多次,執行順序按first..last
<< 操作符相當於 doLast的別名
6.8獲取任務名稱
可以用$task.name來獲取任務名稱
task hello << {
println 'Hello world!'
}
hello.doLast {
println "Greetings from the $hello.name task."
}
6.9
擴充套件引數
task myTask {
ext.myProperty = "myValue"
}
task printTaskProperties << {
println myTask.myProperty
}
Output of gradle -q printTaskProperties
> gradle -q printTaskProperties
myValue
6.12
預設任務
defaultTasks 'clean', 'run'
task clean << {
println 'Default Cleaning!'
}
task run << {
println 'Default Running!'
}
task other << {
println "I'm not a default task!"
}
Output of gradle -q
> gradle -q
Default Cleaning!
Default Running!
-------------------------
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'
}
}
7:JAVA quickStart
7.2 使用JAVA plugin
apply plugin :'java'
可參考示例
samples/java/quickstart
該外掛將會依據如下目錄結構來檢索你的專案
src/main/java
src/main/resources
src/test/java
src/test/resources
build/libs
build:你可以執行其中的build任務來對你的原始碼進行編譯,單元測試以及打包
gradle build
clean:執行清除任務,刪除build目錄,移除所有構建檔案
assemble:編譯並且打jar包,但不會執行單元測試,很多其它外掛都可以增強該任務功能,比如如果添加了war plugin,該任將會打出war包
check:編譯並且測試程式碼,如果添加了Code-quality外掛,該任務同時會對程式碼風格進行檢查
7.2.2額外依賴
可以使用MAVEN中央倉庫來獲取依賴jar包
---定義倉庫
repositories {
mavenCentral()
}
---增加依賴
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
7.2.3 自定義專案
7.2.4釋出jar包
uploadArchives{
repositories{
flatDir{
dirs 'repos'
}
}
}
執行gradle uploadArchives便可將jar包釋出到respos資料夾下
7.3建立eclipse專案
apply plugin: 'eclipse'
apply plugin: 'java'
apply plugin: 'eclipse'
//自定義屬性
sourceCompatibility = 1.5
version = '1.0'
jar {
manifest {
attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
}
}
//中央倉庫
repositories {
mavenCentral()
}
//依賴JAR包
dependencies {
compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
testCompile group: 'junit', name: 'junit', version: '4.+'
}
//給test任務傳遞引數
test {
systemProperties 'property': 'value'
}
//Jar包位置
uploadArchives {
repositories {
flatDir {
dirs 'repos'
}
}
}
7.3.1多專案管理
建立多專案指令碼必須建立setting.gradle配置檔案如下
include "shared", "api", "services:webservice", "services:shared"
7.3.2公共配置
8.基礎依賴管理
8.3配置依賴
8.4外部依賴
定義
dependencies {
compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
}
簡要寫法"group:name:version".
dependencies {
compile 'org.hibernate:hibernate-core:3.6.7.Final'
}
8.5gralde如何為外部依賴下載jar包?只需要定義一個倉庫即可
至少需要定義一個倉庫,也可以直接使用maven中央倉庫或本地maven倉庫
如下
使用中央倉庫:
repositories {
mavenCentral()
}
使用自定義倉庫
repositories {
maven {
url "http://repo.mycompany.com/maven2"
}
}
使用遠端ivy倉庫
repositories {
ivy {
url "http://repo.mycompany.com/repo"
}
}
使用本地ivy倉庫
repositories {
ivy {
// URL can refer to a local directory
url "../local-repo"
}
}
一個專案可以包含多個倉庫,Gradle將會在每個倉庫中搜尋所需的依賴,當搜尋到所需依賴時停止搜尋
8.6手工釋出
依賴於各種好用的外掛,通常無需對釋出操作進行特殊處理;但需要通知gradle進行釋出的位置
可以用uploadArchives task來完成此項工作;以下是一個釋出到遠端IVY倉庫的示例
uploadArchives {
repositories {
ivy {
credentials {
username "username"
password "pw"
}
url "http://repo.mycompany.com"
}
}
}
現在,執行gradle uploadArchives,gradle將會構建並上傳生成的jar包,同時生成並上傳ivy.xml檔案
---釋出到maven倉庫
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
repository(url: "file://localhost/tmp/myRepo/")
}
}
}
10.WEB application quickStart
本章介紹如何讓gradle來為web應用提供支援,gradle為web應用開發提供了兩個外掛War plugin和jetty plugin
war plugin繼承了java plugin為專案構建war包
jetty plugin繼承了war plugin可以將你的web應用部署到jetty 容器中
當執行build命令時,gradle會尋找src/main/webapp下的原始檔,並獲取編譯後的class和他們執行時的依賴打入war包中
10.2部署到容器中執行
使用外掛apply plugin : 'jetty'
應用該外掛時同時會應用war plugin,執行gradle jettyRun將會把你的專案部署到容器中
執行gradle jettyRunWar將會構建war包並部署到容器中
url,埠號等可以通過編輯指令碼檔案來進行載入
11.1使用gradle命令列
你可以使用命令列一次執行多個任務,如
gradle compile test將分別執行compile和test任務,gradle會按命令中的順序來依次執行每個任務,並且也會執行每個任務中的依賴,每個任務只會被執行一次;
如下示例
task compile << {
println 'compiling source'
}
task compileTest(dependsOn: compile) << {
println 'compiling unit tests'
}
task test(dependsOn: [compile, compileTest]) << {
println 'running unit tests'
}
task dist(dependsOn: [compile, test]) << {
println 'building the distribution'
}
dist和test都依賴於complie,但compile只會被執行一次 執行結果如下
> gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 1 secs
11.2排除任務
你可以使用 -x引數來排除不需要執行的任務,讓我們用上一個示例中的指令碼來體驗一下
> gradle dist -x test
:compile
compiling source
:dist
building the distribution
BUILD SUCCESSFUL
Total time: 1 secs
可以看到,test任務並沒有被執行,即使它被dist任務所依賴,同時test任務所依賴的compileTest也同樣沒有被執行,
而像complie所依賴的除了test之外的任務仍然正常執行
11.3持續構建-即使發生錯誤
預設情況下,Gradle在執行時如果發生錯誤會立即中止執行任務,這能使構建更快完成但無法檢視更多其它的失敗資訊,
此時可以使用
-continue
引數,在一個構建中儘可能捕獲更多的失敗資訊;
當增加此引數是,gradle會在其所依賴的任務成功執行的前提下獨立的執行每個任務;例如,如果你使用了
'java'和'checkStyle'外掛,即使你的程式碼規範檢查和單元測試失敗,gradle仍然可以執行checkStyle,執行單元測試並且構建javadoc;所有的錯誤都會在
P.S:只有其所依賴的任務成功執行之後任務才會被執行,所以如果當java程式碼編譯失敗時,單元測試任務即不會被執行,因為
test任務直接依賴於compileJava任務
11.4任務名稱簡寫
在執行任務時,只要鍵入的字元足以唯一區分出一個任務即可省去其它字元.如11.1示例
執行dist任務 輸入gradle di即可
駝峰命名式簡寫.如11.1示例
要執行compileTest任務只需輸入 gradle cT即可 當然輸入 gradle cmopTest同樣可以執行構建
11.5執行gradle構建時,會預設執行當前目錄下的構建檔案;也可以用"-b"引數來指定其它構建檔案;
如下示例
subdir/myproject.gradle
task hello << {
println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
}
Output of gradle -q -b subdir/myproject.gradle hello
> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.
你也可以使用-q引數,僅僅指明project路徑即可,在"多專案構建"中需要用-p引數來替代-b引數