You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Spring项目作为Maven依赖引入另一个Spring项目?

问题:将独立Spring应用作为依赖引入另一个项目的配置问题

我通过Spring Initializr创建了可独立运行的Spring应用Project A,其pom.xml内容如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0-SNAPSHOT</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>my.group.id</groupId>
<artifactId>projectA</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ProjectA</name>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>

    <!-- [...] many more Spring and non-spring related dependencies -->

</dependencies>

<!--  org.springframework.boot plugin, repositories and plugin repos here -->

该项目可独立运行,便于测试。我希望将其作为依赖引入另一个Spring应用Project B,实现以下目标:

  • Project B能扫描Project A的所有Bean
  • 保留Project A/resources目录下的文件(理想情况是这些文件位于最终可执行Jar的根目录)

遇到的问题

尝试通过IntelliJ的Maven菜单将Project A安装到本地仓库,然后在Project B中添加依赖后,出现以下问题:

  • 无法导入Project A的依赖,导致Project B中Spring注解失效
  • 无法访问Project A的类

将依赖类型设为pom后,依赖问题得到解决,但Project A的类仍然无法被查看、扫描或导入。

已尝试的无效操作

  • 将Project A设为父项目
  • 清除缓存
  • 更新Maven
  • 检查.m2目录,确认Project A已正确存在
  • 删除Project A pom.xml中的<build>标签内容

内容的提问来源于stack exchange,提问作者Xeonfin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 04:02:20