IntelliJ IDEA 2019.1 Help

Work with Gradle projects

IntelliJ IDEA lets you manage Gradle projects. You can link, ignore projects and synchronize changes in Gradle and IntelliJ IDEA projects. Also, you can configure a Gradle composite build.

When a project is created by importing from Gradle model, the link of the project 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 the Gradle Projects tool window.

  2. In the Gradle Projects tool window, click icons general add svg 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 existing sources. In this case, you can link your Gradle project even if the Gradle Projects tool window is not available.

  1. In the Gradle projects tool window, right-click a linked project.

  2. From the context menu, select gradle.

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

Detach or ignore a linked Gradle project

  1. In the Gradle projects tool window, right-click a linked project.

  2. From the context menu, select icons general remove svg (Delete ). Alternatively, you can select the linked project and click icons general remove svg on the Gradle toolbar.

  3. In the Import Gradle Projects popup, clear the checkbox against the modules if you don't want to delete the project from the IntelliJ IDEA Project tool window.

  4. Click OK.

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

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

  1. In the Gradle Projects tool window, right-click the project that you want to ignore.

  2. From 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 tool window and will not import them anymore. However, you would still be 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.

Refresh a linked Gradle project

  1. In the Gradle projects tool window, right-click a linked project.

  2. From the context menu, select icons actions refresh svg.

    On clicking this icon, 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.

    If you configure a dependency through the Project Structure dialog (click projectStructure on the main menu), the dependency will only appear in the IntelliJ IDEA Project tool window, not in the Gradle projects tool window. 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.

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

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

  1. Open the settings.gradle file in the editor.

  2. Using the includeBuild command, specify the location of the builds you want to add as dependencies to your project.

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

  1. Open a Gradle project.

  2. Link other Gradle projects that you want to use for the 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.

Use Gradle source sets

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

Create a source set

  1. Select use separate module per source set when you create or import a Gradle project. Alternatively, click icons general settings svg 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.

    gradle main source set

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)

    gradle source sets editor
    (This source set contains interfaces without implementations. The implementations for the interfaces are in the default main source set.)

  3. Open the Gradle projects tool window to see that IntelliJ IDEA added the api source set.

    gradle source sets tool window
    The test source set contains the appropriate dependencies.
    gradle api dependency
    Note that the default main source set has the 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.
    gradle source sets main tool window

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

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

Use source sets for custom tests

You can add custom tests and run them separately from the main ones using a source set feature.

  1. Declare a source set the same way as you would declare the custom source set. Besides the name of you source set, specify an output directory and a task that will run the declared test. For example, declare an integration test (integrationTest).

    gradle integration tests

  2. In the Gradle projects tool window, click Tasks | other.

  3. In the list that opens, double-click the integrationTest to run it.

    gradle run integration test

Add package prefixes in the Gradle project

If you use package prefixes in your Gradle project, specify them in the build.gradle file. That way, everything is saved when you reimport your project.

For more information, see https://github.com/JetBrains/gradle-idea-ext-plugin.

  1. Open the build.gradle file.

  2. Add the following plugin to support package prefixes:

    plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "0.5" }

  3. Add the package prefixes. For example, you have the following set of source sets:

    sourceSets { main.java.srcDirs = [] main.java.srcDirs += "src" main.java.srcDirs += "src/main/java" main.java.srcDirs += "../other-root/src/main/java" }
    Add the package prefixes (in our case it is "org.example") to them with the following code:
    idea { module { settings { packagePrefix["src"] = "org.example" packagePrefix["src/main/java"] = "org.example" packagePrefix["../other-root/src/main/java"] = "org.example" } } }

  4. Reimport your changes or use the auto-import.

Specify project settings in the build.gradle file

Using the https://github.com/JetBrains/gradle-idea-ext-plugin plugin, you can describe project settings such as project encodings, and the encoding for properties files inside the build.gradle file.

  1. Open the build.gradle file.

  2. Add the following plugin to support the encodings configuration:

    plugins { id "org.jetbrains.gradle.plugin.idea-ext" version "0.5" }

  3. Describe the project encodings with the following code:
    import org.jetbrains.gradle.ext.EncodingConfiguration.BomPolicy idea { project { settings { encodings { encoding = 'windows-1251' bomPolicy = BomPolicy.WITH_NO_BOM properties { encoding = '<System Default>' transparentNativeToAsciiConversion = false } mapping['../sample-gradle-free/module'] = 'windows-1251' mapping['module'] = 'windows-1251' mapping['module2/src/main/java'] = 'windows-1251' } } } }
  4. Reimport your changes or use the auto-import.

Use buildSrc

If you have a large Gradle script that includes several Java, Groovy, or Kotlin classes, you can move such classes to the buildSrc directory and refer to them from your main Gradle script. In this case you ensure the readability of the build.gradle file.

  1. If you don't have and existing buildSrc, add it as a Gradle module to your main project.

    gradle buildSrc dir

  2. Open your main build.gradle file in the editor and move the classes you need to the main subdirectory of the buildSrc directory.

    gradle groovy class

  3. Run your task from your project's build.gradle file.

    gradle main build file

Gradle build and run actions

In Gradle projects IntelliJ IDEA uses Gradle for build and run actions by default.

When you build a project (Build | Build Project), IntelliJ IDEA invokes the correct tasks using Gradle. Also, the Run and Debug actions from the Run menu are executed with Gradle.

HotSwap is also gets triggered and the classes are reloaded during a debugging process.

If you want to use IntelliJ IDEA for building a Gradle project, you need to explicitly specify so.

Delegate a build to IntelliJ IDEA

  1. Click icons general settings svg 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 list, select Runner.

  3. On the Runner page, clear the Delegate IDE build/run actions to Gradle option.

    gradle delegate ide run to gradle
    Note that the Run test using option stays active and you can select how you want to run your tests even if you delegated all build and run actions to IntelliJ IDEA.

  4. Click OK.

Now if you build your Gradle project, it will be built with IntelliJ IDEA.

Build a linked Gradle project

If you have linked projects, you can configure how to build each linked project.

  1. In the Settings/Preferences dialog (Ctrl+Alt+S), go to Build, Execution, Deployment | Gradle.

  2. On the Gradle settings page, in the Linked Gradle Projects, select the one for which you want to configure the delegation settings.

  3. In the Delegate settings section, specify Build and Run using and Run tests using options. Click OK.

Build WAR or EAR artifacts with Gradle

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

    gradle web project

  2. Create the Tomcat server run/debug configuration.

    gradle tomcat run debug config

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

    gradle deployment artifact

  4. Click icons toolwindows toolWindowRun svg 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.

    gradle web deployment

Last modified: 20 May 2019