Skip to content

gradle/dpeuni-gradle-intro-devs-tasks

Repository files navigation

DPE University Training

Gradle Tasks Exercise

This is a hands-on exercise to go along with the Introduction to Gradle Build Tool for Developers training module. In this exercise you will go over the following:

  • See available tasks
  • Run an application
  • Run test task and inspect report
  • Add and use external plugin

Prerequisites

  • Completed the first hands-on exercise
  • You can perform the exercises in the same Gradle project used in exercise 1

Add gradle.properties File

  1. Create a file called gradle.properties in the top level directory

  2. In it put the line: org.gradle.console=verbose

  3. Save the file


See Available Tasks in Editor

This section is specific to using IntelliJ editor. If you don't use IntelliJ you can skip this section.

  1. Open the Gradle toolbar on right side of the IDE and explore the tasks. Notice there are tasks under the top level Tasks as well as under app. Usually you will want to run the tasks under the specific Gradle project, in this case app. Running tasks under the top level Tasks will run the task for all projects, if there were more than one.

  1. Close the Gradle toolbar.

See Available Tasks in Command-line

  1. Open a terminal in the project folder. In IntelliJ you can open the Terminal tab in the lower toolbar.

  2. Run ./gradlew :app:tasks --all. Scroll up and explore the tasks.


Run an Application

  1. In the tasks list, notice the run task under the Application tasks group.

  1. Inspect the file app/src/main/java/com/gradle/lab/App.java. Notice the main method is supposed to print a greeting message.

  1. Now look at the application block in build.gradle.kts, notice that the mainClass is set as the file just viewed.

  1. In the terminal, execute the ./gradlew :app:run command. Notice the greeting message is printed.


Run Test Task and Inspect Report

  1. Open the file app/src/test/java/com/gradle/lab/AppTest.java in the IDE. Notice there is one test defined.

  1. Open the Gradle toolbar again and open app/Tasks/verification. Double-click on the test task.

  1. Notice the task run output on the bottom of the IDE.

  1. Navigate to app/build/reports/tests/test and open the index.html in a browser.


Add External Plugin

  1. Open the Gradle Plugin Portal page in a browser.

  2. Search for task tree. In the search results notice the information on the right side which indicates when the plugin was last updated. Click on the org.barfuin.gradle.taskinfo plugin.

  1. Copy the id line and paste it into the plugin section of build.gradle.kts. Notice a popup that appears when you make a change to the Gradle file. Click on the second icon to have the IDE process the changes to the Gradle config.

NOTE: The version you will see may differ than in the screenshot below.

  1. If using IntelliJ, open the Gradle toolbar and expand Tasks/other. Notice the 3 tasks tiJson, tiOrder and tiTree. These have been added by the plugin. Do not run the tasks yet.

  1. Go back to the plugin page in the browser. Click on the repository link near the top of the page.

  1. In the README, notice the author describes how to invoke the tasks. An argument has to be passed, which is the task whose dependencies you want to inspect. Therefore it is easier to run the task in the terminal where you can easily pass arguments.

  1. Open the terminal. Run ./gradlew :app:tiTree :app:test to see the dependencies for the test task.


Solution Reference

If you get stuck you can refer to the project files in this repository.

About

Hands-on exercise for DPE University

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages