1. 程式人生 > >Gradle:Gradle入門

Gradle:Gradle入門

overflow word tis ati ren put option dependson pac

一、安裝Gradle?

1.首先確保你安裝的JDK1.5或以上版本號。?

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 secs 

3.在運行任務時排除一個任務,-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

Gradle:Gradle入門