Adding Maven Dependencies to IntelliJ IDEA

I am facing an issue with Maven library dependencies. The problem is that I cannot include all the dependencies automatically, as IDEA shows dependency errors only when I open a specific class. Therefore, I am wondering if it is possible to add all dependencies automatically or if I have to manually identify and add Maven dependencies to each class file.

Question:

Regarding
IntelliJ IDEA
11, I have a minor inquiry. Recently, I brought in a project from subversion which happens to be a
maven project
. Unfortunately, I have encountered a difficulty with the maven library dependencies. As a result, I am unable to incorporate all the
maven dependencies
in an automated manner. The dependency errors are only visible when I access that specific class through IDEA. This is the issue that I am facing currently.

Is it feasible to add all the dependencies automatically or do I need to manually identify and include maven dependencies in each class file?

I was able to resolve my issue after making some modifications. The solution involved the following steps, as depicted in

.

I have doubts that new dependencies will be checked and included logically. Is there an option in IntelliJ’s settings to add
auto export
dependencies to the classpath?


Solution 1:

If your POM is compliant and all dependencies are available, IntelliJ will automatically download and include them in the project’s classpath.

Upon importing
importing Maven projects
into IntelliJ, an information box typically appears, prompting the user to configure the Auto-Import feature for
Maven projects
. This feature ensures that any changes made to the POM will be automatically loaded.

To activate this functionality, navigate to the Maven Importing section under File > Settings and check the box labeled “Import Maven
projects automatically
“.

If the initial suggestion proves ineffective, my recommendation would be to carry out a complete clean-up and commence once more.

  • Shut down
    project window
    as well as IntelliJ, then delete every

    *.iml

    file and

    .idea

    folder, with one folder per module.
  • Execute

    mvn clean install

    using the command prompt.
  • When you bring back the project to IntelliJ, be sure to enable auto-import when prompted.

The 2016 version of IntelliJ has received an update.

The option to automatically import Maven projects in IntelliJ preferences has been relocated to Build, Execution, Deployment > Build Tools > Maven > Importing.


Solution 2:

Fix before IntelliJ 14


Navigate to the File menu, then select Settings and go to Maven. From there, open the Importing tab and deselect the option that says “use
maven3
to import project”.

You may find some additional ideas by referring to http://youtrack.jetbrains.com/issue/IDEA-98425.

Fix IntelliJ 15+

While using IntelliJ 15, I encountered a problem where the “use maven3 to import” option was not available. This issue arises when IntelliJ fails to parse one of the maven dependencies correctly, causing it to give up parsing them altogether. To check if this is the case, you can access the
Maven Projects
tool window by selecting View menu -> Tool Windows ->
maven projects
. Then, expand your maven project and its dependencies. If you notice that the dependencies are underlined in red, it indicates that there is an issue that needs to be resolved.

By hovering over the project name, you can witness the actual failure.

In my case, the error message displayed either “Problems: No versions available for XXX” or “Failed to read descriptor for artifact org.xy.z”. These issues are documented on the following links:
https://youtrack.jetbrains.com/issue/IDEA-128846 and https://youtrack.jetbrains.com/issue/IDEA-152555.

In this instance, the jar I was working with did not have an associated
pom file
in either the
maven nexus
repository or my local repository. If you are also experiencing this issue, you can temporarily comment out the jar from the pom and reload the Maven project. If you only require the jar for a transitive
maven dependency
, this workaround should suffice. Alternatively, adding a
maven transitive
exclude temporarily may also resolve the problem if it arises from a transitive dependency chain.

One possible solution is to utilize a more recent version of Maven instead of the included 3.0.5. To make it the default, close all Intellij windows and navigate to Preferences ->
Build, Execution
and then Deployment -> Build Tools -> Maven. Adjust the Maven home directory, ensuring that “For default project” is displayed at the top. However, you can also modify it for specific projects, but be sure to “re-import” after making changes.

Clear Caches

To resolve difficult-to-track issues with IntelliJ, you can opt to uninstall and then reinstall the software or delete the
intellij cache
folders located in HOMEPATH/.{IntellijIdea,IdeaC}XXX for Windows or ~/.IdeaIC15 for Linux. Another option is to access the File [menu] and select

Invalidate Caches / Restart...

, followed by clicking on

invalidate and restart

to reindex the entire project, which can also help address these problems.



Solution 3:


When importing the project, opt for

pom.xml

in lieu of the project directory. This approach should be effective.


Solution 4:

Attempt to import the project once again from the Maven Projects panel in IntelliJ IDEA. This will automatically download and set up all dependencies mentioned in your

pom.xml

.

In case the download fails to work through IDEA, attempt executing

mvn install

via the command line to verify if the dependencies can be retrieved.

It is essential to specify both the necessary dependencies and any personalized repositories directly within the

pom.xml

document.

Frequently Asked Questions