Maven项目中的依赖管理、RelativePath、Maven多模块项目中Base模块和Common模块

dependencyManagement元素用于在Maven项目中统一管理依赖版本,常在顶层父pom中定义,以便子项目引用时不需指定版本。这样在升级或切换依赖版本时只需更改父pom,提高了效率。在多模块项目中,例如base、common、service等不同功能模块,可以合理分配依赖,如公用依赖在base中声明,特定模块的依赖在对应模块声明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Maven项目中的依赖管理——dependencyManagement

通常会在一个组织或者项目的最顶层父pom中可以看到dependencyManagement元素

使用pom.xml中的dependencyManagement元素能让所有的子项目中引用一个依赖而不需要显示的列出版本号。

Maven会沿用父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用dependencyManagement元素中提供的指定版本号。

dependencyManagement的优点:

多个子项目引用同样的依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或者切换到另外一个版本时,只需要在顶层父容器更新版本号即可,不需要一个一个子项目中修改;另外如果某个子项目需要另外一个版本,则需要声明version即可。

RelativePath


1)指定查找该父项目pom.xml的(相对)路径。默认顺序:relativePath > 本地仓库 > 远程仓库
2)没有relativePath标签等同…/pom.xml, 即默认从当前pom文件的上一级目录找
3)表示不从relativePath找, 直接从本地仓库找,找不到再从远程仓库找


maven首先从当前构建项目开始查找父项目的pom文件,然后从本地仓库,最有从远程仓库。RelativePath允许你选择一个不同的位置。

多模块项目中

各个模块

xxx-service-base         用于编写业务逻辑 
xxx-service-common       用于存放一些公用的模块,比如说枚举,topic
xxx-service-socket       用于维护socket连接
xxx-service-vue          前端
xxx-service-web           web模块,为前端提供一些接口

Base模块

Base包是模块公用部分抽出来 Base包不对外暴露 其中基本全是业务代码(与业务相关的)。

 <!-- 阿里数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
 
        <!--mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!--aop-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
 
        <!-- xxl-job-core -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

base包依赖于common包,可能用到common的枚举

 <dependency>
            <groupId>com.qcby</groupId>
            <artifactId>chat-service-common</artifactId>
        </dependency>

注意:公用的依赖会在base里面,但是如果web用不到,socket可以用到的依赖就可以只放在socket包中

Common模块

Common包基本不含业务代码 Common不含任何与业务相关的东西·

 该模块为全局公共模块,其它各模块以项目依赖的方式直接引用,在类中直接import该模块下的类。

### Jeecg-Boot Maven Dependencies Configuration Jeecg-Boot 是一个基于 Spring Boot 的快速开发平台,其模块化设计使得开发者可以通过调整 `pom.xml` 文件来引入不同的功能模块。以下是关于如何配置 Jeecg-Boot 相关的 Maven 依赖的内容。 #### 配置示例 在 `jeecg-boot-module-system` 项目中,`pom.xml` 文件内的 `<dependencies>` 节点定义了所需的依赖项。例如: ```xml <dependencies> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-base-common</artifactId> </dependency> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-module-jm</artifactId> <version>2.0.2</version> </dependency> </dependencies> ``` 上述代码片段展示了两个主要依赖项:一个是基础公共组件 `jeecg-boot-base-common`[^1];另一个是特定功能模块 `jeecg-boot-module-jm` 并指定了版本号 `2.0.2`。 #### Dependency Management Plugin Management 为了更好地管理维护依赖关系以及插件配置,通常会在父 POM 中使用 `<dependencyManagement>` `<pluginManagement>` 节点。其中: - `<dependencyManagement>` 提供了一种机制用于集中管理依赖版本号,从而避免子模块重复声明版本信息[^2]。 - `<pluginManagement>` 则负责统一管理构建过程中使用的插件及其配置参数。 对于 Spring Boot 应用程序而言,默认情况下通过继承 `spring-boot-starter-parent` 或者导入 `spring-boot-dependencies` BOM (Bill of Materials),可以实现大部分常用库版本的一致性控制[^3]。这意味着如果您的项目已经继承了某个 Spring Boot Starter Parent,则无需显式指定许多第三方库的具体版本号。 #### 特殊资源配置 除了基本的依赖之外,Spring Boot 还提供了强大的资源处理能力,比如自动识别并加载位于 classpath 下的应用配置文件 (`application.properties`, `application.yml`) 及其变体形式(如按 profile 分割后的配置)。这有助于根据不同环境灵活切换相应的设置值。 --- ### 示例代码展示 下面是一个完整的 `pom.xml` 骨架结构,包含了前面提到的关键部分: ```xml <?xml version="1.0" encoding="UTF-8"?> <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> <!-- 继承自 Spring Boot Starter Parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo-project</artifactId> <version>1.0-SNAPSHOT</version> <properties> <java.version>11</java.version> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-bom</artifactId> <version>3.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-base-common</artifactId> </dependency> <dependency> <groupId>org.jeecgframework.boot</groupId> <artifactId>jeecg-boot-module-jm</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 此示例不仅涵盖了 Jeecg-Boot 的核心依赖,还利用了 BOM 来简化版本管理过程,并集成了 Spring Boot 官方推荐的最佳实践。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值