IntelliJ IDEA lets you manage Gradle projects. You can import, link, configure the Gradle composite build, and synchronize changes of your Gradle project with IntelliJ IDEA project.

Linking a Gradle project to the IntelliJ IDEA project

When a project is created by importing from Gradle model, this link is established automatically and the Gradle Projects tool window is enabled.

If an IntelliJ IDEA project is not linked to a Gradle project, then the Gradle tool window is disabled.

In this case, IntelliJ IDEA displays a message with a link that quickly lets you import your Gradle project and enable the Gradle Projects tool window.

If the Gradle Projects tool window is active then you have at least one Gradle project linked.

  1. Open Gradle Projects tool window.
  2. In the Gradle Projects tool window, click ../../Shared/new.png to attach a gradle project.
  3. In the dialog that opens, select the desired build.gradle file, and click OK.
  4. In the Import Module from Gradle window, specify options for the Gradle project that you are trying to link and click OK.

    The project is linked. The Gradle Projects tool window shows the toolbar and a tree view of Gradle entities.

Alternatively, you can link your Gradle project if you import your project as a Gradle module from the existing sources. In this case, you can link your Gradle project even if the Gradle Projects tool window is not available.

Navigating to the build.gradle file

  1. In the Gradle projects tool window, right-click on the linked project.
  2. From the context menu select ../../Shared/icon_gradle.png.

    IntelliJ IDEA navigates to the appropriate Gradle configuration file and the related build.gradle file opens in the editor.

Detaching or ignoring a linked Gradle project

  1. In the Gradle projects tool window, right-click on the linked project.
  2. From the context menu, select ../../Shared/delete.png (DeleteDelete ). Alternatively, you can select the linked project and click ../../Shared/delete.png on the Gradle tool bar.
  3. In the Import Gradle Projects pop-up window, clear the check box against the modules if you don't want to delete the project from the IntelliJ IDEA project's view.
  4. Click OK.

    The Gradle project is detached from the IntelliJ IDEA project and is not synchronized any more.

You can also de-activate Gradle project using Ignore Gradle Project option.

  1. In the Gradle Projects tool window, right-click the project that you want to ignore.
  2. In the context menu, select Ignore Gradle Project.
  3. In the window that opens, select projects and modules that you want to de-activate and click OK.

    In this case IntelliJ IDEA removes the selected projects and modules from the IntelliJ IDEA project's view and will not import them any more. However, you still are able to see the list of ignored Gradle modules and projects in the Gradle Projects tool window.

    If you want to activate your Gradle projects or modules, select Unignore Gradle Projects from the context menu.

Refreshing a linked Gradle project

  1. In the Gradle projects tool window, right-click on the linked project.
  2. From the context menu, select ../../Shared/refresh.png.

    On pressing this button, IntelliJ IDEA parses the project structure in the Gradle Projects tool window.

    IntelliJ IDEA cannot refresh just a part of your project, it refreshes the whole project including modules and dependencies.

    Note that if you configure a dependency through the Project Structure dialog (click ../../Shared/projectStructure.png on the main menu), the dependency will only appear in the IntelliJ IDEA Project tool window, not in the Gradle tool window. Also, note that the next time you refresh your project, IntelliJ IDEA will remove the added dependency since IntelliJ IDEA considers the Gradle configuration as a single source of truth.

If you selected Use auto-import when you created or imported a Gradle project then the re-import of the project is done automatically every time you make changes to the project.

Configuring Gradle Composite Build

Before you start configuring your composite build, make sure you have the Gradle version 3.1 or higher configured for your project.

Also, note that there are some restrictions on using Gradle composite build.

You can use settings.gradle file to include Gradle builds for your Gradle composite build.

  1. Open the settings.gradle file in the editor.
  2. Using includeBuild command, specify the location of the builds you want to add as dependencies to you project.

You can also use the Gradle Projects tool window to configure your composite build.

  1. Open your Gradle project.
  2. Link other Gradle projects that you want to use for your composite build.
  3. In the Gradle projects tool window, right-click your main project and from the context menu select Composite Build Configuration.
  4. In the Gradle Project Build Composite dialog, select projects that you want to include in your Gradle composite build.
  5. Refresh your main Gradle project.

    IntelliJ IDEA finds the included Gradle projects and treats them as IntelliJ IDEA modules.

Using gradle source sets

IntelliJ IDEA lets you use Gradle source sets in resolving your Gradle projects. The source set is treated as a module in the IntelliJ IDEA project. You can declare a custom source set and IntelliJ IDEA adds it as a module to the project.

  1. Select use separate module per source set when you create or import a Gradle project. Alternatively, click ../../Shared/settings.png icon in the Gradle projects tool window and on the Gradle settings page, select the same option.
  2. IntelliJ IDEA creates a main Source Sets directory that contains two source sets - main and test. For compiling the test sources there is a dependency to the main source set.

    ../../Shared/gradle_main_source_set.png

You can add a custom source set.

  1. Open the gradle.build file in the editor.
  2. Declare a custom source set. (In our example, it's api)

    ../../Shared/gradle_source_sets_editor.png

    (This source set contains interfaces without implementations. The implementations for the interfaces are in the default main source set.)
  3. Open Gradle projects tool window to see that IntelliJ IDEA added the api source set.

    ../../Shared/gradle_source_sets_tool_window.png

    The test source set contains the appropriate dependencies.

    ../../Shared/gradle_api_dependency.png

    Note that the default source set main has a compile dependency on the output of the api source set. Gradle will invoke the apiClasses task automatically if you compile the sources in the main source set.

    ../../Shared/gradle_source_sets_main_tool_window.png

  4. Open project structure (../../Shared/projectStructure.png). Notice that all source sets are separate modules that are grouped into a single module.

    ../../Shared/gradle_source_sets_project_structure.png

    If you click on the test module and select the Dependencies tab, you will see a list of dependencies for the source set.

    ../../Shared/gradle_source_sets_dependencies.png

You can also add custom tests and run them separately from the main ones using source sets.

  1. Declare a source set the same way as would declare the custom source set. Besides the name of you source set, specify the output directory and the task that will run you test.

    ../../Shared/gradle_integration_tests.png

  2. In the Gradle projects tool window, click Tasks | other.
  3. In the list that opens, double-click the integrationTest to run your integration tests.

    ../../Shared/gradle_run_integration_test.png

Delegating build and run actions to gradle

IntelliJ IDEA lets you delegate all your build and run actions to Gradle. If you invoke the build project action from the build menu, IntelliJ IDEA invokes the correct tasks using Gradle. Also, the run and debug actions from the run menu are executed with Gradle.

  1. Click ../../Shared/settings.png icon in the Gradle projects tool window. Alternatively, on the main menu select File | Settings | Build, Execution, Deployment |Build Tools |Gradle.
  2. Click Gradle and from the drop-down list, select Runner.
  3. On the Runner page, select Delegate IDE build/run actions to Gradle.

    ../../Shared/gradle_delegate_ide_run_to_gradle.png

  4. Click OK.
  5. Open any Java project with the build.gradle file.
  6. Invoke Build Project action (⌘F9⌘F9⌘BF7^ F9). Gradle compiles the code and displays it in the Run tool window.

    ../../Shared/gradle_build.png

  7. Run your main method (⌃⇧R⌃⇧F10⌃⇧R^ F9^ ⇧ F10). You can see that IntelliJ IDEA uses Gradle JavaExec task to run the class.

    ../../Shared/gradle_running_main_method.png

IntelliJ IDEA also lets you build WAR or EAR artifacts using Gradle.

  1. Create a Gradle Web project.(For more instructions, see Creating a Gradle project)

    ../../Shared/gradle_web_project.png

  2. Create the Tomcat server run/debug configuration.

    ../../Shared/gradle_tomcat_run_debug_config.png

  3. Click Deployment tab and add an artifact for your deployment.

    ../../Shared/gradle_deployment_artifact.png

  4. Click ../../Shared/run.png to start the Tomcat server configuration.

    In the Run tool window you can see that the artifact is built by Gradle and deployed by the IntelliJ IDEA Tomcat's integration.

    ../../Shared/gradle_web_deployment.png

Configuring and using a gradle test runner

IntelliJ IDEA lets you use different test runners to execute tests in a Gradle project. You can use JUnit to run your tests (in this case tests are run much faster due to the incremental compilation), delegate the testing process to Gradle (in this case when you run your tests, you will get the same results on the continuous integration (CI) server. Also, no matter how difficult your Gradle project is, tests that are run in command line will always work in IDE), or decide which test runner to use specifically per each test.

  1. In the Gradle Projects tool window, select ../../Shared/settings.png icon.
  2. In the Settings dialog, right-click Gradle and from the drop-down list, select Runner.
  3. On the page that opens, in the Run test using drop-down list, select an option that you want to use for your test run.

    ../../Shared/gradle_test_runner.png

    (The default value is Platform Test Runner.)
  4. Click OK.
  5. In your Gradle project, in the editor, create or select a test that you want to run.
  6. From the context menu, select Run <test name>.

    Alternatively, click ../../Shared/run.png icon in the left gutter. ../../Shared/gradle_run_test_gutter.png
  7. Depending on what you chose as your test runner option, IntelliJ IDEA runs your tests in one of the following ways:
    • If you selected Platform Test Runner, IntelliJ IDEA runs tests using a JUnit test runner and displays the output in the Run JUnit tool window. ../../Shared/platform_test_runner_output.png
    • If you selected Gradle Test Runner, IntelliJ IDEA runs tests using a Gradle test runner and displays the output in the Run Gradle tool window. ../../Shared/gradle_test_runner_output.png
    • If you selected Let me choose per test, IntelliJ IDEA lets you choose between JUnit or Gradle to run your test. ../../Shared/gradle_choose_per_test.png Once, you select the test runner, IntelliJ IDEA will remember your selection and automatically will run your test using the option you've chosen.
    You can change your selection, in the Runner settings page.