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

微服务架构下如何配置pom.xml引入SpringBoot共享项目依赖

问题描述

我的微服务架构包含以下项目:

  • account(第一个微服务)
  • loan(第二个微服务)
  • shared(SpringBoot项目)

account微服务与card微服务需使用shared项目中的通用POJO类,请问如何配置account和card微服务的pom.xml来引入该共享项目?


解决方案

第一步:配置shared项目的pom.xml

确保shared项目能被其他项目作为依赖引入,核心配置如下:

<!-- 显式声明打包类型为jar(Spring Boot默认即为jar,可省略但更清晰) -->
<packaging>jar</packaging>

<!-- 定义项目坐标,这三个属性是依赖引入的核心依据 -->
<groupId>com.yourcompany</groupId> <!-- 替换为你的实际groupId -->
<artifactId>shared</artifactId>
<version>1.0.0-SNAPSHOT</version> <!-- 替换为你的实际版本号 -->

<!-- 保持Spring Boot父项目配置,保证自身构建正常 -->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.15</version> <!-- 替换为你的Spring Boot版本 -->
    <relativePath/>
</parent>

执行Maven命令将shared同步到仓库:

# 本地开发场景:安装到本地Maven仓库
mvn clean install

# 团队协作场景:部署到私有仓库(需提前在settings.xml配置仓库地址)
mvn clean deploy

第二步:在account和card微服务的pom.xml中添加依赖

直接引入shared项目的坐标即可,配置示例:

<dependencies>
    <!-- 引入shared项目依赖 -->
    <dependency>
        <groupId>com.yourcompany</groupId> <!-- 必须与shared项目的groupId一致 -->
        <artifactId>shared</artifactId> <!-- 必须与shared项目的artifactId一致 -->
        <version>1.0.0-SNAPSHOT</version> <!-- 必须与shared项目的version一致 -->
    </dependency>

    <!-- 保留原有微服务依赖,比如Spring Boot Web Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

额外注意事项

  1. 若shared包含Spring Boot自动配置类,但微服务不需要这些配置,可通过排除避免干扰:
<dependency>
    <groupId>com.yourcompany</groupId>
    <artifactId>shared</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  1. 确保所有项目的Maven仓库配置一致,避免出现依赖找不到的问题。
  2. 若card微服务为笔误(原架构中仅存在loan微服务),将上述配置同步到loan的pom.xml即可。

内容的提问来源于stack exchange,提问作者Mohamed Aymen Charrada

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 06:05:22