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

Running gradle tasks

IntelliJ IDEA lets you run Gradle tasks using several different ways.

Running a gradle task from the gradle tool bar

  1. In the Gradle projects tool window, on the tool bar, click images/gradle_icon.png.
  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.

    images/gradle_run_task_from_toolbar.png

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

    images/gradle_task_run_result.png

    IntelliJ IDEA also saves the task in Gradle Projects under the Run Configurations node.

    images/gradle_run_config_task_save.png

Running a gradle task via run configurations

  1. Right-click the task for which you want to create the Run configuration.
  2. From the drop-down list select Create 'task name'.

    images/gradle_run_config_create.png

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

    images/run_debug_gradle_task.png

    IntelliJ IDEA displays the task under the Run Configurations node.

    images/gradle_run_config_task_display.png

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

    images/gradle_config_task_run.png

Running a gradle task from the context menu

  1. Right-click the task that you want to run.
  2. From the context menu select Run 'task name'.

    images/gradle_task_run_from_menu.png

Running 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 images/add.png icon and select Gradle to add a new configuration.

    images/gradle_run_debug_config_add.png

  3. On the right side of the Run/Debug Configurations dialog, in the Name field, specify the name of your configuration.
    For example, specify the following settings:
    • Gradle project - click images/gradle_registered_projects_button.png and select the registered Gradle project.

      images/gradle_select_registered_project.png

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

    images/gradle_run_debug_config_settings.png

  4. Click OK.
    The created Run/Debug 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 or right-click the configuration and select Run to execute the task.

    images/gradle_run_configuration.png

Debugging gradle tasks in the gradle projects tool window

Tasks available for debugging via the Gradle projects tool window are the ones that implement org.gradle.process.JavaForkOptions interface. For example, test or run. For debugging Gradle script tasks themselves, use a Groovy run/debug configuration that appears in the context menu when you right-click on the script task in the editor.

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

    The list of tasks opens.

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

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

Assigning a shortcut to a gradle task

IntelliJ IDEA lets you assign shortcuts to Gradle tasks that can be run in several different 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.

    images/gradle_assign_shorcut.png

    The Keymap dialog opens.
  3. In the Keymap dialog, under the Gradle node you will see your task.

    images/gradle_task_in_keymap_dialog.png

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

    images/gradle_add_shorcut_options.png

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

    images/gradle_keyboard_shortcut.png

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

    images/gradle_shortcut_displayed.png

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.

    images/gradle_choose_task_while_in_keymap_dialog.png

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

    images/gradle_select_task_keymap.png

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

Configuring 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 the Gradle project.
  2. On the context menu, select Task Activation.
  3. In the Task Activation dialog, click images/add.png icon.
  4. On the Choose activation phase menu, select when you want your task to run, for example, Before Build, After Sync, etc.
  5. On the Choose task menu, select the actual task.

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

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

You also can create a run/debug configuration that would depend on the 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 Before Launch section, click images/add.png 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 default configurations (for example, a bundled Gradle version) to run the task.
  5. Press OK.