IntelliJ IDEA 2020.3 Help

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

  1. On the welcome screen, click New Project.

  2. On the page that opens, from the options on the left select Gradle.

  3. To the options on the right, IntelliJ IDEA automatically adds a project SDK (JDK). 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.

    In the Additional Libraries and Frameworks area IntelliJ IDEA selects the default option Java which is what we need for our project.

    New project Gradle
    Click Next.
  4. On the next page of the wizard let's specify our project's name (FizzBuzz) and the location. We can use the default information for ArtifactId which basically is the name of our project and leave the default information in the version and GroupId fields.

    New Project wizard Gradle page
    Click Finish.

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:
    plugins { id 'java' } group 'org.example' version '1.0-SNAPSHOT' repositories { mavenCentral() } dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' }

    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.

  • IntelliJ IDEA also creates the src folder with main and test subdirectories in the Project tool window.

    Gradle project view

  • IntelliJ IDEA enables the dedicated Gradle tool window with a liked project and its default tasks. We will use this window to run our tasks.

    Gradle tool window
    If for some reason you closed this window, you can always access it from the main menu by selecting View | Tool Windows | Gradle.

  • The Gradle settings in our project are used to store the information about the linked projects, Gradle JVM, and build actions. You can quickly access them from the Gradle tool window (click Icons general settings on the toolbar).
    the Gradle settings

    As you can see, the build and test actions are delegated to Gradle. Also, the Gradle wrapper was used to determine Gradle for our project.

  • The project structure (Ctrl+Alt+Shift+S) contains information about the project's JDK and a language level used in the project.

    Project Structure

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

  1. In the Project tool window open the src folder.

  2. Click the main directory then right-click the java subdirectory and from the list select New | Package.

  3. In the New Package dialog, let's enter a name of our package which is com.gradle.tutorial.

  4. Now right-click the package and select New | Java Class.

  5. In the New Java Class dialog specify a name of your Java class and click OK. In our case it is FizzBuzzProcessor.

    Create a new class dialog

  6. Add the following code to the main FizzBuzzProcessor class:

    package com.gradle.tutorial; public class FizzBuzzProcessor { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { System.out.println(convert(i)); } } public static String convert(int fizzBuzz) { if (fizzBuzz % 15 == 0) { return "FizzBuzz"; } if (fizzBuzz % 3 == 0) { return "Fizz"; } if (fizzBuzz % 5 == 0) { return "Buzz"; } return String.valueOf(fizzBuzz); } }

Our application is ready. Now, let's create the necessary tests for it.

Create a test class

  1. Open the main class FizzBuzzProcessor in the editor, place the caret at the class name and press Ctrl+Shift+T.

  2. In the dialog that opens, let's make sure that our testing library is JUnit4 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.

  3. Now open the created test class and add the following code:

    package com.gradle.tutorial; import org.junit.Assert; import org.junit.Test; public class FizzBuzzTest { @Test public void FizzBuzzNormalNumbers() { FizzBuzzProcessor fb = new FizzBuzzProcessor(); Assert.assertEquals("1", fb.convert(1)); Assert.assertEquals("2", fb.convert(2)); } @Test public void FizzBuzzThreeNumbers() { FizzBuzzProcessor fb = new FizzBuzzProcessor(); Assert.assertEquals("Fizz", fb.convert(3)); } @Test public void FizzBuzzFiveNumbers() { FizzBuzzProcessor fb = new FizzBuzzProcessor(); Assert.assertEquals("Buzz", fb.convert(5)); } @Test public void FizzBuzzThreeAndFiveNumbers() { FizzBuzzProcessor fb = new FizzBuzzProcessor(); Assert.assertEquals("Buzz", fb.convert(5)); } }

Step 3. Run the application with Gradle

Let's quickly run the application to see if it works.

Run main class from the editor

  1. Open the main class FizzBuzzProcessor in the editor.

  2. In the left gutter, click Icons toolwindows tool window run and select Run 'FizzBuzzProcessor.main()'.

    Run application in Gradle

  3. Check the result in the Run tool window.

    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 Icons toolwindows tool window run in the left gutter of the editor.

    Gradle Run test from the gutter

The result of the test will be displayed in the Run tool window.

Run tool window /test passed

If we change the default number in one of the tests, it will fail.

Run tool window / test failed

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.

  1. In the Project tool window, double click the build.gradle file to open it in the editor.

  2. Add the following code:

    jar { manifest { attributes "Main-Class": "com.gradle.tutorial.FizzBuzzProcessor" } from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } }

  3. Click Gradle common icons gradle load changes in the editor to load the changes to your project.

  4. In the Gradle tool window, open the project's node, then the Tasks node and double-click the build task to run it.
    Gradle tool window: build task

    IntelliJ IDEA creates the build directory that contains our JAR file.

    Project tool window: build directory

    You can run the created JAR file in the command line with java -jar command.

  5. Check the Run tool window for the results.
    Run tool window: build task

    Note that the build task includes the test task that Gradle executes. So, if we make a mistake in one of our tests, the test task will fail and the build task will fail as well.

    Run tool window: build with failed test

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

  1. In the Project tool window, double click the build.gradle file to open it in the editor.

  2. Let's add id 'application' to the plugins section and the following code:

    application { mainClassName = 'com.gradle.tutorial.FizzBuzzProcessor' }

  3. Click Gradle common icons gradle load changes in the editor to load the changes to your project.

  4. In the Gradle tool window, open the project's node, then the Tasks node. We can see that Gradle added the distribution node. Open the node and double-click the assembleDist task to run it.

    If we check the build directory now, we'll see that IntelliJ IDEA created additional directories.

    Project tool window: build directory
  5. In the Gradle tool window, click Run anything on the toobar.

  6. In the window that opens, enter the gradlew run command.
    Run anything: gradlew run

    We should have the same result as when we ran the application in the IntelliJ IDEA editor.

    Run tool window: run task output

    Alternatively, you can execute the run task under the application node.

    Gradle tool window: run task
Last modified: 08 March 2021