Why IntelliJ Wouldn’t Import My Maven Dependencies

I had my first introduction to PHPStorm five years ago now, and I’ve become a huge fan of JetBrains IDEs. In fact, I currently have four installed on my main development machine – PHPStorm, RubyMine, PyCharm and IntelliJ.

Recently I had my first taste of Maven. The other developers on the project told me that IntelliJ has great support for Maven so I was frustrated when two hours later I still hadn’t worked out how to get it to download the project’s dependencies. I was greeted with a sea of red every time I opened a class file. My first two attempts at clearing this involved selecting Build -> Make from the menu bar, or clicking the “Reimport All Maven Projects” button on the Maven toolbar, but after trying these several times each I was stumped.

By running mvn compile on the command line (or even from IntelliJ’s Maven toolbar), I was able to download the dependencies into my local repository. The sea of red persisted though, until I manually added them all onto my classpath in IntelliJ’s Project Structure dialog. Yuck!

For once my Google fu failed me. Stack Overflow wasn’t a lot of help. The IntelliJ community forums had a few questions on this page, but by following all of the random suggestions I managed to muck up my project settings so royally I decided to chuck them out and start again.

And that was when I figured it out. After selecting New -> Project from Existing Sources, I took a proper look at this dialog:

Notice the third option in that greyed-out list there? Turns out that sometimes trusting your IDE’s default options is not the right strategy. When I selected “Import project from external model” and Maven, everything “just worked” and IntelliJ pulled in all my dependencies and put them on the classpath for me. No more red!