SpringCloud 多模块依赖统一管理

本文介绍如何从零开始搭建一个Spring Boot项目,包括创建Maven工程、配置必要的依赖及版本管理等步骤。

新建maven工程
删除多余文件,只留下pom.xml

加入以下依赖

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

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
    
    <dependencies>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>
    
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>2020.0.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2021.1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    

选择在父模块下,右键,新建模块

在这里插入图片描述

### 提取和管理 Spring Cloud 多模块项目中的通用 POM 依赖 #### 使用父级 POM 文件集中管理依赖版本 为了有效管理和统一控制各个子模块之间依赖关系,在多模块 Maven 项目中通常会创建一个父级 `pom.xml` 文件。该文件通过 `<dependencyManagement>` 部分声明所有可能用到的库及其版本号,而具体的子模块仅需指定所需的依赖而不必重复定义版本信息。 ```xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <!-- 父POM基本信息 --> <groupId>com.example.parent</groupId> <artifactId>parent-pom</artifactId> <version>1.0-SNAPSHOT</version> <packaging>pom</packaging> <!-- 统一管理依赖版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring.boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> <!-- 定义其他常用依赖--> ... </dependencies> </dependencyManagement> <!--模块列表 --> <modules> <module>service-a</module> <module>service-b</module> <!-- 更多子模块... --> </modules> </project> ``` #### 创建可重用的 API 或工具类模块 对于那些被多个服务共同使用的接口、实体对象或是辅助函数,则建议将其封装成独立的 Java 库,并作为普通的 Maven 工程发布至私有仓库或公开仓库供各微服务引用[^3]。 ```xml <!-- service-common/pom.xml 中的内容 --> <project ... > <parent> <groupId>com.example.parent</groupId> <artifactId>parent-pom</artifactId> <version>1.0-SNAPSHOT</version> </parent> <artifactId>api-common</artifactId> <dependencies> <!-- 添加必要的开发时依赖 --> ... </dependencies> </project> ``` 之后,在任何需要的地方都可以轻松导入这个共享组件: ```xml <dependency> <groupId>com.csnz</groupId> <artifactId>api-common</artifactId> <version>${project.version}</version> </dependency> ``` #### 设置环境特定配置文件支持 考虑到不同部署环境下可能存在差异化的设置需求,可以在每个具备独立打包能力的服务内部维护一套基于 profile 的资源配置方案,即在 resources 路径下放置诸如 `application-{profile}.yml` 格式的文档来区分测试、预生产以及正式上线场景下的参数调整[^5]。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值