235

I just imported a project from subversion to IntelliJ IDEA 11 - it's 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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Zaur Guliyev
  • 4,254
  • 7
  • 28
  • 44
  • 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... – Alonso Dominguez Jul 12 '12 at 15:10
  • 9
    It downloads all dependencies but it doesn't seem to add all of them to classpath... – Zaur Guliyev Jul 13 '12 at 07:59
  • 3
    and another problem is that intelliJ shows me red underlined errors only when I open that java file... – Zaur Guliyev Jul 13 '12 at 08:00
  • 4
    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. – RCross Nov 22 '12 at 16:12
  • 4
    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. – Kevin Pauli Dec 10 '15 at 17:25
  • 3
    @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. – Zaur Guliyev Dec 23 '15 at 14:04
  • 1
    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. – DaddyMoe Dec 10 '16 at 13:51
  • At the end it was an error in a parent pom. – Pierre Thibault May 05 '17 at 22:44
  • 1
    I love Eclipse way of downloading maven dependancies, right click on pom.xml and force update. I wonder why IDEA has made is difficult – vikramvi Aug 31 '18 at 15:16
  • 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 – linker Oct 26 '19 at 11:29
  • **right click on the project/module > Maven (at bottom) > Reload Project** – Amandeep Jul 21 '22 at 04:35

39 Answers39

319

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.

Alonso Dominguez
  • 7,750
  • 1
  • 27
  • 37
  • 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? – Alonso Dominguez Jul 13 '12 at 11:24
  • 1
    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.. ?! – Zaur Guliyev Jul 13 '12 at 11:40
  • 1
    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 – Alonso Dominguez Jul 13 '12 at 11:58
  • Oh that made sense :) imported them as Maven.. Thanks Alonso! – Zaur Guliyev Jul 13 '12 at 13:08
  • 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! – Martin Andersson Feb 03 '13 at 16:04
  • 2
    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 – artronics Jan 30 '16 at 02:15
  • As far as I can tell, none of the settings mentioned in this answer/comments are available in IntelliJ 2016 community edition. – Justin Blank Nov 26 '16 at 01:04
  • 3
    @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). – Alonso Dominguez Feb 03 '17 at 10:48
  • Bug still remains in 11.2017, mvn clean install fixed the bug. Eclipse never had these straight forward bugs – vikramvi Nov 09 '17 at 13:04
  • 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. – Kabeer Ahmed Jan 24 '19 at 13:28
  • This was really helpful. Thanks a lot @AlonsoDominguez – greenhorn Aug 28 '20 at 06:15
  • cleaning the *.iml and .idea and then run the command works for me, thank you. – norullah karimi Sep 02 '23 at 15:56
76

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.

technogeek1995
  • 3,185
  • 2
  • 31
  • 52
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
  • 2
    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! – janhink Nov 28 '14 at 14:51
  • 4
    In v14 ultimate, I chose an external maven and everything got back to working. – AlikElzin-kilaka Apr 12 '15 at 10:12
  • @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 – JerseyGood Jun 24 '15 at 08:58
  • 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. – Wheeler Aug 08 '16 at 21:41
  • @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. – inanutshellus Aug 30 '16 at 13:02
  • 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 – morpheus Oct 02 '16 at 19:46
  • 1
    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 – Marcelo Xavier May 06 '19 at 14:30
42

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

chichi
  • 421
  • 4
  • 2
30

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.

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
22

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'.

Paul Back
  • 1,269
  • 16
  • 23
12

What helped me:

Navigage: Settings | Build, Execution, Deployment | Maven

Specify "Maven home directory" - the place you installed the maven

Anatolii Stepaniuk
  • 2,585
  • 1
  • 18
  • 24
9

If certain maven modules are not compiling check if their pom.xml is on the "ignored files" list. In IntelliJ goto

preferences -> project settings -> maven -> ignored files

and check if the related pom.xml is ignored.

Udo
  • 2,300
  • 1
  • 23
  • 27
8

I had the same issue and tried all the answers mentioned here, none worked.

The simple solution is go to your project folder and delete all the .idea and .iml files and restart the IntelliJ ide. It works. No need to do anything with the settings.

Yashaswi N P
  • 896
  • 8
  • 15
7

Open IntelliJ Idea, Go to File > Other Settings > Default Settings... > Maven (Preferences) > Importing or| Preferences > Maven > Importing

enter image description here

Click on Enable Auto-import in the console.

enter image description here

Prashanth Sams
  • 19,677
  • 20
  • 102
  • 125
5

When you create a new project you simply need to choose the option:

...
Import project from external model
Create IDEA project structure over existing external model (Eclipse, Maven...)
...

You'll find it pretty straight forward from there.
And in your case you can close your project and simply create a new one. Choose your project's directory, which will override it, making it a Maven project.

Markus Coetzee
  • 3,384
  • 1
  • 29
  • 26
5

Importing Maven dependencies may not work if you import the same path several times. This may happen automatically after importing an existing maven project. I can't figure out why this happens, so I'm inclined to think it is a bug.

For example, if my project is

hibernate
  src/main
  src/test

the three paths may be imported as top nodes:

hibernate
src/main
src/test

If such is the case, the dependencies appear to be right, but they won't be used until you remove the superfluous paths (in this case, src/main and src/test). Once you do this, refresh and click Build > Rebuild Project. IDEA will pick up the dependencies.

A symptom of this problem is that IDEA warns you of a duplicated path when you manually check a library (second screenshot in the question).

There are several ways to re-read the dependencies in case you need it. If you go to the “Maven Projects” tab, there is a “Reimport All Maven Projects” icon at the top left of the tab. If you suspect IDEA became confused you can click on File > Invalidate Caches.

Jano
  • 62,815
  • 21
  • 164
  • 192
5

First check path Specified for User Settings file: in Settings -> Build,Execution,Development -> Build Tools -> Maven . The field should have path of the settings.xml of your maven. Also the settings.xml should have correct path of remote repository.

amol13
  • 362
  • 4
  • 11
  • 1
    This worked for me, in the settings.xml file, the entry for the tag nested in the 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. – Steve T Jan 30 '20 at 21:04
4

Go into your project structure, under project Settings, Modules, select the dependencies table. For each dependency, change the scope from 'Test' to 'Compile'.

Bud
  • 49
  • 1
4

If in the lower right corner it says "2 processes running..." or similar, you may just need to wait for that to finish, since it may take time to download all the jars.

djechlin
  • 59,258
  • 35
  • 162
  • 290
4

Reimport the project. If you install maven plugin you can use this.

Right click on the project -> Maven -> Reimport

Damith Ganegoda
  • 4,100
  • 6
  • 37
  • 46
4

I had a similar issue, in my case I am using a custom settings.xml which was not picked from IntelliJ.

Solution:
File > Settings > Build, Execution, Deployment > Maven: User settings file (chose here my custom settings.xml).

petre
  • 508
  • 7
  • 9
4

IntelliJ 2020.3.3

This one did it for me.

Build, Execution, Deployment > Build Tools.

Change "Reload project after changes in the build scripts":

  • from default "External Changes" to "Any Changes"

enter image description here

With this, it took a while for the project to rebuild but now I can see Maven dependencies in IntelliJ:

enter image description here

xmar
  • 1,729
  • 20
  • 48
3

I ran into the problem that some subdependencies couldn't be resolved in IntelliJ 2016.3.X. This could be fixed by changing the Maven home directory in Settings > Build, Execution, Deployment > Build Tools > Maven from Bundled (Maven 3) to /usr/share/maven.

After that all subdependencies got resolved as in previous IntelliJ versions.

Johannes Stadler
  • 737
  • 12
  • 24
3

In my case, path for JAVA used by MAVEN was not set as JAVA_HOME as configured on the machine. Hence, it was not even trying to download the dependencies. Steps I followed which solved this issue,

  1. Checked for JAVA path , Settings >> Build >> Build Tools >> Importing >> JDK for importer. Pointed it to JAVA_HOME.

  2. mvn clean install -U

    Used above command to force the dependencies download.

  3. Re imported Maven Projects

Community
  • 1
  • 1
3

So I was also facing this problem in lots of my organisation's codebase. On addition to the answers suggested above, we can tweak around with the allocation pool memory of the jvm. This is because, the volume of the dependencies brought in large codebases may be overwhelming for the jvm for IDE to build the project completely. Hence tried increasing XMX value and it worked! Added -Xmx3072 to resolve the issue of "not building" my Java project in IntelliJ

enter image description here

2

Maven - Reimport did not work for me. I have Spring project in STS(Eclipse) and my solution is to import project to IDEA like so:

1) File - New - Project from Existing Sources... - select directory - choose Eclipse.
2) Set Maven autoimport to true in settings.
3) Then right click in pom.xml and choose Add as Maven Project.

After this it has imported everything.

Svitlana Onish
  • 284
  • 3
  • 14
2

I had the similar issue with my macbook, just did a small change in pom.xml and it started downloading all dependencies:

Earlier dependencies were written as below for my windows machine:

<dependencies>
 <dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4</version>
</dependency>
</dependencies>

I just removed the <dependencies> and </dependencies> tags and it started downloading all the dependencies:

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.4</version>
</dependency>

I am not sure it will work for you or not.. but worked fine for me.

Thanks

pretzelhammer
  • 13,874
  • 15
  • 47
  • 98
Amit Joshi
  • 57
  • 3
2

Deleting the .idea folder from the project directory, and then re-importing the project as a Maven project is what worked for me.

Janac Meena
  • 3,203
  • 35
  • 32
2

Invalidate Caches worked for me:

File > Invalidate Caches

Checks:

Clear file system cache and Local History
Clear downloaded shared indexes

Then Invalidate and Restart

nhaht
  • 1,005
  • 12
  • 21
1

I solved this issue by updating my settings.xml file with correct mirror config, seems that intellij will try to download meta-data from repository every time the maven module imported.

Abel Zhan
  • 11
  • 1
1

Hijacking a bit to add what ended up working for me:

Go into the Maven Projects sidebar on the right edge of the IDE, and verify that your dependencies are listed correctly under your module there. Assuming they are, just ask IDEA to reimport then (the first button at the top, looks like two blue arrows forming a counter-clockwise circle).

Once I did that, and let IDEA reload the project for me, all my dependencies were magically understood.

For reference: this was with IDEA 13.1.2

Sunspar
  • 56
  • 5
1

I faced the same problem and tried everything suggested which did not solve the issue, I was using Intellij version 13.1.3

Finally after spending more than couple of hours trying to fix it, I decided to try an upgraded version and opened the project in version 14.1.4 which ultimately resolved the issue. I would think this as a probable bug in the previous version.

I hope this helps!

sjaiswal
  • 498
  • 6
  • 8
1

I was able to resolve it by removing unnecessary modules from Project Settings -> Modules list.

enter image description here

It turns out these additional modules were created automatically by IntelliJ IDEA when I created the IntelliJ project by importing from the project folder (instead of by opening pom.xml). Then, after declaring the project as Maven project, proper modules were created, and existing modules were conflicting with them. It is also possible to exclude those modules during project creation.

elyor
  • 998
  • 9
  • 20
1

In my case, one of the modules was ignored for some reason. To resolve that: Right click the problematic module and choose "Unignore Projects".

unignore project example

elirandav
  • 1,913
  • 19
  • 27
1

You might be working under a company's internal network.

If so, to download or add external Maven dependencies your settings.xml file under user/<username>/.m2 folder might need to be updated.

Contact your administrator to provide the right settings.xml file and then paste it into you .m2 folder.

Shahbaz A.
  • 4,047
  • 4
  • 34
  • 55
  • 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. – learningIsFun Feb 16 '22 at 05:22
1

re-import-maven -> close-intellij -> delete-the-entire -.idea-folder -> re-open-the-project -> build -> build-project/build-module-'your parent module'

Dharman
  • 30,962
  • 25
  • 85
  • 135
linker
  • 821
  • 1
  • 8
  • 20
0

In maven the dependencies got included for me when I removed the dependencyManagement xml section and just had dependencies directly under project section

seahorse
  • 2,420
  • 4
  • 31
  • 40
0

If everything else fails, check if the jar file in your local .m2 repository is indeed valid and not corrupted. In my case, the file had not been fully downloaded.

Markus Barthlen
  • 389
  • 4
  • 15
0

The problem appears to be that despite listing your dependencies in the pom.xml, IntelliJ IDEA does not rebuild those dependencies when you run your project.

What worked for me is this:

Go to 'Run' -> 'Edit Configurations...', find your application, make sure the "Before launch:" section is expanded, click the green plus sign, and select "Build Project".

enter image description here

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142
0

i was able to fix mine by adding this lines of code after the build tag in the pom.xml file, i compared it from my running project and discovered that was the difference, now i'm all good.

<repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
0
  1. From maven tab click + and choose pom.xml
  2. From maven tab click download sources and documentation
  3. On project structure(where you can view project files/directories) right click the project you're trying to build and choose Build Module Project Name.
  4. From tab Run- Edit Configurations with + add Application and fill the below fields: i. Main Class- Manually choose from Project tab select the main class ii. Use classpath of module - choose the application name iii. Shorten command line - classpath file
  5. Now simply run the app.
0

A possible pattern for this issue is that you dont have connection to Nexus and basically you want to build the project in offline mode. But when you import the project Idea tries to download Maven dependencies automatically.. and fails. You set Work Offline checkbox in Settings, but its too late: something was already broken during initial download attempt. Neither of the options listed here worked for me to resolve this. Instead I did the following:

  1. first created empty Java (not Maven) project
  2. checked Work Offline option in Maven settings
  3. added my project as a Maven module to this new project

This way it worked.

MiamiBeach
  • 3,261
  • 6
  • 28
  • 54
0

The solution for this: -

  1. go to file -> settings -> maven -> maven home path
  2. there you will find bundled (maven 3) something like that select the appropriate home path of maven where the maven file is present in system (the path given in the environment variable)
  3. click on apply
  4. refresh the project.
0

I fixed the problem in intellij IDEA 2022 by adding "requires package_name" in module-info.java file in the project structure folder. For example requires AnimateFX;

HB FL3X
  • 1
  • 2