IntelliJ IDEA 2019.1 Help

Work with Maven dependencies

IntelliJ IDEA lets you manage Maven dependencies in your project. You can add, import Maven dependencies, and view them as diagrams.

Add a Maven dependency

IntelliJ IDEA lets you add a Maven dependency to your project. We recommend that you specify the dependency inside your POM. Dependencies that you set up manually inside IntelliJ IDEA module settings will be discarded on the next Maven project import.

  1. Open your POM in the editor.

  2. Press Alt+Insert to open the Generate context menu.

  3. From the context menu, select Dependency or Dependency Template for quick search.

  4. In the dialog that opens either search for artifacts or for classes if you switch to the Search for class tab.
    The syntax for searching maven artifacts is as follows:
    group-id:artifact-id:version
    maven search artifact
    You can also use the :*: wildcard character to specify your search where : acts as a separator between group-id, artifact-id and version.

    For example, typing *:fest:* will return artifacts that contain "fest" part in their artifact-id.

    maven search artifact wildcard example
    Typing easy:*:4 will return artifacts that have "4" in their versions and "easy" in their group-id.
    maven search artifact wildcard example2
    Click Add. IntelliJ IDEA adds the dependency to your pom.xml.
    maven pom dependency
    IntelliJ IDEA also adds the dependency to the Dependencies node in the Maven Projects tool window and to the External Libraries in the Project tool window.
    If the added dependency has its own transitive dependencies, IntelliJ IDEA displays them in both tool windows.
    maven dependency trans

Enable annotation processors

  1. Open your POM file.

  2. Specify the annotationProcessors and annotationProcessorPaths options.

  3. Re-import your project. IntelliJ IDEA adds the appropriate paths to the Annotation processor settings located in Settings / Preferences | Build, Execution, Deployment | Compiler.

Centralize dependency information

In a multi-module Maven project, the dependency in the parent POM will be inherited by all sub projects. You can use dependencyManagement to consolidate and centralize the management of the dependencies' versions.

  1. Open your POM in the editor.

  2. Press Alt+Insert to open the Generate context menu.

  3. From the context menu, select the Managed Dependency option that will show you the list of the dependencies that are defined in the dependencyManagement section of your parent POM in a multi-module project.

    maven pom dependencyManagement

  4. Select the desired dependency and click OK.

    maven managed depen
    The dependency is added to the POM. You don't need to specify the version on the dependency it will be taken from the DependencyManagement.
    maven dependency no version
    However, if you want to overwrite the defined version, you need to include version when you add the managed dependency to the POM.
    maven version overwrite

Add a scope for the Maven dependency

You can add a scope for your dependency using POM. In this case IntelliJ IDEA will execute the dependency at the specified phase.

  1. In your POM, in the dependency description add scope and using the code completion add the name of the scope.

    maven scope

  2. Import your changes. The name of the scope is displayed in the Maven Projects tool window. In the Project Structure dialog, on the Modules page you can see that the scope of the dependency is also displayed.

    maven project structure scope
    Note that changing dependency's scope in the Project Structure dialog will not affect the pom.xml file.

You can also add a custom jar file as a dependency using the Maven scope system when you define your dependency. However, note that this dependency will only be available on your machine and you can use it only for the local deployment.

Work with Maven transitive dependencies

IntelliJ IDEA lets you view transitive dependencies that were pulled in with the added or importded Maven dependency. You can check their versions, chanage them, or exclude those dependencies altogether.

The Maven Projects tool window displays the direct dependency and all its transitive dependencies that were pulled in.

  1. In your project's POM, press Ctrl and hover the mouse over the dependency.

    maven see depen pom

  2. Click the dependency to open the dependency POM.

  3. In the dependency POM, view the active dependency, its transitive dependencies and their versions.

    maven depen pom
    You can check the origin from which the dependency was pulled in.
    maven depen origin

You can exclude a transitive dependency if you want.

  1. Open the dependency POM and find the transitive dependency you want to exclude. Copy groupId and artifactId.

  2. In your project POM, underneath your active dependency, enter exclusions and using code completion paste the copied info of the dependency you want to exclude.

    maven depen exclusion
    The dependency is also excluded from the Project and Maven Projects tool windows.

Import Maven dependencies

IntelliJ IDEA lets you import dependencies to your Maven project. When IntelliJ IDEA imports added dependencies, it should parse the dependencies and set up the project automatically.

  • In the Maven Projects tool window, click icons general settings svg icon to open the Maven | Importing settings and select the Import Maven projects automatically checkbox. Also, ensure that the JDK for importer matches the JDK version you are trying to use.
    In this case the dependencies are updated automatically every time you change the POM.

  • Press reimport icon in the Maven Projects tool window. In this case you manually trigger the re-import process and the update of dependencies.

If for some reason the dependencies weren't imported correctly, try to perform the following actions:

  • You can try to update Maven indices by updating the remote repository in the Maven | Repositories settings.

    maven remote repo

  • You can check your local maven repository in the Maven | Repositories settings and try to updated it.

  • You can check the jar file of the local .m2 repository to see if it was downloaded correctly.

  • You can check the effective POM to determine which Maven repository was used as an origin of the dependency.

  • You can select the Always update snapshots option in Maven settings. In this case IntelliJ IDEA checks the latest version of the downloaded dependency and updates it accordingly.

    maven update snapshots

View Maven dependencies as a diagram

IntelliJ IDEA lets you view and work with Maven dependencies in a diagram format.

  1. In the Maven Projects tool window, right-click the desired sub project and choose Show Dependencies. Show Dependencies Popup.

    maven show depen diagram

  2. In the diagram window, IntelliJ IDEA displays the sub project and all its dependencies including the transitive ones.

    maven diagram

You can perform different actions while in the diagram window.

Change the visibility level

You can change the visibility level and, for example, view dependencies that have a specific scope (compile, test, and so on).

  1. In the diagram window, select the sub project and click level visibility icon icon.

  2. From the list, select the dependency scope you want to see. IntelliJ IDEA displays only the specified dependency scope.

    maven specific scope diagram

Navigate to POM

You can easily navigate to POM from the diagram window.

  • Select the desired node, and press F4, or choose Jump to Source on its context menu. The corresponding file opens in the editor.

You can check conflicts and duplicates by clicking icon in the diagram window.

Check conflicts and duplicates

  • In the diagram window, click the Conflicts and Duplicates icon.

Show conflicts and duplicates

Show path from the selection to the root

You can select a dependency and see how it is connected to the root.

  1. In the diagram window, select a dependency for which you want to see a connection to a project.

  2. Click the Show Path Root Selection.

Show neighbors of the selected node

You can select a dependency and see what other dependencies are connected to the selected node. It might be helpful if you have a large diagram and want to focus on just a part of it.

  1. In the diagram window, select a dependency you need.

  2. Click the Show Neighbors of Selected Nodes.

Exclude a dependency from a diagram

You can exclude a dependency from the diagram.

  1. Select a dependency in the diagram window.

    Exclude diagrams

  2. From the context menu, choose Exclude.

  3. From the list, select the module (if any) where the exclusion definition will be added. The selected dependencies will be removed from diagram, and the exclusion section will be added to the corresponding dependency in the module's POM.

    maven pom exclusion

Last modified: 13 May 2019