Import Maven dependencies in IntelliJ IDEA

2019-01-03 08:18发布

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 ?!

27条回答
虎瘦雄心在
2楼-- · 2019-01-03 08:35

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.

查看更多
干净又极端
3楼-- · 2019-01-03 08:35

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.

查看更多
等我变得足够好
4楼-- · 2019-01-03 08:35

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.

查看更多
5楼-- · 2019-01-03 08:36

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

查看更多
冷血范
6楼-- · 2019-01-03 08:36

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

查看更多
一纸荒年 Trace。
7楼-- · 2019-01-03 08:38

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.

查看更多
登录 后发表回答