简体   繁体   中英

How can I make IntelliJ IDEA update my dependencies from Maven?

When I manually add dependencies in the pom.xml of my project, let Maven download the dependencies and let IntelliJ build the module, IntelliJ complains about missing libraries. At the same time Maven can find the dependent JARs and build the project.

How can I tell IntelliJ to use the libs which are downloaded by Maven ?

It turns out IntelliJ does not pick up added dependencies from the local Maven repository. We have to tell IntelliJ to reimport the pom.xml.

  1. Open the project view in IntelliJ
  2. Right click the pom.xml file and select Maven - Reimport
  3. If this works for you IntelliJ will add the dependencies to the project
  4. Check the if the dependencies you need are added in
    • File - Project Structure - Project Settings - Libraries
    • and File - Project Structure - Modules - Dependencies

You don't have to reimport manually each time. You can enable auto-import as documented here. Change this in Settings -> Maven -> Import Maven projects automatically.

IntelliJ IDEA 2016

Import Maven projects automatically

Approach 1

  • File > Settings... > Build, Execution, Deployment > Build Tools > Maven > Importing > check Import Maven projects automatically

    自动导入 Maven 项目

Approach 2

  • press Ctrl + Shift + A > type "Import Maven" > choose "Import Maven projects automatically" and press Enter > check Import Maven projects automatically

Reimport

Approach 1

  • In Project view, right click on your project folder > Maven > Reimport

Approach 2

  • View > Tools Windows > Maven Projects :

    • right click on your project > Reimport

    or

    • click on the "Reimport All Maven Projects" icon:

      重新导入所有 Maven 项目

Uncheck

"Work Offline"

in Settings -> Maven ! It worked for me ! :D

You need to go to: Maven settings -> Auto-Reload Settings

自动重新加载设置

Then check "Any Changes":

在此处输入图片说明

File>Settings>Build,Execution,Deployment>Maven> Check : Always update snapshot

That worked for me.

For some reason IntelliJ (at least in version 2019.1.2) ignores dependencies in local .m2 directory. None of above solutions worked for me. The only thing finally forced IntelliJ to discover local dependencies was:

  1. Close project
  2. Open project clicking on pom.xml (not on a project directory)
  3. Click Open as Project

项目清单

  1. Click Delete Existing Project and Import

在此处输入图片说明

in IntelliJ 2020 in the pom.xml view one should be able to apply pom changes by following key combination: CTRG + SHIFT + O .

And as correctly commented before - IntelliJ additionally shows a balloon widget to import changes.

除了检查“自动导入 Maven 项目”之外,请确保文件 > 设置 > Maven > 用户设置文件中的 settings.xml 文件存在,如果不存在,则覆盖并提供您的 settings.xml 文件路径。

I tried absolutely everything to get IntelliJ to pickup my pom.xml changes but it just wasn't doing it. I commented out all of the <dependencies> in pom.xml and rebuilt the project which should've shown hundreds of compile errors but didn't.

In the end, I had to delete the .idea folder to get IntelliJ to regenerate it's maven model. Once I did this, subsequent changes to pom.xml were picked up when I did a "Reload All Projects" so I suspect a bug where IntelliJ is using a cached model rather than updating it when changes are made.

Here's what I did:

  1. Close the project in IntelliJ (file -> close project)
  2. Delete the .idea folder in the root folder of the project
  3. Open the project again in IntelliJ
  4. Maven -> Reload All Projects
  5. Build -> Rebuild Project

Todays, there is a Reload Project option under the Maven tab when you do mouse right-click inside the pom.xml file.

重新加载 Maven 依赖项

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM