Part3-學習使用gradle命令行

這一章節主要介紹gradle命令行的基礎,我們使用gradle開頭的命令來運行一個構建任務

我們可以一次性運行多個gradle任務,使用空格做為每個任務之間的分隔符,比如:命令 gradle compile運行任務compile,compile test運行任務compile和任務test,其中compile和test按順序執行(執行順序是:compile的依賴任務-compile-test的依賴任務-test)。

注意:每個任務僅僅會被執行一次,下面舉個??說明一下:

定義了四個任務,dist、test、compileTest、compile


commandLineTutorialTasks.png

依賴關系如箭頭所示。dist依賴test和compile任務,test依賴compileTest和compile任務,compileTest又依賴compile任務。

task compile {
    doLast {
        println 'compiling source'
    }
}

task compileTest(dependsOn: compile) {
    doLast {
        println 'compiling unit tests'
    }
}

task test(dependsOn: [compile, compileTest]) {
    doLast {
        println 'running unit tests'
    }
}

task dist(dependsOn: [compile, test]) {
    doLast {
        println 'building the distribution'
    }
}

執行gradle dist test后的輸出:

> gradle dist test
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed

執行順序是compile compileTest test dist
每個任務僅僅執行了一次,所以gradle test test和gradle的效果是一樣的

假如不需要執行test任務,則可以使用-x選項進行排除
執行gradle dist -x test后的輸出

> gradle dist -x test
:compile
compiling source
:dist
building the distribution

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

可以看出test任務沒有被執行,test的依賴任務compileTest也沒有執行。

默認情況下,gradle遇到執行出錯的任務會停止執行,這樣構建任務可以快速的停止,并且拋出異常。但是這樣會導致未被執行到的任務的錯誤不能被發現。為了盡可能發現更多的錯誤,可以使用 --continue 選項。

當使用 --continue 選項的時候,gradle執行時遇到錯誤會繼續執行下一個任務,并且在執行完成時在命令行末尾拋出所有錯誤。

如果一個任務執行失敗了,那么依賴(直接或間接)這個任務的其他任務將不會被執行。

task任務的名稱支持縮略形式,我們只需要提供足夠的特征字符就可以。比如上邊的例子,我們可以使用 gradle d 命令執行dist任務

執行gradle di命令

> gradle di
:compile
compiling source
:compileTest
compiling unit tests
:test
running unit tests
:dist
building the distribution

BUILD SUCCESSFUL in 0s
4 actionable tasks: 4 executed

也可以依照駝峰命名規則來書寫縮略名稱,gradle compileTest =
gradle compTest = gradle cT

gradle cT的輸出

> gradle cT
:compile
compiling source
:compileTest
compiling unit tests

BUILD SUCCESSFUL in 0s
2 actionable tasks: 2 executed

使用 -x(排除) 選項時依然可以使用縮寫形式。

當我們使用gradle命令時,會在當前目錄下尋找build文件,我們可以使用-b選項選擇其他目錄下的build文件

比如說:存在文件subdir/myproject.gradle

task hello {
    doLast {
        println "using build file '$buildFile.name' in '$buildFile.parentFile.name'."
    }
}

執行gradle -q -b subdir/myproject.gradle hello

> gradle -q -b subdir/myproject.gradle hello
using build file 'myproject.gradle' in 'subdir'.

或者我們也可以使用 -p 選項來指定整個項目目錄,多項目目錄使用 -p 代替 -b 選項

gradle -q -p subdir hello

> gradle -q -p subdir hello
using build file 'build.gradle' in 'subdir'.

-q 參數的作用是什么?
該文檔的示例中很多地方在調用 gradle 命令時都加了 -q 參數。該參數用來控制 gradle 的日志級別,可以保證只輸出我們需要的內容。

日志級別:
ERROR 錯誤消息
QUIET 重要的信息消息
WARNING 警告消息
LIFECYCLE 進度信息消息
INFO 信息性消息
DEBUG 調試消息

日志參數:
沒有日志選項 LIFECYCLE 及更高
-q or –quiet QUIET 及更高
-i or –info INFO 及更高
-d or –debug DEBUG 及更高
-s or –stacktrace 打印關鍵堆棧信息
-S or –full-stacktrace 打印全棧信息

gradle內置的任務一般都是支持增量構建的,任務執行與否,取決于任務的input和output是否存在修改。如果任務不需要執行則出現 UP-TO-DATE 標識

gradle doIt

> gradle doIt
:doIt UP-TO-DATE

gradle --rerun-tasks doIt

> gradle --rerun-tasks doIt
:doIt

這個 --rerun-tasks 選項會強制執行所有任務(當前任務依賴的任務),和 clean 命令有些類似,但是不會清除構建產出。

運行 gradle projects 會以分層的形式打印出當前project的所有子project

gradle -q projects

> gradle -q projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'projectReports'
+--- Project ':api' - The shared API for the application
\--- Project ':webapp' - The Web application implementation

To see a list of the tasks of a project, run gradle <project-path>:tasks
For example, try running gradle :api:tasks

可以看到打印出了子project名字和描述信息,描述信息是可以通過description屬性進行設置的

比如在build.gradle文件中:

description = 'The shared API for the application'

運行命令 gradle tasks 會列出當前project的主要tasks和每個task的description

gradle -q tasks

> gradle -q tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Default tasks: dists

Build tasks
-----------
clean - Deletes the build directory (build)
dists - Builds the distribution
libs - Builds the JAR

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'.
components - Displays the components produced by root project 'projectReports'. [incubating]
dependencies - Displays all dependencies declared in root project 'projectReports'.
dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'projectReports'. [incubating]
projects - Displays the sub-projects of root project 'projectReports'.
properties - Displays the properties of root project 'projectReports'.
tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>

僅僅是展示了分組下的task(包括子project的task),稱作可見task,我們可以給task指定group屬性,當然和project類似,也存在description屬性。

build.gradle

dists {
    description = 'Builds the distribution'
    group = 'build'
}

使用 --all 選項,可以查看所有的task

gradle -q tasks --all

> gradle -q tasks --all

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Default tasks: dists

Build tasks
-----------
clean - Deletes the build directory (build)
api:clean - Deletes the build directory (build)
webapp:clean - Deletes the build directory (build)
dists - Builds the distribution
api:libs - Builds the JAR
webapp:libs - Builds the JAR

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'projectReports'.
api:buildEnvironment - Displays all buildscript dependencies declared in project ':api'.
webapp:buildEnvironment - Displays all buildscript dependencies declared in project ':webapp'.
components - Displays the components produced by root project 'projectReports'. [incubating]
api:components - Displays the components produced by project ':api'. [incubating]
webapp:components - Displays the components produced by project ':webapp'. [incubating]
dependencies - Displays all dependencies declared in root project 'projectReports'.
api:dependencies - Displays all dependencies declared in project ':api'.
webapp:dependencies - Displays all dependencies declared in project ':webapp'.
dependencyInsight - Displays the insight into a specific dependency in root project 'projectReports'.
api:dependencyInsight - Displays the insight into a specific dependency in project ':api'.
webapp:dependencyInsight - Displays the insight into a specific dependency in project ':webapp'.
dependentComponents - Displays the dependent components of components in root project 'projectReports'. [incubating]
api:dependentComponents - Displays the dependent components of components in project ':api'. [incubating]
webapp:dependentComponents - Displays the dependent components of components in project ':webapp'. [incubating]
help - Displays a help message.
api:help - Displays a help message.
webapp:help - Displays a help message.
model - Displays the configuration model of root project 'projectReports'. [incubating]
api:model - Displays the configuration model of project ':api'. [incubating]
webapp:model - Displays the configuration model of project ':webapp'. [incubating]
projects - Displays the sub-projects of root project 'projectReports'.
api:projects - Displays the sub-projects of project ':api'.
webapp:projects - Displays the sub-projects of project ':webapp'.
properties - Displays the properties of root project 'projectReports'.
api:properties - Displays the properties of project ':api'.
webapp:properties - Displays the properties of project ':webapp'.
tasks - Displays the tasks runnable from root project 'projectReports' (some of the displayed tasks may belong to subprojects).
api:tasks - Displays the tasks runnable from project ':api'.
webapp:tasks - Displays the tasks runnable from project ':webapp'.

Other tasks
-----------
api:compile - Compiles the source files
webapp:compile - Compiles the source files
docs - Builds the documentation
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容