Using Gradle
-
The
tasks/task1_4subdirectory 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_4and its various subdirectories. -
Open the file
build.gradle.ktsin 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_4subdirectory and enter./gradlew tasksThis 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 gradlewNote: 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.\gradlewto run it. -
Search under the
srcsubdirectory oftask1_4for the tests and the application source code. Examine these source files in your editor. -
Enter the following command to run the tests:
./gradlew testGradle 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
testtask depends upon this task, and it will run the compilation task first if needed. -
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 -
Now try running the application, with
./gradlew runIt should behave exactly as before2.
-
Finally, try packaging the application for distribution, using
./gradlew distZipThis will create a file named
task1_4.zip, in thebuild/distributionssubdirectory oftask1_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).
-
When you’re done, you can remove all of the build artifacts for the project with
./gradlew clean
-
Note that this build script is itself written in Kotlin! ↩
-
By default, the
runtask 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 theruntask in the build script so that it provides a default set of arguments to the application. ↩