5 minutes read

Hopefully, you've already got a basic understanding of what Gradle is and how to use it. This topic will look at how to build and run a small application using this build tool. The knowledge you obtain here can be used for any JVM-based programming language supported by Gradle (for example, Java or Kotlin).

This article was written using Gradle 9.0.0. There may be some differences between other versions of Gradle. If you have trouble with this article, you can read the comments or follow the official Gradle doc instead of this.

Initializing an application

We assume that you already have some experience with the terminal of your operating system and will interact with Gradle using it. First of all, create a new empty folder named as you want (e.g., demo). In this folder, invoke the gradle init command to start initializing a new Gradle-based project. This command will show you a dialog form to set up the project you need.

In this form, choose application as the type of the project and Java or Kotlin as the implementation language.

Select type of build to generate:
  1: Application
  2: Library
  3: Gradle plugin
  4: Basic (build structure only)
Enter selection (default: Application) [1..4] 1

Select implementation language:
  1: Java
  2: Kotlin
  3: Groovy
  4: Scala
  5: C++
  6: Swift
Enter selection (default: Java) [1..6] 1

Enter target Java version (min: 7, default: 21): 21

Project name (default: demo): demo

Select application structure:
  1: Single application project
  2: Application and library project
Enter selection (default: Single application project) [1..2] 1

Select build script DSL:
  1: Kotlin
  2: Groovy
Enter selection (default: Kotlin) [1..2] 1

Select test framework:
  1: JUnit 4
  2: TestNG
  3: Spock
  4: JUnit Jupiter
Enter selection (default: JUnit Jupiter) [1..4] 4

Generate build using new APIs and behavior (some features may change in the next minor release)? (default: no) [yes, no] no

After completing the initialization, the project structure will be the following:

.
├── app
│   ├── build.gradle.kts
│   └── src
│       ├── main
│       │   ├── java
│       │   │   └── org
│       │   │       └── example
│       │   │           └── App.java
│       │   └── resources
│       └── test
│           ├── java
│           │   └── org
│           │       └── example
│           │           └── AppTest.java
│           └── resources
├── gradle
│   ├── wrapper
│   │   ├── gradle-wrapper.jar
│   │   └── gradle-wrapper.properties
│   └── libs.version.toml
├── gradlew
├── gradlew.bat
├── gradle.properties
└── settings.gradle.kts

This structure includes a lot of files you have already considered (settings.gradle.kts, wrapper files, and others). There is a folder app that exists because you've chosen application as the type of project, and the folder represents our application. You will take a closer look at the file build.gradle.kts in the next topic.

There is also the src directory inside app. It contains two sub-directories main and test. This is quite a standard project structure when using Gradle. In our case, the package org.example has some Java source code (App.java). If you chose Kotlin as the implementation language, the project structure would be the same except for Kotlin source code files (.kt instead of .java) and kotlin folders instead of java ones.

Please note it is a good practice for Java and Kotlin projects to include the name of your organization in the path to your source code files as a package name like org.hyperskill.

Running the application

If you look at the list of available tasks for managing the project using the command gradle tasks --all, you will see that the list is fairly long. Here is a shortened version of it:

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

Build tasks
-----------
app:assemble - Assembles the outputs of this project.
app:build - Assembles and tests this project.
...

To start the application, you can use the run command of Gradle. To do it, invoke the gradle run command, or you can use a Gradle wrapper script for your OS. This command will build and run the application. Here is an output example:

> Task :app:run
Hello World!

BUILD SUCCESSFUL in 3s
2 actionable tasks: 2 executed

As you can see, the autogenerated application can already display a welcome string. If you get a similar result, it means that everything is OK: your application works, and Gradle can manage it!

If you look at the project structure again, you will see that it has some new files, including files with bytecode (App.class, AppTest.class) in app/build/. Actually, Gradle built and started the App.class file when we invoked the run command.

Building the application

If you would like to generate a bundle of the application with all its dependencies and a script to start the application, use the gradle build command.

BUILD SUCCESSFUL in 22s
7 actionable tasks: 7 executed

If everything is OK, Gradle will have produced the archive in two formats for you: app/build/distributions/app.jar and app/build/distributions/app.zip. Now, you can distribute your application!

Conclusion

In this topic, you learned how to create applications using Gradle. You can write these applications in either Java or Kotlin. You also learned how to run these applications using gradle run command. Additionally, you got to know the basic structure of the project folder generated by Gradle. Further, you will learn more about configuring files in that folder.

643 learners liked this piece of theory. 129 didn't like it. What about you?
Report a typo