- Getting Started with Gradle
- Step 1. Create a project
- Create a new Gradle Project with IntelliJ IDEA
- Step 2. Add Java code
- Add a Java class to the Gradle project
- Create a test class
- Step 3. Run the application with Gradle
- Run main class from the editor
- Step 4. Run tests
- Run tests in a Gradle project
- Step 5. Create an executable JAR file
- Step 6. Run the JAR file with Gradle
- Run the JAR file
- Getting Started
- Before you start
- Installation
- Try Gradle
- Command line vs IDEs
- Executing Gradle builds
- Authoring Gradle builds
- Integrating 3rd-party tools with Gradle
Getting Started with Gradle
In this tutorial, we’ll create a Gradle project, will run and test it, and run the executable JAR file using Gradle.
The project used in this tutorial can be found on GitHub.
Step 1. Create a project
Let’s create a Gradle project with Java.
Create a new Gradle Project with IntelliJ IDEA
- On the welcome screen, click New Project .
- On the page that opens, let’s specify our project’s name (FizzBuzz) and the location.
- Let’s select the Java option, which is what we need for our project and Gradle since we are creating a Gradle project.
- IntelliJ IDEA automatically adds a project SDK (JDK) in the JDK field. In our tutorial we use the open JDK 14 version. You can change the selected JDK, IntelliJ IDEA will download the appropriate Gradle version. The only thing you need to have is the internet connection. Let’s leave the default Groovy for Gradle DSL and unselect the Add sample code option since we’re going to add our own code from scratch.
- We can use the default information for ArtifactId which basically is the name of our project and leave the default information in the GroupId field. Click Create .
After we’ve created our project and it finished indexing, let’s see what is inside:
- IntelliJ IDEA creates a project with the build.gradle file including the following code:
As you can see, IntelliJ IDEA conveniently adds a test dependency. IntelliJ IDEA supports code completion inside the build.gradle file. So, if we decide to add more dependencies, IntelliJ IDEA will quickly locate their names and versions.
Step 2. Add Java code
Now let’s create a Java application that outputs the first 100 FizzBuzz numbers.
Add a Java class to the Gradle project
- In the Project tool window open the src folder.
- Click the main directory then right-click the java subdirectory and from the list select New | Package .
- In the New Package dialog, let’s enter a name of our package which is com.gradle.tutorial .
- Now right-click the package and select New | Java Class .
- In the New Java Class dialog specify a name of your Java class and click OK . In our case it is FizzBuzzProcessor .
- Add the following code to the main FizzBuzzProcessor class:
Our application is ready. Now, let’s create the necessary tests for it.
Create a test class
- Open the main class FizzBuzzProcessor in the editor, place the caret at the class name and press Control+Shift+T .
- In the dialog that opens, let’s make sure that our testing library is JUnit4 (if you want use JUnit5 then make sure you have the JUnit5 ) and the destination package is com.gradle.tutorial . We add the name FizzBuzzTest and leave the rest of the default options as is and click OK .
- Now open the created test class and add the following code:
Step 3. Run the application with Gradle
Let’s quickly run the application to see if it works.
Run main class from the editor
- Open the main class FizzBuzzProcessor in the editor.
- In the gutter, click and select Run ‘FizzBuzzProcessor.main()’ .
- Check the result in the Run tool window.
Step 4. Run tests
Now, let’s run the test we’ve created.
Run tests in a Gradle project
We can run our test from the editor or from the Gradle tool window using the test task. We will use the editor.
- Click in the gutter of the editor.
The result of the test will be displayed in the Run tool window.
If we change the default number in one of the tests, it will fail.
As you can see, the Run tool window displays information obout the failed test including the specific line of the code where the error occurred.
Step 5. Create an executable JAR file
Now let’s build our application to create an executable JAR file.
- In the Project tool window, double click the build.gradle file to open it in the editor.
- Add the following code:
Step 6. Run the JAR file with Gradle
Now let’s tweak the build.gradle file a little bit more, so we can execute our JAR file in the Run anything window.
Run the JAR file
- In the Project tool window, double click the build.gradle file to open it in the editor.
- Let’s add id ‘application’ to the plugins section and the following code:
Getting Started
Everyone has to start somewhere and if you’re new to Gradle, this is where to begin.
Before you start
In order to use Gradle effectively, you need to know what it is and understand some of its fundamental concepts. So before you start using Gradle in earnest, we highly recommend you read What is Gradle?.
Installation
If all you want to do is run an existing Gradle build, then you don’t need to install Gradle if the build has a Gradle Wrapper, identifiable via the gradlew and/or gradlew.bat files in the root of the build. You just need to make sure your system satisfies Gradle’s prerequisites.
Android Studio comes with a working installation of Gradle, so you don’t need to install Gradle separately in that case.
In order to create a new build or add a Wrapper to an existing build, you will need to install Gradle according to these instructions. Note that there may be other ways to install Gradle in addition to those described on that page, since it’s nearly impossible to keep track of all the package managers out there.
Try Gradle
Actively using Gradle is a great way to learn about it, so once you’ve installed Gradle, try one of the introductory hands-on tutorials:
- Building Android apps
- Building Java applications
- Building Java libraries
- Building Groovy applications
- Building Groovy libraries
- Building Scala applications
- Building Scala libraries
- Building Kotlin JVM applications
- Building Kotlin JVM libraries
- Building C++ applications
- Building C++ libraries
- Building Swift applications
- Building Swift libraries
- Creating build scans
There are more samples available on the samples pages.
Command line vs IDEs
Some folks are hard-core command-line users, while others prefer to never leave the comfort of their IDE. Many people happily use both and Gradle endeavors not to discriminate. Gradle is supported by several major IDEs and everything that can be done from the command line is available to IDEs via the Tooling API.
Android Studio and IntelliJ IDEA users should consider using Kotlin DSL build scripts for the superior IDE support when editing them.
Executing Gradle builds
If you follow any of the tutorials linked above, you will execute a Gradle build. But what do you do if you’re given a Gradle build without any instructions?
Here are some useful steps to follow:
- Determine whether the project has a Gradle wrapper and use it if it’s there — the main IDEs default to using the wrapper when it’s available.
- Discover the project structure.
Either import the build with an IDE or run gradle projects from the command line. If only the root project is listed, it’s a single-project build. Otherwise it’s a multi-project build.
If you have imported the build into an IDE, you should have access to a view that displays all the available tasks. From the command line, run gradle tasks .
The help task can display extra information about a task, including which projects contain that task and what options the task supports.
Many convention-based builds integrate with Gradle’s lifecycle tasks, so use those when you don’t have something more specific you want to do with the build. For example, most builds have clean , check , assemble and build tasks.
From the command line, just run gradle
Gradle builds often follow standard conventions on project structure and tasks, so if you’re familiar with other builds of the same type — such as Java, Android or native builds — then the file and directory structure of the build should be familiar, as well as many of the tasks and project properties.
For more specialized builds or those with significant customizations, you should ideally have access to documentation on how to run the build and what build properties you can configure.
Authoring Gradle builds
Learning to create and maintain Gradle builds is a process, and one that takes a little time. We recommend that you start with the appropriate core plugins and their conventions for your project, and then gradually incorporate customizations as you learn more about the tool.
Here are some useful first steps on your journey to mastering Gradle:
- Try one or two basic tutorials to see what a Gradle build looks like, particularly the ones that match the type of project you work with (Java, native, Android, etc.).
- Make sure you’ve read What is Gradle?
- Learn about the fundamental elements of a Gradle build: projects, tasks, and the file API.
- If you are building software for the JVM, be sure to read about the specifics of those types of projects in Building Java & JVM projects and Testing in Java & JVM projects.
- Familiarize yourself with the core plugins that come packaged with Gradle, as they provide a lot of useful functionality out of the box.
- Learn how to author maintainable build scripts and best organize your Gradle projects.
The user manual contains a lot of other useful information and you can find samples demonstrating various Gradle features on the samples pages.
Integrating 3rd-party tools with Gradle
Gradle’s flexibility means that it readily works with other tools, such as those listed on our Gradle & Third-party Tools page.
There are two main modes of integration:
- A tool drives Gradle — uses it to extract information about a build and run it — via the Tooling API
- Gradle invokes or generates information for a tool via the 3rd-party tool’s APIs — this is usually done via plugins and custom task types
Tools that have existing Java-based APIs are generally straightforward to integrate. You can find many such integrations on Gradle’s plugin portal.