Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Using Gradle

  1. The tasks/task1_4 subdirectory in your repository contains another version of the application seen previously, organized as a Gradle project.

    Take a few minutes to explore the files in task1_4 and its various subdirectories.

  2. Open the file build.gradle.kts in your editor and examine it. This is the Gradle build script1 for the application. Notice that it specifies some dependencies, specifically for the unit tests that accompany the application.

    To see the tasks that can be performed by the build script, go to a terminal window, move into the task1_4 subdirectory and enter

    ./gradlew tasks
    

    This command runs the Gradle wrapper. It will work as above on Linux or macOS. If you see a ‘Permission denied’ error on these systems, you can fix this with

    chmod u+x gradlew
    

    Note: If you are using Windows and your command prompt is provided by cmd.exe, you’ll need to omit the leading ./ from the command to run it. If your command prompt is provided by Windows Powershell then you’ll need to use .\gradlew to run it.

    Warning

    The Gradle wrapper will be VERY slow the first time it runs on your PC, or the first time it runs in a newly-created Codespace!

    This is because it will need to download the code for Gradle itself, the Kotlin compiler and any application dependencies.

    Subsequent tasks will run much faster.

  3. Search under the src subdirectory of task1_4 for the tests and the application source code. Examine these source files in your editor.

    Note

    You don’t need to understand how the tests work right now. We will go through all of this in detail later in the module.

  4. Enter the following command to run the tests:

    ./gradlew test
    

    Gradle should report the status of each test, showing that they all pass. It should also report ‘BUILD SUCCESSFUL’ as the overall result.

    Notice that, although a task to compile the code exists, we didn’t have to specify it. Gradle recognizes that the test task depends upon this task, and it will run the compilation task first if needed.

  5. Try running the tests a second time. You should see Gradle jump straight to the ‘BUILD SUCCESSFUL’ message. It recognizes that there is no need to rerun the tests, because no code has changed.

    You can force Gradle to recompile everything and rerun the tests with

    ./gradlew --rerun-tasks test
    
  6. Now try running the application, with

    ./gradlew run
    

    It should behave exactly as before2.

  7. Finally, try packaging the application for distribution, using

    ./gradlew distZip
    

    This will create a file named task1_4.zip, in the build/distributions subdirectory of task1_4. This Zip archive contains JAR files for the application and the Kotlin standard library, plus a shell script and batch file that can be used to run the application on Linux, Mac or Windows systems.

    If you like, copy this Zip archive to somewhere else on your system, unzip it, then try running the application using the shell script (on Linux or macOS) or batch file (on Windows).

  8. When you’re done, you can remove all of the build artifacts for the project with

    ./gradlew clean
    

  1. Note that this build script is itself written in Kotlin!

  2. By default, the run task runs an application without command line arguments. You can supply arguments as a quoted string, like this: ./gradlew run --args='arg1 arg2'. It is also possible to customize the run task in the build script so that it provides a default set of arguments to the application.