【问题标题】:maven idea plugin unable to resolve local dependencymaven idea插件无法解析本地依赖
【发布时间】:2021-04-20 09:35:09
【问题描述】:

我创建了一个包含三个 Maven 子项目模块 1-3 的 Maven (Java) 项目。 module2依赖于module1,module3同时依赖于module2和module1。

在原始构建中,module1和module2是多个项目共享的公共模块,module3是合并其他两个的最终程序。所有模块最终都编译成一个 uber-jar 分发给最终客户。

从命令行构建并运行得很好,但在导入 IntelliJ 后,它无法正确解析本地依赖项。 IntelliJ 似乎使用了 idea:idea 目标来导入和运行 idea:idea 在我的测试用例上给出了以下输出,尽管最终构建成功,但它显然有问题解决。

将项目导入 IntelliJ 后,所有跨模块引用都显示为未解析的符号。

此测试用例的完整源代码位于: https://github.com/hutch31/maven-intellij-plugin-dep

让 IntelliJ 找出跨 Maven 子项目的这些引用是否需要缺少/额外的步骤?

我正在使用 Maven 3.6.3 和 IntelliJ 2021.1。 IntelliJ 的集成 Maven 也是 3.6.3 版本。

编辑: 我试过改变父模块的顺序,给子模块添加父标签,不影响运行。我更新了存储库以反映原始代码的状态,其中 module3 有一个父标签,但 module1 和 module2 没有(因为它们是通用代码)。

在命令行上,我使用“mvn clean test”构建和运行。我还添加了一个 Maven 'compile' 配置,尽管报告了错误,但它仍然可以正确构建。

$ mvn idea:idea
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] mod1                                                               [jar]
[INFO] mod2                                                               [jar]
[INFO] mod3                                                               [jar]
[INFO] Cable Test Studio                                                  [pom]
[INFO]
[INFO] -------------------------< org.ghutchis:mod1 >--------------------------
[INFO] Building mod1 1.0-SNAPSHOT                                         [1/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-idea-plugin:2.2.1:idea (default-cli) > generate-resources @ mod1 >>>
[INFO]
[INFO] <<< maven-idea-plugin:2.2.1:idea (default-cli) < generate-resources @ mod1 <<<
[INFO]
[INFO]
[INFO] --- maven-idea-plugin:2.2.1:idea (default-cli) @ mod1 ---
[INFO]
[INFO] -------------------------< org.ghutchis:mod2 >--------------------------
[INFO] Building mod2 1.0-SNAPSHOT                                         [2/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-idea-plugin:2.2.1:idea (default-cli) > generate-resources @ mod2 >>>
[INFO]
[INFO] <<< maven-idea-plugin:2.2.1:idea (default-cli) < generate-resources @ mod2 <<<
[INFO]
[INFO]
[INFO] --- maven-idea-plugin:2.2.1:idea (default-cli) @ mod2 ---
[WARNING] An error occurred during dependency resolution of the following artifact:

    org.ghutchis:mod21.0-SNAPSHOT

Caused by: Missing:
----------
1) org.ghutchis:mod1:jar:1.0-SNAPSHOT

  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -DgroupId=org.ghutchis -DartifactId=mod1 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -DgroupId=org.ghutchis -DartifactId=mod1 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency:
        1) org.ghutchis:mod2:jar:1.0-SNAPSHOT
        2) org.ghutchis:mod1:jar:1.0-SNAPSHOT

----------
1 required artifact is missing.

for artifact:
  org.ghutchis:mod2:jar:1.0-SNAPSHOT

from the specified remote repositories:
  central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)

[INFO]
[INFO] -------------------------< org.ghutchis:mod3 >--------------------------
[INFO] Building mod3 1.0-SNAPSHOT                                         [3/4]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] >>> maven-idea-plugin:2.2.1:idea (default-cli) > generate-resources @ mod3 >>>
[INFO]
[INFO] <<< maven-idea-plugin:2.2.1:idea (default-cli) < generate-resources @ mod3 <<<
[INFO]
[INFO]
[INFO] --- maven-idea-plugin:2.2.1:idea (default-cli) @ mod3 ---
[WARNING] An error occurred during dependency resolution of the following artifact:

    org.ghutchis:mod31.0-SNAPSHOT

Caused by: Missing:
----------
1) org.ghutchis:mod2:jar:1.0-SNAPSHOT

  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -DgroupId=org.ghutchis -DartifactId=mod2 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -DgroupId=org.ghutchis -DartifactId=mod2 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency:
        1) org.ghutchis:mod3:jar:1.0-SNAPSHOT
        2) org.ghutchis:mod2:jar:1.0-SNAPSHOT

2) org.ghutchis:mod1:jar:1.0-SNAPSHOT

  Try downloading the file manually from the project website.

  Then, install it using the command:
      mvn install:install-file -DgroupId=org.ghutchis -DartifactId=mod1 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file

  Alternatively, if you host your own repository you can deploy the file there:
      mvn deploy:deploy-file -DgroupId=org.ghutchis -DartifactId=mod1 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/path/to/file -Durl=[url] -DrepositoryId=[id]

  Path to dependency:
        1) org.ghutchis:mod3:jar:1.0-SNAPSHOT
        2) org.ghutchis:mod1:jar:1.0-SNAPSHOT

----------
2 required artifacts are missing.

for artifact:
  org.ghutchis:mod3:jar:1.0-SNAPSHOT

from the specified remote repositories:
  central (https://repo.maven.apache.org/maven2, releases=true, snapshots=false)

[INFO]
[INFO] ------------------------< org.ghutchis:parent >-------------------------
[INFO] Building Cable Test Studio 1.0-SNAPSHOT                            [4/4]
[INFO] --------------------------------[ pom ]---------------------------------
[INFO]
[INFO] >>> maven-idea-plugin:2.2.1:idea (default-cli) > generate-resources @ parent >>>
[INFO]
[INFO] <<< maven-idea-plugin:2.2.1:idea (default-cli) < generate-resources @ parent <<<
[INFO]
[INFO]
[INFO] --- maven-idea-plugin:2.2.1:idea (default-cli) @ parent ---
[INFO] jdkName is not set, using [java version1.8.0_265] as default.
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for Cable Test Studio 1.0-SNAPSHOT:
[INFO]
[INFO] mod1 ............................................... SUCCESS [  0.231 s]
[INFO] mod2 ............................................... SUCCESS [  0.021 s]
[INFO] mod3 ............................................... SUCCESS [  0.021 s]
[INFO] Cable Test Studio .................................. SUCCESS [  0.087 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.750 s
[INFO] Finished at: 2021-04-20T10:28:53+01:00
[INFO] ------------------------------------------------------------------------

【问题讨论】:

  • 首先,您创建的项目是一个所谓的聚合器,而不是您应该使用的多模块构建(每个模块都应该有一个父模块)然后它也将正常工作,否则按顺序更改这些模块会破坏你的构建。此外,这个想法插件已经有七年的历史了,不应该再使用了。你能写出你使用的是哪个 IDEA IntelliJ 版本吗?还有你使用的是哪个 Maven 版本?
  • 模块没有父模块,因为它复制了有问题的原始设计。在我们的用例中,一些模块被多个项目使用,调用父级会破坏这个用例。
  • 即使子模块以相反的顺序列出,Maven 似乎也能找出正确的依赖项构建顺序,正如我所希望的那样。
  • Maven 版本? IntelliJ 版本?你怎么称呼 Maven?
  • @khmarbaise 在原始帖子中添加了关于版本以及如何调用 Maven 的注释。

标签: java maven intellij-idea


【解决方案1】:

maven idea 插件早已被弃用:

https://maven.apache.org/plugins/maven-idea-plugin/

避免使用它。

【讨论】:

  • 我实际上并没有使用想法插件,我试图弄清楚为什么 IntelliJ 无法正确处理我的项目中的依赖项,并且似乎它正在使用想法插件来做到这一点.
  • 我不认为 IntelliJ 以任何方式使用了这个想法插件。
  • 这很公平。问题是,对我来说,idea 插件和 IntelliJ(使用它所做的任何事情)都无法正确解决模块之间的依赖关系,而 Maven 本身完全有能力做到这一点。
【解决方案2】:

我相信我已经弄清楚了测试用例中的问题,即 mod1/mod2/mod3 的包规范与文件路径不匹配。将文件从 src/main/java/org/ghutchis/module1.java 移动到 src/main/java/org/ghutchis/mod1/module1.java 修复了 IntelliJ 的测试用例构建问题(但遗憾的是,这不是原始问题)。

更新的 Java 代码已推送到 Github 以供检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-14
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    相关资源
    最近更新 更多