Gradle執行構建


Gradle提供了一個命令列來執行構建指令碼。 它可以一次執行多個任務。在這裡將介紹如何使用不同的選項來執行多個任務。

執行多個任務

Gradle可以從單個構建檔案執行多個任務。使用gradle命令處理構建檔案。此命令將按列出的順序編譯每個任務,並使用不同的選項執行每個任務以及依賴關係。

範例 - 假設有四個任務 - task1task2task3task4task3task4取決於task1task2。 看看下面的圖表。

在上面的四個任務是相互依賴的,用一個箭頭符號表示。 看看下面的程式碼。 將其複製並貼上到build.gradle檔案中。

task task1 << {
   println 'compiling source #1'
}

task task2(dependsOn: task1) << {
   println 'compiling unit tests #2'
}

task task3(dependsOn: [task1, task2]) << {
   println 'running unit tests #3'
}

task task4(dependsOn: [task1, task3]) << {
   println 'building the distribution #4'
}

使用以下程式碼來編譯和執行上述任務。如果命令執行成功,將獲得以下輸出 -

D:/worksp/tw511.com/gradle-3.1/study/script>gradle task4 task1
:task1
compiling source #1
:task2
compiling unit tests #2
:task3
running unit tests #3
:task4
building the distribution #4

BUILD SUCCESSFUL

Total time: 1.265 secs

排除任務

要執行中排除某個任務時,可以在gradle命令中使用-x選項,並指出要排除的任務的名稱。
使用以下命令用於從上面的指令碼中排除 task1 這個任務。
使用以下程式碼來編譯和執行上述任務。如果命令執行成功,將獲得以下輸出 -

D:/worksp/tw511.com/gradle-3.1/study/script>gradle task4 -x task1
:task2
compiling unit tests #2
:task3
running unit tests #3
:task4
building the distribution #4

BUILD SUCCESSFUL

Total time: 1.272 secs

發生故障時繼續構建

Gradle將在任何任務失敗時立即中止執行。但是有時我們希望即使發生故障,也可以繼續執行。 為此,要在gradle命令使用-continue選項。它分別處理每個任務及其依賴關係。 重要的是,它將捕獲每個遇到的故障,並在構建的執行結束時生成報告。 假設一個任務失敗,那麼相關的後續依懶任務也不會被執行。

選擇執行哪些構建

當執行gradle命令時,它在當前目錄中查詢構建檔案。我們也可以使用-b選項選擇指定的構建檔案的路徑。以下範例顯示在subdir/ 子目錄中建立一個新檔案 newbuild.gradle,並建立一個名稱為 hello 專案。建立的newbuild.gradle檔案的程式碼內容如下 -

task hello << {
   println "Use File:$buildFile.name in '$buildFile.parentFile.name'."
}

使用以下程式碼來編譯和執行上述任務。如果命令執行成功,將獲得以下輸出 -

D:/worksp/tw511.com/gradle-3.1/study/script> gradle -q -b subdir/newbuild.gradle hello
Use File:newbuild.gradle in 'subdir'.

獲取構建資訊

Gradle提供了幾個內建任務來檢索有關任務和專案的詳細資訊。這對理解構建的結構和依賴性以及偵錯一些問題很有用。可使用專案報告外掛向專案中新增任務,來生成這些報告。

列出專案

可以使用gradle -q projects命令來列出所選專案及其子專案的專案層次結構。下面是一個列出構建檔案中的所有專案的範例 -

D:/worksp/tw511.com/gradle-3.1/study/script>gradle -q projects

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

Root project 'script'
No sub-projects

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

報告顯示每個專案的描述(如果有指定的話)。可以使用以下命令指定描述。將其貼上到build.gradle檔案中。
再一次執行命令,得到以下結果 -

D:/worksp/tw511.com/gradle-3.1/study/script>gradle -q projects

------------------------------------------------------------
Root project - The shared API for the application
------------------------------------------------------------

Root project 'script' - The shared API for the application
No sub-projects

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

列出任務

使用以下命令列出屬於多個專案的所有任務。如下所示 -

D:/worksp/tw511.com/gradle-3.1/study/script>gradle -q tasks --all

------------------------------------------------------------
All tasks runnable from root project - The shared API for the application
------------------------------------------------------------

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

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'script'.
components - Displays the components produced by root project 'script'. [incubating]
dependencies - Displays all dependencies declared in root project 'script'.
dependencyInsight - Displays the insight into a specific dependency in root project 'script'.
help - Displays a help message.
model - Displays the configuration model of root project 'script'. [incubating]
projects - Displays the sub-projects of root project 'script'.
properties - Displays the properties of root project 'script'.
tasks - Displays the tasks runnable from root project 'script'.

Other tasks
-----------
task4
    task1
    task2
    task3

以下是其它一些命令及其說明的列表。

編號 命令 描述
1 gradle –q help –task 提供有關指定任務或多個任務的使用資訊(如路徑,型別,描述,組)。
2 gradle –q dependencies 提供所選專案的依賴關係的列表。
3 gradle -q api:dependencies —configuration 提供有關組態的有限依賴項的列表。
4 gradle –q buildEnvironment 提供構建指令碼依賴項的列表
5 gradle –q dependencyInsight 提供了一個洞察到一個特定的依賴
6 gradle –q properties 提供所選專案的屬性列表