// Copyright (C) 2024 Gradle, Inc.
//
// Licensed under the Creative Commons Attribution-Noncommercial-ShareAlike 4.0 International License.;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[part2_gradle_tasks]]
= Part 2: Running Gradle Tasks

Learn the basics of Gradle tasks by running one and looking at its output.

****
**In this section you will:**

- See available tasks
- Run a task and inspect the results
- Understand tasks
- Explore task dependencies
****

[[part2_begin]]
== Step 0. Before you Begin

1. You initialized your Java app in <<part1_gradle_init.adoc#part1_begin,part 1>>.

[[rb_part2_step1]]
== Step 1. Viewing available Tasks

A _task_ is a basic unit of work that can be done by Gradle as part of the build.

A task might be responsible for compilation, copying and moving files, creating JAR files, generating Javadoc, publishing artifacts to repositories, or many other discrete units of work.

In the `tutorial` directory, enter the command below to list all the available tasks in the project:

[source,bash]
----
$ ./gradlew tasks
----

The list includes tasks contributed by the `application` plugin and the plugin it applies:

[source,text]
----
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.

...

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

...

Other tasks
-----------
compileJava - Compiles main Java source.

...
----

Tasks are the fundamental building blocks of the build process.
Each task represents a specific action that Gradle can perform, and tasks can be combined and ordered based on their dependencies.

For example, the `build` task you ran in <<part1_gradle_init.adoc#part1_begin,part 1>> of the tutorial depends on `compileJava`, `processResources`, and `test`, ensuring that code is compiled, resources are processed, and tests are run before the final build is packaged.

If you want to list the tasks available in a specific subproject, you can use `./gradlew :<subproject name>:tasks`.
To list the tasks in the `app` subproject:

[source,bash]
----
$ ./gradlew :app:tasks
----

You can view more tasks by adding the `--all` option:

[source,bash]
----
$ ./gradlew tasks --all
$ ./gradlew :app:tasks --all
----

This shows internal and lifecycle tasks that are normally hidden in the default summary.

[[rb_part2_step2]]
== Step 2. Understanding Tasks

Tasks can come from several places.
Gradle includes some tasks by default, and many others are added automatically when you apply plugins.

If you remember back in <<part1_gradle_init.adoc#part1_begin,part 1>>, the build script of our Java application applies the `application` plugin:

====
include::sample[dir="snippets/tutorials/running-builds/generated/kotlin",files="app/build.gradle.kts[tags=init-plugins]"]
include::sample[dir="snippets/tutorials/running-builds/generated/groovy",files="app/build.gradle[tags=init-plugins]"]
====

Applying the `application` plugin adds tasks like `run`, `build`, `compileJava`, `test`, and `jar`.

You can also define your own custom tasks in your build script using the `tasks.register()` method, and configure what the task does using Gradle’s task APIs.

Gradle provides several built-in task types that make it easy to perform common operations, like copying files, archiving outputs, or running commands.

For example, the `Copy` task type is used to copy files from one location to another as part of your build.

Here's how you could register a custom task that copies all `.war` files from a source directory to a target directory:

====
include::sample[dir="snippets/tutorials/running-builds/generated/kotlin",files="app/build.gradle.kts[tags=init-task]"]
include::sample[dir="snippets/tutorials/running-builds/generated/groovy",files="app/build.gradle[tags=init-task]"]
====

If this task is defined in the `build.gradle(.kts)` file for the app subproject, you can run it with:

[source,bash]
----
$ ./gradlew :app:copyTask
----

Gradle will execute the task and copy any `.war` files it finds in the source directory into the target directory.

Popular built-in tasks include:

- **Copy** - `Copy` is useful to copy files around.
- **Delete** -  `Delete` is useful to delete files and directories.
- **Exec** - `Exec` is useful to execute arbitrary O/S commands.
- **Zip** - `Zip` is useful to bundle files.

With many more documented in the Kotlin and Groovy DSLs.

[[rb_part2_step3]]
== Step 3. Understanding Dependencies Between Tasks

Often, one task needs another task to run first.
For example, a task that creates a JAR file can’t run until the code has been compiled.

If Task B needs the output of Task A, then Gradle must run Task A before it runs Task B.
This relationship is called a task dependency.

There are two kinds of task dependencies:

1. *Explicit dependencies*: One task directly declares that it depends on another.
2. *Implicit dependencies*: Gradle figures out the order automatically because one task uses the output of another (like compiled class files).

Here is an example of explicit task dependency:

====
include::sample[dir="snippets/tutorials/running-builds/generated/kotlin",files="app/build.gradle.kts[tags=init-hello]"]
include::sample[dir="snippets/tutorials/running-builds/generated/groovy",files="app/build.gradle[tags=init-hello]"]
====

In this case, `hello` prints before `greet`.
The output is `Hello! How are you?`.

Task execution order is automatically determined by Gradle, taking into account explicit and implicit task dependencies.
If there is no dependency between tasks, Gradle enables users to request a specific execution order.

[[rb_part2_step4]]
== Step 4. Viewing Tasks in the IDE (Optional)

Project tasks are also available in IntelliJ.
The project should be open following <<part1_gradle_init.adoc#part1_begin,part 1>> of the tutorial.

On the right-hand side of your window, open the `Gradle` pane to view all available tasks:

image::tutorial/intellij-idea-tasks.png[]

[[rb_part2_step5]]
== Step 5. Running Tasks in the IDE (Optional)

You can run a Gradle task via IntelliJ by double-clicking that task in the pane.

Double-click **tutorial > app > build > build**.

image::tutorial/intellij-idea-build.png[]

Once the build finishes, make sure it is successful in the IntelliJ console:

[source,text]
----
BUILD SUCCESSFUL in 966ms
7 actionable tasks: 7 executed
3:18:24 AM: Execution finished 'build'.
----

[[rb_part2_step6]]
== Step 6. Running Tasks in the Terminal

Run the following command in your terminal:

[source,bash]
----
$ ./gradlew build
----
[source,text]
----
> Task :app:compileJava
> Task :app:processResources
> Task :app:classes
> Task :app:jar
> Task :app:startScripts
> Task :app:distTar
> Task :app:distZip
> Task :app:assemble
> Task :app:compileTestJava
> Task :app:processTestResources
> Task :app:testClasses
> Task :app:test
> Task :app:check
> Task :app:build
----

The `build` task uses the source code and its dependencies to build the app.
As seen in the output, the `build` task compiles, assembles, tests, and checks the code.

The tasks are printed in order of execution.
The `jar` tasks is a dependency of the `build` task.

The `jar` task creates an executable JAR file of the app.
Let's run it by itself:

[source,bash]
----
$ ./gradlew jar
----
[source,text]
----
> Task :app:compileJava
> Task :app:processResources
> Task :app:classes
> Task :app:jar
----

As expected, the `compileJava` task is a dependency of the `jar` task and executed first.
Once the task finishes, an `app.jar` file is created in your `tutorial/app/build/libs/` folder.

Invoke the `run` task and check the output:

[source,text]
----
$ ./gradlew run
----
[source,text]
----
> Task :app:compileJava
> Task :app:processResources
> Task :app:classes

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 325ms
----

The `run` task executes the code in `tutorial/app/src/main/java/com.gradle.tutorial/App.java`.

You can take a minute and take a look at the Java code in `App.java` which simply prints "Hello World" to the screen:

====
include::sample[dir="snippets/tutorials/running-builds/generated/groovy",files="app/src/main/java/org/example/App.java[]"]
====

[.text-right]
**Next Step:** <<part3_gradle_dep_man#part3_begin,Dependency Management>> >>
