IntelliJ IDEA 2018.1 Help

Getting Started with Gradle

Creating a new Gradle Project

  1. Open Project Wizard, in the left-hand pane select Gradle.
  2. In the right-hand pane, IntelliJ IDEA automatically adds a project SDK (JDK) and a default option Java in the Additional Libraries and Frameworks area. You can edit this information if you like.
    gradle new project
    Click Next.
  3. On the next page of the wizard let's specify ArtifactId which basically is the name of our project. We can use the default information in the version field. Unless we plan to deploy our project in some Maven repository we don't need to specify a GroupId.
    gradle groupId
    Click Next.
  4. On the next page of the wizard, let's leave options Create separate module per source set and Use default gradle wrapper (recommended) selected. Let's also specify the Use auto-import option to resolve all the changes made to the Gradle project automatically every time we refresh our project.
    Click Next.
    gradle settings page
  5. We've already specified our project's name, let's specify the location of our project and click Finish.
    gradle project info page

IntelliJ IDEA creates a project with the build.gradle file and the src folder with main and test subdirectories.

gradle project view

IntelliJ IDEA also creates a dedicated tool window with default tasks.

gradle tool window
For more information on creating a Gradle project with the options that are out of this scope, see Gradle.

Adding Java and test classes to a Gradle project

Let's add Java and test classes to our Gradle project.

  1. In the Project tool window open the src folder.
  2. Right-click the main or test directory then the java subdirectory and from the drop-down list select New | Java Class.
    gradle add java class
  3. In the Create New Class dialog specify the name of your Java or test class and click OK.
    gradle create new class dialog

Let's add the following code:

  • for our HelloWorld class -
    gradle helloworld example
  • for our Test class -
    gradle test example

Running tests in a Gradle project

We can run our test in several different ways:

  • using the editor - click run in the left gutter of the editor.
    gradle run test editor
  • using the Gradle task test - in the Gradle Projects tool window open the Tasks directory and then the verification subdirectory. In the list that opens, double-click test to run your test.
    gradle run test task
  • using the Delegate all IDE builds/run actions to gradle option in the Settings dialog.
    gradle delegate to gradle option

In all these cases the result of the test will be displayed in the Run tool window.

gradle test result
Last modified: 26 June 2018

See Also