IntelliJ IDEA 2019.2 Help

Getting Started with Gradle

Create 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.

    New project Gradle
    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.

    New Project wizard Gradle page
    Click Next.

  4. We've already specified our project's name, let's specify the location of our project and click Finish.

    New Project wizard Gradle project information 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, refer to Gradle.

Add Java and test classes to a 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 list select New | Java Class.

    Add Java class to a Gradle project

  3. In the Create New Class dialog specify the name of your Java or test class and click OK.

    Create a new class dialog

  4. Add the following code for the HelloWorld class:

    import java.io.PrintStream; import static sun.misc.Version.print; public class HelloWorld { public static void main(String[] args) { print(System.out); } public static void print(PrintStream out) { out.println("Hello, World!"); } }

  5. Add the following code for the Test class:

    import org.junit.Assert; import org.junit.Test; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class MyTest { @Test public void name() throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); HelloWorld.print(new PrintStream(out)); String s = out.toString(); Assert.assertEquals("Hello", s); } }

Run your application in a Gradle project

  1. In the editor, in the left gutter, press icons toolwindows toolWindowRun svg and select Run 'HelloWorld.main()'.

    Run application in Gradle

  2. Check the result in the Run tool window.

    Run tool window

Run tests in a Gradle project

We can run our test using several different ways:

  • the editor: click icons toolwindows toolWindowRun svg in the left gutter of the editor.

    Gradle Run test from the gutter

  • the test Gradle task: 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.

    Run the Gradle test task

  • the Run test using option in the Settings/Preferences dialog.

    Gradle settings

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

Run tool window / running Gradle test
Last modified: 20 September 2019