Last Updated on November 25, 2022
If you’re using Gradle as the build automation tool for your project, then there’s no better IDE of choice than IntelliJ IDEA 2019. This is thanks to its tight integration which exposes much of the Gradle functionality via the UI, meaning there’s little reason to revert to the command line. Here are 5 tips for making better use of Gradle with IntelliJ IDEA.
1. Create projects as Gradle projects
Select Create New Project from IntelliJ IDEA’s home screen (or File > New > Project):

You are given the option to create a Gradle project. This is going to save you some time as it can automatically:
- initialise the project with Gradle build.gradle and settings.gradle files
- add the Gradle wrapper
- apply the Java plugin
- create src/main/java and src/test/java code directories

Info: the Gradle wrapper is a script that you can use to execute Gradle (gradlew for Linux and gradlew.bat for Windows). The benefit is that it can be committed into version control meaning that anyone who checks out your project can run it without having to install Gradle on their machine.
2. Use the Gradle panel to run tasks
Whenever you need to run a Gradle task you can do this through IntelliJ IDEA’s Gradle panel. Click on Gradle on the right hand side (assuming you have Tool Window Bars enabled) or use the quicker Ctrl + Tab, G
shortcut.

Then just double click on one of the tasks to run it:

3. Customise your Gradle task executions
Following on from point 2, if we need to pass any extra parameters to Gradle we can do so by right clicking the task name in the Gradle panel and selecting Edit:

This presents us with the Edit Run Configuration dialog, where we can add any extra parameters we want. For example, if we wanted to increase the log output we could enter --info
in the Arguments text box:

Click OK then run the task by selecting the run configuration from the Navigation Bar at the top, and hitting the green Run button:

We now get info level output from the Gradle task execution:

Important: note that your extra arguments will only be applied if you hit the green run arrow at the top, and not if you double click the task in the Gradle panel.
4. Automatically import the project when you change Gradle’s build.gradle file
By default, when you change build.gradle you’ll have to reimport the project by clicking the Reimport button at the top left of the Gradle panel:

You can change this to happen automatically though whenever you make a change to build.gradle. Go to File > Settings (or hit Ctrl + Alt + s
), and navigate to Build, Execution, Deployment > Build Tools > Gradle. Select Automatically import this project on change to build script files and hit OK:

JetBrains, who created IntelliJ IDEA, say that this setting can slow things down on large projects. I haven’t seen any issues so far though, but it could be the case if you have less CPU or memory available.
5. Build and run Gradle project using IntelliJ IDEA (at your peril)
Recent versions of IntelliJ IDEA are automatically set to build projects using Gradle. This means that compilation and execution of code and tests is deferred 100% to Gradle by running Gradle tasks in the background.
This should be the option of choice, but in certain scenarios there may be an advantage to using IntelliJ IDEA to build and run. In the words of JetBrains:
In a pure Java/Kotlin project, building and running by means of the IDE might be faster, thanks to optimizations. Note, that the IDE doesn’t support all Gradle plugins and the project might not be built correctly with some of them.
So if you’re experience a slow build, this could be an option to try. Go to File > Settings (or hit Ctrl + Alt + s
), and navigate to Build, Execution, Deployment > Build Tools > Gradle. Select IntelliJ IDEA from the Build and run using and Run tests using drop down lists, and hit OK:

This has the following effects:
a) Compiled classes output to out directory
Compiled classes will be output to an out directory rather than Gradle’s default build directory. The reason for this is to keep the IntelliJ and Gradle builds completely separate.

b) Execution is via Java
If I run a class, for example a public static void main
method, IntelliJ IDEA is going to run that using a Java command rather than Gradle. We can see this in the run output:

c) Annotation processor generated classes go to generated directory
If you’re using an annotation processor to generate classes (e.g. MapStruct) your generated classes will be output to a src/main/generated directory in your project source, rather than the build/generated directory as is custom with Gradle.

d) Custom builds may stop working
If you have a more complex build that relies on running tasks in addition to standard compilation, this may not working when using IntelliJ IDEA to build your project. This is because Gradle is not being used, therefore your additional task will not run during the build process.
An example of this would be a task that generates additional classes, such as the Gradle Jooq Plugin. In this case, you can run the task separately manually.
Resources
INTELLIJ IDEA SHORTCUTS
Check out this cheat sheet
VIDEO
If you prefer to learn in video format, check out this accompanying video to this post on the Tom Gregory Tech YouTube channel.
