IntelliJ IDEA

Maven and IntelliJ IDEA

On February 10, 2021, we hosted the live stream Using Maven in IntelliJ IDEA by Java Champion and Java Group Leader Chandra Guntur, a very dear friend and amazing speaker. In this session, Chandra Guntur demonstrated some of his best tips for using Apache Maven with IntelliJ IDEA. You can find the slides from this presentation at this repository, and here’s the recording of the session. These tips could be useful no matter how much experience you have with Maven in IntelliJ IDEA.

I’d like to highlight some of the most exciting features of using Maven in IntelliJ IDEA. If you want a comprehensive account of the tools IntelliJ IDEA has for working with Maven, be sure to check out the full recording.

Visual representation of all dependencies in your pom.xml file

When you see a list of dependencies in your project, it can be easy to miss the transitive ones. One of my favorite features in IntelliJ IDEA is the ability to view all the dependencies in code visually. You can also save this visual representation as an image and share it with your team.

There are multiple ways to access this feature. In the Structure window, you can right-click on the dependencies node and select Diagrams, and then Show Dependencies… In the editor window, you can access the context menu by right-clicking and accessing Diagrams and then selecting Show Dependencies.

The same option is available in the Maven window as an icon, and it can also be accessed using shortcuts: Ctrl+Alt+Shift+U on Windows / Linux and ⌥⇧⌘U on macOS.

Assigning a shortcut to your Maven tool window

I often see developers who keep their tool window bars constantly on display because it seems to be the fastest way to access various tool windows. You can claim the real estate used by the tool window bars and quickly access tool windows like the Maven Tool window by using its shortcut (or assign one if none exists).

Assigning a shortcut to a tool window like Maven takes just a couple of steps, and they are the same steps you need to take to assign a shortcut to any tool window. Use Find Action by using Ctrl+Shift+A (or ⇧⌘A), search for ‘Maven’ with description ‘View | Tool Windows’. When the Find Action dialog appears, Press Alt+Enter to assign your shortcut. I used Alt+N.

There are other ways to assign a shortcut. For example, you can search for ‘Keymap’ in your IntelliJ IDEA settings, search for the ‘Maven tool window’, and then simply assign a shortcut to it.

Building Maven projects using archetypes

You can create new Maven projects using archetypes (an ‘archetype’ is a ‘template’). However, some archetypes might not be up-to-date with the latest Java or framework versions.

For example, when you use the Maven quickstart archetype, it might use an older version of the Java compiler and runtime than the one you selected for your project,as pom.xml values override the values you define in IntelliJ IDEA when creating a project. To fix such issues, you can open your pom.xml file and modify it. In this specific case (using the Maven quickstart archetype), you can open your project’s pom.xml file, search for the following text, and replace 1.7 with the Java version you want to use for your project:

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
</properties>

If you are using the preview language features, you can also add the compilerArgs tag to your pom.xml file.

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <maven.compiler.source>1.7</maven.compiler.source>
 <maven.compiler.target>1.7</maven.compiler.target>
   <compilerArgs>
      --enable-preview
   </compilerArgs>
</properties>

Quick tip: Most windows in IntelliJ IDEA use Type Ahead Search and let you search for text if you just start typing – you don’t have to look for the search text field. For example, in the preceding gif, I was able to search for the Maven quickstart archetype, simply by typing ‘quickstart’ in the search textbox, and without even pressing Enter. This can be quite useful when you can’t remember the exact or full name of an archetype or when there are a lot of options to choose from.

Additionally, if you are interested in creating your own archetypes, check out this guide.

Using different Maven versions

IntelliJ IDEA bundles Apache Maven so you don’t have to install it first to use it. This usually works well. However, if you are working with a team on a project, all the members of the team will probably want to work with the same version of Maven. In IntelliJ IDEA, switching to a different version of Maven is no problem.

When creating a new project, you can specify the Maven version to use. For existing projects, modify Maven home path in Settings.

Using pom.xml to open a Maven project in IntelliJ IDEA

Imagine you check out an open source project to work on, one that happens to be a Maven project. When you choose to open its pom.xml file in IntelliJ IDEA, the IDE detects that it can be opened either as a file or as a project. When you choose the latter, IntelliJ IDEA sets a project up and opens it for you. You can even accomplish this by simply dragging and dropping the project’s pom.xml file into IntelliJ IDEA.

Browsing through the structure of a Pom.xml using the Structure tool window

You might already be familiar with looking into the structure of your classes using the Structure tool window in IntelliJ IDEA. Did you know you can use the same tool window to browse through the structure of your pom.xml file? This can be useful for quickly navigating to its properties, dependencies, plugins, and other sections.

Other sections of the Maven tool window

In addition to being able to use the tool window’s icons to reload all Maven projects and show Dependencies as a diagram, you can also use the Maven tool window to view Maven lifecycle phases, plugins, run configurations, dependencies, and more. You can even use it to execute goals.

Jump to Source of pom.xml

While navigating the dependencies of your pom.xml file in the Maven tool window, you can use IntelliJ IDEA’s Jump to Source feature (F4 in Windows and Linux / ⌘Down on Mac) to view the details of the dependency in the editor. Navigate to the artifactId or groupId and use Jump to Source again to view the pom.xml file of the dependency.

Using this you can drill down and view any number of pom.xml files for the dependencies you want. When the editor window displays another pom.xml file, the contents of the Structure window will change accordingly.

Download sources and documentation

You can use the Maven tool window to download sources and documentation for specific dependencies in your project, or you can download all of them using a single command. This can be useful if you want to browse through the implementation details.

Life cycles – are there just a handful of them?

By default, the Maven tool window displays the most frequently used Maven lifecycles or phases. To view more phases than those listed in the Maven tool window, you can uncheck Show Basic Phases Only.

Using the Run window to track the execution of Maven phases

You can use the Maven tool window to select and execute multiple phases for your project, say verify and install. When you click on Run, the Run tool window appears and displays a collection of metrics, like the current execution phase and the time it takes to finish. It also marks the completed phases with a cross, allowing you to easily track which ones have been run successfully.

You can also click on the individual phases to view the messages specific to the execution of that phase. It is easy to read through the messages related to a phase, rather than wading through all the messages in one big chunk.

Creating and saving custom run configurations

Usually, all of the members of a team need to be working with the same Maven build or run configurations. In IntelliJ IDEA, you can create new run configurations, define the Maven goals you want to run for them, rename them, and store them as project files. Of course, you can also override the default directory where they are defined.

Saving these configuration files to your shared repository helps you share run configurations across teams.

Navigating to a module pom.xml from your project pom.xml

Imagine you have a multi-module project. While browsing through the list of modules in your root pom.xml, you can access a module’s pom.xml by using Ctrl+Click on the module’s name or by using Ctrl+B (or ⌘ B), which is a navigation feature in IntelliJ IDEA for accessing declarations or usages.

No implicit bi-directional relation between aggregator and child pom.xml

If a pom.xml file lists the coordinates of a parent pom.xml file (groupId, artifactId, and version), this doesn’t imply that the parent is aware of this child. Similarly, a pom.xml file might list modules as its children, but those child pom.xml files may or may not list any parents. In this case, the root.xml file is referred to as an aggregator pom.xml and not as a parent.

This is a powerful setup that allows a child or parent to execute commands or inherit values and properties without the other being aware of the relationship.

Using the IntelliJ IDEA terminal to run Maven commands

If you prefer executing Maven commands using the command line, you can use IntelliJ IDEA’s terminal to do so (meaning you don’t have to leave the IDE and start another application). Additionally, IntelliJ IDEA detects when you are executing Maven commands and reminds you that you can use the IDE to execute the command. For example, if you are using IntelliJ IDEA’s built-in terminal window to execute the Maven command ‘maven clean verify’, you just need to hit Ctrl+Enter (or Cmd+Enter) and IntelliJ IDEA will execute the command.

Using Run Anything to execute Maven commands

With the Run Anything feature, you can execute multiple goals with a Maven command. With code completion, you don’t even need to remember all the available options. You can actually perform a variety of tasks using the Run Anything window, including opening another project.

Dependencies Tool Window (new in IntelliJ IDEA 2021.2)

IntelliJ IDEA 2021.2 includes a new tool window – Dependencies, which can be used to update all your dependencies in a super convenient manner. It lists all your dependencies, their current version and the new versions that are available. You can upgrade all your dependencies or choose to upgrade selected ones. This saves you from browsing through the dependencies in your code base and checking for their newer version using tools external to IntelliJ IDEA.

Autocompletion in pom.xml file

One of the most used features in IntelliJ IDEA – auto-completion – is also available in your pom.xml file. You get the options to complete tags and their values too.

Summary

IntelliJ IDEA provides a wide variety of features that allow you to work on your Maven projects more easily and efficiently.

Happy developing!

image description