Import Maven dependencies in IntelliJ IDEA

577,770

Solution 1

IntelliJ should download and add all your dependencies to the project's classpath automatically as long as your POM is compliant and all the dependencies are available.

When importing Maven projects into IntelliJ an information box usually comes up asking you if you want to configure Auto-Import for Maven projects. That means that if you make any changes to your POM those changes will be loaded automatically.

You can enable such feature going to File > Settings > Maven > Importing, there is a checkbox that says "Import Maven projects automatically".

If that doesn't help, then I would suggest to make a full clean-up and start again:

  • Close your project window (and IntelliJ) and remove all *.iml files and all .idea folders (there should be one per module)
  • Run mvn clean install from the command line
  • Re-import the project into IntelliJ and pay attention when it asks you to enable auto-import

IntelliJ 2016 Update:

The Import Maven Projects automatically setting has been moved to Build, Execution, Deployment > Build Tools > Maven > Importing in your IntelliJ preferences.

Solution 2

Fix before IntelliJ 14

File [menu] -> Settings -> maven -> importing and uncheck "use maven3 to import project"

ref: http://youtrack.jetbrains.com/issue/IDEA-98425 (which may have a few other ideas too)

Fix IntelliJ 15+

Ran into this again, with IntelliJ 15 this time, which has no "use maven3 to import" option available anymore. The cause was that sometimes IntelliJ "doesn't parse maven dependencies right" and if it can't parse one of them right, it gives up on all of them, apparently. You can tell if this is the case by opening the maven projects tool window (View menu -> Tool Windows -> Maven Projects). Then expand one of your maven projects and its dependencies. If the dependencies are all underlined in red, "Houston, we have a problem". enter image description here

You can actually see the real failure by mousing over the project name itself.

enter image description here

In my instance it said "Problems: No versions available for XXX" or "Failed to read descriptor for artifact org.xy.z" ref: https://youtrack.jetbrains.com/issue/IDEA-128846 and https://youtrack.jetbrains.com/issue/IDEA-152555

It seems in this case I was dealing with a jar that didn't have an associated pom file (in our maven nexus repo, and also my local repository). If this is also your problem, "urrent work around: if you do not actually need to use classes from that jar in your own code (for instance a transitive maven dependency only), you can actually get away with commenting it out from the pom (temporarily), maven project reload, and then uncomment it. Somehow after that point IntelliJ "remembers" its old working dependencies. Adding a maven transitive exclude temporarily might also do it, if you're running into it from transitive chain of dependencies."

Another thing that might help is to use a "newer version" of maven than the bundled 3.0.5. In order to set it up to use this as the default, close all your intellij windows, then open preferences -> build, execution and deployment -> build tools -> maven, and change the maven home directory, it should say "For default project" at the top when you adjust this, though you can adjust it for a particular project as well, as long as you "re import" after adjusting it.

Clear Caches

Deleting your intellij cache folders (windows: HOMEPATH/.{IntellijIdea,IdeaC}XXX linux ~/.IdeaIC15) and/or uninstalling and reinstalling IntelliJ itself. This can also be done by going to File [menu] -> Invalidate Caches / Restart.... Click invalidate and restart. This will reindex your whole project and solve many hard-to-trace issues with IntelliJ.

Solution 3

When importing the project, select pom.xml instead of the project directory. It should work.

Solution 4

Try to Re-Import the project from the Maven Projects panel in IntelliJ IDEA. It should download and configure all the dependencies defined in your pom.xml automatically.

If download doesn't work from IDEA for some reason, try mvn install from the command line and see if the dependencies can be fetched.

Of course all the required dependencies and any custom repositories must be defined directly in the pom.xml file.

Solution 5

For IntelliJ 2016-2.4 (and I believe other new-ish versions):

View > Tool Windows > Maven Projects

In the newly revealed toolbar, select Maven settings (icon of a toolset).

When this screen opens, expand the Maven menu and click 'Importing'

Here, click "Import Maven projects automatically." Also ensure that the 'JDK for Importer' option matches the JDK version you mean to use.

Click OK. Now go to the red dependency in your pom.xml, select the red lightbulb, and click 'Update Maven indices'.

Share:
577,770

Related videos on Youtube

Zaur Guliyev
Author by

Zaur Guliyev

Backend software engineer @trivago.

Updated on July 08, 2022

Comments

  • Zaur Guliyev
    Zaur Guliyev almost 2 years

    I have a small question about IntelliJ IDEA 11. I just imported a project from subversion - its a maven project. But I have a problem in maven library dependencies so that I can't include all maven dependencies automatically - IDEA shows dependency errors only when I open that class/ Thats what I get here:

    enter image description here

    So I want all dependencies to be added automatically - is that possible or do I have to go through all class files to identify and add maven dependencies?!

    UPDATE: After doing some modifications I found how to resolve my problem in some way. Thats what I did: enter image description here

    but I think logically it will not include and check new dependencies ahead?!... Is there any settings area for this in intelliJ - auto export dependencies to classpath ?!

    • Alonso Dominguez
      Alonso Dominguez almost 12 years
      what do you mean by "automatically"? You must declare all your dependencies in your POM as with any normal Maven project. IntelliJ or other IDE will just download them if they are declared there...
    • Zaur Guliyev
      Zaur Guliyev almost 12 years
      It downloads all dependencies but it doesn't seem to add all of them to classpath...
    • Zaur Guliyev
      Zaur Guliyev almost 12 years
      and another problem is that intelliJ shows me red underlined errors only when I open that java file...
    • RCross
      RCross over 11 years
      One instance where you get this red line is if you have a SNAPSHOT dependency with a classifier, where a newer version of the dependency without the classifier exists. This is perfectly legal in Maven, but IntelliJ gets confused trying to work out the path to the physical file in your local Maven repository.
    • Kevin Pauli
      Kevin Pauli over 8 years
      FYI this can still happen in 15.0.2. Just happened to me. Had to blow away my .iml file and .idea folder and reimport. Incredibly frustrating b/c I spent 2 hours fighting it, and ultimately lost all my carefully crafted run configs.
    • Zaur Guliyev
      Zaur Guliyev over 8 years
      @KevinPauli no need to remove IDEA project files for that, you could just "Reimport All Maven Projects" under "Maven Projects" tab for manual re-import.
    • DaddyMoe
      DaddyMoe over 7 years
      Most times backup and blowing away your user .m2 folder or drill in offending packages and delete/rename and doing a mvn clean install does the trick.
    • Pierre Thibault
      Pierre Thibault about 7 years
      @KevinPauli I had to live the same.
    • Pierre Thibault
      Pierre Thibault about 7 years
      At the end it was an error in a parent pom.
    • vikramvi
      vikramvi over 5 years
      I love Eclipse way of downloading maven dependancies, right click on pom.xml and force update. I wonder why IDEA has made is difficult
    • LiNKeR
      LiNKeR over 4 years
      Had the same problem after cloning the deeplearning4j repo from github, my work around was to close intellij -> delete the entire .idea folder -> re-open the project -> Build -> Build Project
  • Alonso Dominguez
    Alonso Dominguez almost 12 years
    so your solution was to mark the dependencies as exported? that's weird! I use IntelliJ and Maven a lot and none of my projects (some of them are multi-projects) has the export feature enabled... is that Community Edition or Ultimate?
  • Zaur Guliyev
    Zaur Guliyev almost 12 years
    Its an Ultimate Edition but the project that I have imported was built on Eclipse - so that .classpath was enabled there but IntelliJ uses .iml - So I think its logical to include these libs for the first time if this project did not have .iml file before.. ?!
  • Alonso Dominguez
    Alonso Dominguez almost 12 years
    oh, ok - did you tried to import it as a Maven project rather than an Eclipse one? I'm used to not check in IDE related files into my source repositories when working with Maven projects: if it's a Maven project, then Maven rules
  • Zaur Guliyev
    Zaur Guliyev almost 12 years
    Oh that made sense :) imported them as Maven.. Thanks Alonso!
  • Martin Andersson
    Martin Andersson over 11 years
    I already had this setting, but for some reason a couple of POM files were ignored under Ignored Files. Thank you for pointing me in the right direction!
  • kaqqao
    kaqqao over 11 years
    Agreed. It's almost certain the OP simply wrongly imported the project.
  • janhink
    janhink over 9 years
    Strangely enough almost every other post advises to check the "Use Maven3 to import project" option to solve this issue while actually unchecking it solved the issue for me. Why? I don't know. Thanks!
  • AlikElzin-kilaka
    AlikElzin-kilaka about 9 years
    In v14 ultimate, I chose an external maven and everything got back to working.
  • JerseyGood
    JerseyGood almost 9 years
    @AlikElzin-kilaka Changing the maven home directory to external maven home does help a lot . After I updated IDEA to v14 ultimate, I could not launch my project in IDEA due to "No tomcat6 plugin found", but I can launch project using mvn command, and changing maven home directory to external one solved it
  • artronics
    artronics over 8 years
    You can enable Auto import in preferences/setting. Open preferences/setting and search for "Import maven projects automatically". it is under Build,Execution,Deployment>Build Tools>Maven>Importing
  • Wheeler
    Wheeler almost 8 years
    In IntelliJ 2016.2.1, disabling the auto-import changing the Maven home directory to the version our stack is using (3.3.9) fixed the problem for me.
  • inanutshellus
    inanutshellus over 7 years
    @Wheeler Thanks! Your comment led me to my problem. IntelliJ keeps resetting to the bundled maven (3.0.5). Switching back to Maven 3.3.9 let me see the error tool tip in Roger's post.
  • morpheus
    morpheus over 7 years
    You can also download the jar manually and install it using mvn install:install-file then reimport to see if that fixes the could not find artifact descriptor problem
  • Justin Blank
    Justin Blank over 7 years
    As far as I can tell, none of the settings mentioned in this answer/comments are available in IntelliJ 2016 community edition.
  • Alonso Dominguez
    Alonso Dominguez over 7 years
    @JustinBlank not surprised by that, given that this is an answer from 2012. The settings are still there, they just have been moved somewhere else (answer has been updated).
  • Vincent F
    Vincent F almost 7 years
    picking the right JDK for importer helped me. I also had to run mvn idea:clean idea:idea to close the last issues.
  • vikramvi
    vikramvi over 6 years
    Bug still remains in 11.2017, mvn clean install fixed the bug. Eclipse never had these straight forward bugs
  • vikramvi
    vikramvi over 5 years
    sometime click on "Reimport all maven projects" option under Maven Projects window
  • Kabeer Ahmed
    Kabeer Ahmed over 5 years
    For a windows based installation, in addition to mvn clean; I had to manually re-point maven plugin to the maven installation folder. This is achievable by selecting maven window in intellij and selecting options. After every new dependency entry into pom, I am required to do this.
  • Marcelo Xavier
    Marcelo Xavier about 5 years
    Changing the maven home directory from Bundle (Maven 3) to the path where my maven is installed .../maven/3.6.1/libexec worked for me
  • matanster
    matanster almost 5 years
    Perhaps this is the most elegant, clean, solution (and it's slightly awkward intellij doesn't help you in guessing that)
  • Steve T
    Steve T over 4 years
    This worked for me, in the settings.xml file, the entry for the <url> tag nested in the <mirror> tag was outdated. I conferred with my colleagues to get the updated entry value. I made the change to ~/.m2/settings.xml, closed intelliJ and started it again. Upon restart, doing nothing else, the red underscores showing up in the maven view were gone. Strangely, most of the root dependencies that were highlighted were always present in the "External Libraries" entry of the Project view, both before and after I performed this fix.
  • asymmetryFan
    asymmetryFan about 4 years
    Working for me as well, with IntelliJ Community Edition 2019.3.4. Thank you for such an easy solution. :)
  • greenhorn
    greenhorn over 3 years
    This was really helpful. Thanks a lot @AlonsoDominguez
  • ankit deora
    ankit deora about 3 years
    Worked like a charm, That is what I was looking for :)
  • Mosa
    Mosa over 2 years
    Invalidate Caches and restart is solved my problem thank you.
  • learningIsFun
    learningIsFun about 2 years
    Yes. Same happened with me. I renamed the settings.xml file while doing personal project using same computer. But need to remember to rename settings.xml to original name for internal work related projects.