简体   繁体   English

如何让 IntelliJ IDEA 从 Maven 更新我的依赖项?

[英]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.当我在项目的pom.xml中手动添加依赖项时,让Maven下载依赖项并让IntelliJ构建模块, IntelliJ抱怨缺少库。 At the same time Maven can find the dependent JARs and build the project.同时Maven可以找到依赖的JARs并构建工程。

How can I tell IntelliJ to use the libs which are downloaded by Maven ?如何告诉IntelliJ使用Maven下载的库?

It turns out IntelliJ does not pick up added dependencies from the local Maven repository.事实证明,IntelliJ 不会从本地 Maven 存储库中获取添加的依赖项。 We have to tell IntelliJ to reimport the pom.xml.我们必须告诉 IntelliJ 重新导入 pom.xml。

  1. Open the project view in IntelliJ在 IntelliJ 中打开项目视图
  2. Right click the pom.xml file and select Maven - Reimport右键单击 pom.xml 文件并选择 Maven - Reimport
  3. If this works for you IntelliJ will add the dependencies to the project如果这对您有用,IntelliJ 会将依赖项添加到项目中
  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.在设置 -> Maven -> 自动导入 Maven 项目中更改此设置。

IntelliJ IDEA 2016 IntelliJ IDEA 2016

Import Maven projects automatically自动导入 Maven 项目

Approach 1方法一

  • File > Settings... > Build, Execution, Deployment > Build Tools > Maven > Importing > check Import Maven projects automatically文件>设置... >构建、执行、部署>构建工具> Maven >导入> 勾选自动导入 Maven 项目

    自动导入 Maven 项目

Approach 2方法二

  • press Ctrl + Shift + A > type "Import Maven" > choose "Import Maven projects automatically" and press Enter > check Import Maven projects automatically按下Ctrl + Shift + A>键入“导入的Maven”>选择“自动导入Maven项目”并回车>查看导入的Maven自动项目

Reimport重新导入

Approach 1方法一

  • In Project view, right click on your project folder > Maven > Reimport在项目视图中,右键单击您的项目文件夹 > Maven > Reimport

Approach 2方法二

  • View > Tools Windows > Maven Projects :查看>工具窗口> Maven 项目

    • right click on your project > Reimport右键单击您的项目 > 重新导入

    or或者

    • click on the "Reimport All Maven Projects" icon:单击“重新导入所有 Maven 项目”图标:

      重新导入所有 Maven 项目

Uncheck取消选中

"Work Offline" “离线办公”

in Settings -> Maven !在设置-> Maven 中! It worked for me !它对我有用! :D :D

You need to go to: Maven settings -> Auto-Reload Settings你需要去:Maven settings -> Auto-Reload Settings

自动重新加载设置

Then check "Any Changes":然后检查“任何更改”:

在此处输入图片说明

File>Settings>Build,Execution,Deployment>Maven> Check : Always update snapshot文件>设置>构建、执行、部署>Maven>检查:始终更新快照

That worked for me.那对我有用。

For some reason IntelliJ (at least in version 2019.1.2) ignores dependencies in local .m2 directory.出于某种原因,IntelliJ(至少在 2019.1.2 版本中)忽略了本地.m2目录中的依赖项。 None of above solutions worked for me.以上解决方案都不适合我。 The only thing finally forced IntelliJ to discover local dependencies was:最终迫使 IntelliJ 发现本地依赖项的唯一事情是:

  1. Close project关闭项目
  2. Open project clicking on pom.xml (not on a project directory)单击pom.xml打开项目(不是在项目目录上)
  3. Click Open as Project单击Open as Project

项目清单

  1. Click Delete Existing Project and Import单击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 .IntelliJ 2020pom.xml视图中,应该能够通过以下组合键应用 pom 更改: CTRG + SHIFT + O

And as correctly commented before - IntelliJ additionally shows a balloon widget to import changes.正如之前正确评论的那样 - IntelliJ 还显示了一个气球小部件来导入更改。

除了检查“自动导入 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.我尽一切努力让 IntelliJ 获取我的pom.xml更改,但它只是没有这样做。 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.我注释掉了pom.xml所有<dependencies>并重建了应该显示数百个编译错误但没有显示的项目。

In the end, I had to delete the .idea folder to get IntelliJ to regenerate it's maven model.最后,我不得不删除.idea文件夹才能让 IntelliJ 重新生成它的 maven 模型。 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.执行此操作后,当我执行“重新加载所有项目”时,会接收对pom.xml后续更改,因此我怀疑 IntelliJ 使用缓存模型而不是在进行更改时更新它的错误。

Here's what I did:这是我所做的:

  1. Close the project in IntelliJ (file -> close project)在 IntelliJ 中关闭项目(文件 -> 关闭项目)
  2. Delete the .idea folder in the root folder of the project删除项目根目录下的.idea文件夹
  3. Open the project again in IntelliJ在 IntelliJ 中再次打开项目
  4. Maven -> Reload All Projects Maven -> 重新加载所有项目
  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.今天,当您在pom.xml文件中单击鼠标右键时, Maven选项卡下有一个Reload Project选项。

重新加载 Maven 依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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