【问题标题】:Intellij Spring maven install error "package does not exist"Intellij Spring maven 安装错误“包不存在”
【发布时间】:2020-02-11 02:41:18
【问题描述】:

Intellij 版本:社区 2019.2.3 Maven:3.6.2 春天:2.2.0

我正在尝试创建一个非常简单的 Spring maven 项目,其中包含两个子模块(一个独立的,另一个依赖于独立的)。

根模块 - testmultimodule 独立模块-独立 依赖模块 - 依赖

testmultimodule pom.xml 包含所有 Spring 相关的声明和模块定义

<modules>
    <module>independent</module>
    <module>dependant</module>
</modules>

独立的 poom.xml 最简单,.只有父 maven 声明

<parent>
    <artifactId>testmultimodule</artifactId>
    <groupId>in.org.app</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>

依赖模块 pom.xml 对独立模块有如下依赖声明

   <dependencies>
        <dependency>
            <groupId>in.org.app</groupId>
            <artifactId>independent</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

我在 dependent 模块下创建了一个 Test 类,并使用 independent 模块中的 User 对象。最初,没有上述依赖声明,通常会出现编译错误。 只要我在 Intellij IDE 中添加依赖项并使用“构建”菜单中的“构建项目”选项构建项目,它就会成功构建。

但是,如果我尝试在 Intellij 右侧窗口选项中使用 Maven 安装选项。它总是无法声明 Error:(3,33) java: package in.org.app.independent.bo does not exist

我提供了测试项目的 GitHub URL,如果您想自己查看和测试。

GIT 网址:

https://github.com/DhruboB/testmultimodule

到目前为止,我已经尝试了各种在互联网上发现的调整,例如 清除 Intellij 缓存 & 重启、mvn -U clean install、mvn 范围验证、代理等。

还有什么办法可以解决这个问题吗?我需要在 Intellij 的社区版中解决这个问题。

【问题讨论】:

  • 您的两个项目都是 Spring Boot 项目,您不能在另一个项目中使用 Spring Boot 增强 jar 作为依赖项。您必须为依赖项使用创建一个普通的 jar,Spring Boot Reference Guide 中也提到了这一点。将 spring-boot-maven-plugin 移动到您要实际打包为可执行 jar 的项目中(或按照指南发布 2 个 jar 文件 1 个可执行文件 1 个普通文件作为依赖项使用)。
  • 砰,完全合乎逻辑,非常感谢@M.Deinum,你可以使用 ANswer 按钮,所以我可以接受。
  • 除了之前提到的 Spring Boot 2.2.0 尚不存在 ...2.2.0.RC1 存在..此外,我已针对您的 repo 提出了拉取请求并修复了您的配置...更好地使用 2.1.8.RELEASE 等已发布版本并清理您的配置。构建在命令行上不起作用,因为你有编译问题......到目前为止,其余的看起来不错......在普通命令行上测试构建......
  • @khmarbaise 我已经按照 M.Deinum 的建议修复并更新了 repo。不管怎样,也谢谢你的意见。

标签: spring maven intellij-idea


【解决方案1】:

您的父项目包含spring-boot-maven-plugin 的定义。这导致每个将其定义为父项目的项目将被此插件重新打包为可执行 JAR。这个重新打包的 JAR 不能用作另一个项目的依赖项。

您需要为要用作依赖项的项目更改spring-boot-maven-plugin 的配置。这在 Spring Boot 参考指南中的 here 中进行了解释。你现在基本上有 2 个来自这个项目的 jar,一个是普通的,一个是可执行的。

如果您不需要将该项目作为可执行 JAR 文件,则只需将 spring-boot-maven-plugin 移动到需要的项目中。所有其他项目将不再是基本的 JAR 文件。

另见How to add a dependency to a Spring Boot Jar in another project?

【讨论】:

    猜你喜欢
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 2020-08-02
    • 2022-12-10
    • 1970-01-01
    • 2012-09-06
    • 2012-09-10
    • 2014-10-09
    相关资源
    最近更新 更多