Intellij and Gradle Setup and Integration
There are two main ways to setup your project in Intellij when using gradle as your build tool for a java project:
- Import your gradle project using Intellij.
- Build your Intellij project using the Gradle idea plugin
Here, I’ll discuss the three approaches and the pros and cons. The end will talk about I do in the projects that I work on.
Import your Gradle project Using Intellij
Intellij has two big nice features: “ability to import projects from the existing Gradle models. So doing, IntelliJ IDEA downloads all the necessary dependencies” and “ability to synchronize structures of Gradle and IntelliJ IDEA projects.” In practice, this is where you open a project by selecting the “build.gradle” file in your project and the Intellij wizard(ry) does the rest.
- Very simple.
- Changes are synchronized. As you change your
build.gradlefile, Intellij will automatically pull the dependency down and add it to your build path. Make sure that you select the check box as seen below for this feature.
- Intellij doesn’t import all of your settings. Specifically, if there are plugin configurations for your project that you use with your idea plugin settings
NOTE: JetBrains recommends that if you want to share your project settings on your team, that you should “.idea project configuration directory should be shared via version control.” Source and here but it contains a number of “guidelines” of things that you should not include because they “contain keystore passwords”, “conflicts if another developer has the same name”. I have also found when a project has these files checked in ( incorrectly albeit ), that every commit will inevitably contain some .iws change.
Build your Intellij project using the Gradle idea plugin
Gradle has an idea plugin that allows you to build your Intellij project files. You just need to include the line:
and when you run the command
gradlew idea it will download your dependencies and setup your project.
- Very simple to setup dependencies.
- You can customize project settings including your Version Control, JDK Version, and other plugin settings.
- Changes are not automatically synchronized. You need to run
gradlew ideato rebuild your dependencies.
- Making custom changes are not simple. Most of them involve XML manipulation using groovy.
So What Did I Choose?
I chose to use the second option. I found that even if the setup is harder, it’s worth it to automatically configure more parts of the development environment. We have strong developers that spend the time to get their environment just right. But, there are equally more people that will work with less optimal systems because they are unsure how to get it working.