IntelliJ IDEA 2019.1 Help

Work with Gradle tasks

IntelliJ IDEA lets you create, debug and manage Gradle tasks in your project.

Run Gradle tasks

IntelliJ IDEA lets you run Gradle tasks using several different ways such as using a toolbar in the Gradle tool window, using a run configuration, using a context menu, and even run several tasks using one run configuration.

Moreover, when you work in the distraction-free mode (without toolbars and tool windows), you can run Gradle tasks or commands from the Run Anything window. Press Ctrl twice to open it, type your command and press Enter.

Run Anything

You can check the Mastering shortcuts section for more information.

Run a Gradle task from the Gradle toolbar

  1. In the Gradle projects tool window, on the toolbar, click gradle icon.

  2. In the Run Gradle Task dialog, in the Command line field, start entering the name of your task. You can see that IntelliJ IDEA displays the list of Gradle tasks from which you can select the appropriate one. Click OK.

    gradle run task from toolbar

  3. IntelliJ IDEA runs the selected task and displays the result in the Run tool window.

    gradle task run result
    IntelliJ IDEA also saves the task in Gradle projects under the Run Configurations node.
    gradle run config task save

Run a Gradle task via Run Configurations

  1. Open the Gradle projects tool window.

  2. Right-click the task for which you want to create the Run configuration.

  3. From the context menu select Create 'task name'.

    gradle run config create

  4. In Create Run/Debug Configuration: 'task name', specify the task settings and click OK.

    run debug gradle task
    IntelliJ IDEA displays the task under the Run Configurations node.
    gradle run config task display

  5. Double-click the task to run it or right-click the task and from the context menu select Run.

    gradle config task run

Run a Gradle task from the context menu

  1. Open the Gradle projects tool window.

  2. Right-click a task that you want to run.

  3. From the context menu select Run 'task name'.

    gradle task run from menu

Run several Gradle tasks with one Run/Debug configuration

  1. Select Run | Edit Configurations (Shift+Alt+F10).
    The Run/Debug Configurations dialog opens.

  2. In the Run/Debug Configurations dialog, click Add icon and select Gradle to add a new configuration.

    gradle run debug config add

  3. On the right side of the Run/Debug Configurations dialog, in the Name field, enter the name of your configuration.
    As an example, specify the following settings:
    • Gradle project - click gradle registered projects button and select the registered Gradle project.

      gradle select registered project

    • Tasks - specify tasks you want to execute with this configuration. You can run more than one task.
      For example, specify clean and build.

    • VM options - you can customize VM options. For example, specify -Xmx3g.

    • Arguments - you can specify Gradle command line parameters. For example, specify --debug.

    gradle run debug config settings
  4. Click OK.
    The created configuration is added to the Run Configurations node in the Gradle Projects tool window and is treated as a Gradle task.

  5. Double-click the configuration to run the task or right-click the configuration and select Run.

    gradle run configuration

Debug Gradle tasks and scripts

You can debug Gradle tasks and scripts the same way you debug your regular applications.

You can start the debug session from the editor or from the Gradle projects tool window.

Debug a Gradle script task

  1. Open your build.gradle in the editor.

  2. Set your breakpoints and in the left gutter right-click the Run ( artwork studio icons shell toolbar run dark) icon. From the context menu, select Debug <name>.

    gradle debug script

  3. IntelliJ IDEA starts the debugging session and you can check the results in the Debug tool window.

    gradle debug tool win

Use the Gradle projects tool window to debug Gradle tasks that implement the org.gradle.process.JavaForkOptions interface, for example, test or run.

Debug a Gradle task

  1. In the Gradle projects tool window, in the Tasks area, double-click a Gradle project.

    The list of tasks opens.

  2. In the list of tasks, select the task which you want to debug, right-click it and from the context menu select Debug.

    The debugging process is started and the task is added to the list of the recent tasks located under the Run Configurations node.

Assign a shortcut to a Gradle task

IntelliJ IDEA lets you assign shortcuts to Gradle tasks that can be run in several ways and execute those tasks with a single key-stroke. You can also assign a shortcut to the Gradle run/debug configuration that can contain more than one task.

  1. In the Gradle projects tool window, right-click the desired task.

  2. From the context menu, choose Assign Shortcut.

    gradle assign shorcut
    The Keymap dialog opens.

  3. In the Keymap dialog, under the Gradle node navigate to your task.

    gradle task in keymap dialog

  4. Right-click the task and from the list that opens, select a type of the shortcut you want to assign.

    gradle add shorcut options

  5. In the dialog that opens, depending on the shortcut's type, configure your shortcut and click OK.
    In our case let's add a keyboard shortcut.

    gradle keyboard shortcut
    You can see that the shortcut is displayed against your task in the Gradle projects tool window.
    gradle shortcut displayed

While in the Keymap dialog, you can add a new task to which you want to assign a shortcut.

  1. In the Keymap dialog, under the Gradle node, click Choose a task to assign a shortcut.

    gradle choose task while in keymap dialog

  2. In the dialog that opens, select a task you need and click OK.

    gradle select task keymap
    The task is added to the list under the Gradle node. Now you can configure the shortcut.

Configure running triggers for Gradle tasks

IntelliJ IDEA lets you run Gradle tasks before your project's execution or set other conditions using the task activation configuration.

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

  2. From the context menu, select Task Activation.

  3. In the Task Activation dialog, click Add icon.

  4. On the Choose activation phase menu, choose when to run your task, for example, Before Build, After Sync,and so on.

  5. On the Choose task menu, select the actual task.

    The task and activation phase will be added to the list in the Task Activation dialog. You can also see the activation phase name against the selected task in the Gradle projects tool window.

Alternatively, you can select the activation phase name from the context menu when you right-click the task you want to execute in the Gradle projects tool window.

You can also create a run/debug configuration that would depend on a Gradle task.

  1. On the main menu, select Run | Edit Configurations to open the run/debug configuration for your project.

  2. In the Run/Debug Configurations dialog, in the Before Launch section, click the icons general add icon.

  3. In the list that opens, select Run Gradle task.

  4. In the Select Gradle Task dialog, specify the project and the task that you want to execute before launching the project. You can specify a Gradle linked project or any other Gradle project. Note that if your Gradle project is not linked then IntelliJ IDEA will use the default configurations (for example, a bundled Gradle version) to run the task.

  5. Click OK.

Last modified: 20 June 2019