いいかげんちゃんと理解したいGradle(第二章)

# はじめに
前回は、ビルドの概念や、Gradleの初期設定でできるディレクトリ構成から、初期設定について学びました。

## Running Gradle Tasks

### タスクの基本概念
**タスク**とはビルドプロセスにおける個々の作業単位であり、コンパイルやテスト、デプロイなどの操作を定義します。
Gradleでは、タスクはTaskオブジェクトとして表現され、ビルドスクリプト内で定義・管理されます。


```kotlin
/*
 * This file was generated by the Gradle 'init' task.
 *
 * This generated file contains a sample Kotlin application project to get you started.
 * For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10.2/userguide/building_java_projects.html in the Gradle documentation.
 */

plugins {
    // Apply the org.jetbrains.kotlin.jvm Plugin to add support for Kotlin.
    alias(libs.plugins.kotlin.jvm)

    // Apply the application plugin to add support for building a CLI application in Java.
    application
}

repositories {
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    // Use JUnit Jupiter for testing.
    testImplementation(libs.junit.jupiter)

    testRuntimeOnly("org.junit.platform:junit-platform-launcher")

    // This dependency is used by the application.
    implementation(libs.guava)
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(17)
    }
}

application {
    // Define the main class for the application.
    mainClass = "org.example.AppKt"
}

tasks.named<Test>("test") {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

```
### タスクの確認方法
タスクを確認するには、gradleコマンドにtasksを指定します。
```shell
$ ./gradlew tasks



> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'tutorial'
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildKotlinToolingMetadata - Build metadata json file containing information about the used Kotlin tooling
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the classes of the 'main' feature.
kotlinSourcesJar - Assembles a jar archive containing the sources of target 'kotlin'.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
updateDaemonJvm - Generates or updates the Gradle Daemon JVM criteria.
wrapper - Generates Gradle wrapper files.

Distribution tasks
------------------
assembleDist - Assembles the main distributions
distTar - Bundles the project as a distribution.
distZip - Bundles the project as a distribution.
installDist - Installs the project as a distribution as-is.

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the 'main' feature.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'tutorial'.
dependencies - Displays all dependencies declared in root project 'tutorial'.
dependencyInsight - Displays the insight into a specific dependency in root project 'tutorial'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
outgoingVariants - Displays the outgoing variants of root project 'tutorial'.
projects - Displays the sub-projects of root project 'tutorial'.
properties - Displays the properties of root project 'tutorial'.
resolvableConfigurations - Displays the configurations that can be resolved in root project 'tutorial'.
tasks - Displays the tasks runnable from root project 'tutorial' (some of the displayed tasks may belong to subprojects).

Verification tasks
------------------
check - Runs all checks.
checkKotlinGradlePluginConfigurationErrors - Checks that Kotlin Gradle Plugin hasn't reported project configuration errors, failing otherwise. This task always runs before compileKotlin* or similar tasks.
test - Runs the test suite.

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

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

BUILD SUCCESSFUL in 485ms
1 actionable task: 1 executed

```
application プラグインのtaskなども含まれていてたくさんのタスクが表示されます。
何も定義していないと
```shell

> Task :tasks

------------------------------------------------------------
Tasks runnable from root project 'tutorial'
------------------------------------------------------------

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
updateDaemonJvm - Generates or updates the Gradle Daemon JVM criteria.
wrapper - Generates Gradle wrapper files.

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'tutorial'.
dependencies - Displays all dependencies declared in root project 'tutorial'.
dependencyInsight - Displays the insight into a specific dependency in root project 'tutorial'.
help - Displays a help message.
javaToolchains - Displays the detected java toolchains.
kotlinDslAccessorsReport - Prints the Kotlin code for accessing the currently available project extensions and conventions.
outgoingVariants - Displays the outgoing variants of root project 'tutorial'.
projects - Displays the sub-projects of root project 'tutorial'.
properties - Displays the properties of root project 'tutorial'.
resolvableConfigurations - Displays the configurations that can be resolved in root project 'tutorial'.
tasks - Displays the tasks runnable from root project 'tutorial' (some of the displayed tasks may belong to subprojects).

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

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

BUILD SUCCESSFUL in 360ms
1 actionable task: 1 executed

```
と表示されます。
便利なプラグインを入れるだけででタスクを追加することができます。

### タスクの作成方法
タスクは、build.gradleファイル内でtaskメソッドを使って定義します。
```kotlin
tasks.register<Copy>("copyTask") {
    from("source")
    into("target")
    include("*.war")
}
```

### タスクの依存関係の設定
```kotlin
task taskA {
    // タスクAの定義
}

task taskB {
    dependsOn taskA
    // タスクBの定義
}
```

### タスクの種類
Gradleには、様々なタスクが用意されています。
- **Copyタスク**：ファイルやディレクトリをコピーする
- **Deleteタスス**：ファイルやディレクトリを削除する
- **Zipタスク**：ファイルやディレクトリをZIPアーカイブにする
- **JavaCompileタスク**：Javaソースコードをコンパイルする
- **Testタスク**：テストを実行する

### タスクの実行方法
タスクを実行するには、gradleコマンドにタスク名を指定します。
```shell
$ ./gradlew hello
```
複数のタスクを連続して実行する場合は、タスク名をスペースで区切って指定します。
```shell
$ ./gradlew taskA taskB
```

# まとめ
タスクというのは1つ1つの作業単位である。
プラグインを入れるだけでタスクを追加できたり、自分でもgradleが用意しているタスクを利用することで定義することができる。