Gradle-running-a-build

提供:Dev Guides
移動先:案内検索

Gradle-ビルドの実行

Gradleは、ビルドスクリプトを実行するコマンドラインを提供します。 一度に複数のタスクを実行できます。 この章では、異なるオプションを使用して複数のタスクを実行する方法について説明します。

複数のタスクを実行する

1つのビルドファイルから複数のタスクを実行できます。 Gradleは、* gradleコマンド*を使用してそのビルドファイルを処理できます。 このコマンドは、リストされている順に各タスクをコンパイルし、異なるオプションを使用して依存関係とともに各タスクを実行します。

-4つのタスク-task1、task2、task3、およびtask4があります。 Task3とtask4は、タスク1とタスク2に依存しています。 次の図をご覧ください。

複数のタスクの実行

上記の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

タスクを除外する

実行からタスクを除外するときに、gradleコマンドと一緒に-xオプションを使用し、除外するタスクの名前を指定できます。

上記のスクリプトからtask4を除外するには、次のコマンドを使用します。

C:\> gradle task4 -x test

出力:

:task1
compiling source
:task4
building the distribution

BUILD SUCCESSFUL

Total time: 1 secs

障害が発生したときにビルドを継続する

Gradleは、タスクが失敗するとすぐに実行を中止し、ビルドに失敗します。 障害が発生した場合でも実行を継続できます。 このためには、gradleコマンドで–continueオプションを使用する必要があります。 各タスクを依存関係とともに個別に処理します。 そして、主な重要なポイントは、発生した各障害をキャッチし、ビルドの実行の終了時に報告することです。 タスクが失敗した場合、依存する後続のタスクも実行されないとします。

実行するビルドの選択

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には、タスクとプロジェクトに関する情報の詳細を取得するためのいくつかの組み込みタスクが用意されています。 これは、ビルドの構造と依存関係を理解し​​、問題をデバッグするのに役立ちます。 プロジェクトレポートプラグインを使用して、プロジェクトにタスクを追加すると、これらのレポートが生成されます。

プロジェクトのリスト

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

以下に、さまざまなオプションを説明するコマンドのリストを表に示します。

Sr. No. Command Description
1 gradle –q help –task <task name> Provides the usage information (such as path, type, description, group) about a specific task or multiple tasks.
2 gradle –q dependencies Provides a list of dependencies of the selected project.
3 gradle -q api:dependencies --configuration <task name> Provides the list of limited dependencies respective to configuration.
4 gradle –q buildEnvironment Provides the list of build script dependencies.
5 gradle –q dependencyInsight Provides an insight into a particular dependency.
6 Gradle –q properties Provides the list of properties of the selected project.