Gradle - 執行構建



Gradle 提供命令列來執行構建指令碼。它可以一次執行多個任務。本章解釋如何使用不同的選項執行多個任務。

執行多個任務

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

示例

有四個任務:task1、task2、task3task4。Task3 和 task4 依賴於 task1 和 task2。請檢視下圖。

Executing Multiple Tasks

上圖中,4 個任務彼此依賴,用箭頭符號表示。請檢視以下程式碼。您可以將其複製並貼上到build.gradle 檔案中。

task task1 << {
   println 'compiling source'
}

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

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

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

您可以使用以下程式碼編譯和執行上述任務。

C:\> gradle task4 test

輸出

輸出如下所示:

:task1
compiling source
:task2
compiling unit tests
:task3
running unit tests
:task4
building the distribution

BUILD SUCCESSFUL

Total time: 1 secs

排除任務

在排除執行中的任務時,您可以將 –x 選項與 Gradle 命令一起使用,並提及要排除的任務的名稱。

使用以下命令從上述指令碼中排除 task4。

C:\> gradle task4 -x test

輸出

以下是程式碼的輸出:

:task1
compiling source
:task4
building the distribution

BUILD SUCCESSFUL

Total time: 1 secs

繼續構建

只要任何任務失敗,Gradle 就會中止執行並使構建失敗。即使發生故障,您也可以繼續執行。為此,您必須將 –continue 選項與 gradle 命令一起使用。它分別處理每個任務及其依賴項。

要點是,它將捕獲遇到的每個錯誤,並在構建執行結束時報告。假設如果一個任務失敗,則後續的依賴任務也不會執行。

選擇要執行的構建

執行 gradle 命令時,它會在當前目錄中查詢構建檔案。您可以使用 –b 選項選擇特定的構建檔案以及絕對路徑。

以下示例從位於subdir/中的myproject.gradle 檔案中選擇專案 hello。

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

您可以使用以下命令執行上述指令碼。

C:\> gradle -q -b subdir/myproject.gradle hello

輸出

這將產生以下輸出:

using build file 'myproject.gradle' in 'subdir'.

獲取構建資訊

Gradle 提供多個內建任務來檢索有關任務和專案的詳細資訊。這對於理解構建的結構、依賴項以及除錯問題非常有用。

您可以使用 project report 外掛向您的專案新增任務,這些任務將生成這些報告。

列出專案

您可以使用gradle –q projects 命令列出所選專案及其子專案的專案層次結構。使用以下命令列出構建檔案中的所有專案。這是一個示例:

C:\> 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

如果指定了專案說明,則報告會顯示每個專案的說明。您可以使用以下命令指定說明。將其貼上到build.gradle 檔案中。

description = 'The shared API for the application'

列出任務

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

C:\> 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. [incubating]
wrapper - Generates Gradle wrapper files. [incubating]

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'.
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>

您可以使用以下命令顯示所有任務的資訊。

C:\> 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, webapp:libs]
   docs - Builds the documentation
api:libs - Builds the JAR
   api:compile - Compiles the source files
webapp:libs - Builds the JAR [api:libs]
   webapp:compile - Compiles the source files

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 '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'.
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'.

以下是命令列表及其說明。

序號 命令 說明
1 gradle –q help –task <task name> 提供有關特定任務或多個任務的用法資訊(例如路徑、型別、說明、組)。
2 gradle –q dependencies 提供所選專案的依賴項列表。
3 gradle -q api:dependencies --configuration <task name> 提供關於配置的有限依賴項列表。
4 gradle –q buildEnvironment 提供構建指令碼依賴項的列表。
5 gradle –q dependencyInsight 提供對特定依賴項的深入瞭解。
6 Gradle –q properties 提供所選專案的屬性列表。
廣告
© . All rights reserved.