【Java基础】Maven继承

server/2024/9/24 9:20:27/

1. 前言

Maven 在设计时,借鉴了 Java 面向对象中的继承思想,提出了 POM 继承思想。

2. Maven继承

当一个项目包含多个模块时,可以在该项目中再创建一个父模块,并在其 POM 中声明依赖,其他模块的 POM 可通过继承父模块的 POM 来获得对相关依赖的声明。对于父模块而言,其目的是为了消除子模块 POM 中的重复配置,其中不包含有任何实际代码,因此父模块 POM 的打包类型(packaging)必须是 pom。

如图 1 所示,一个项目中存在如下多个模块。

在这里插入图片描述
如上图所示:

  • App-UI-WAR 依赖于 App-Core-lib 和 App-Data-lib。
  • Root 是 App-Core-lib 和 App-Data-lib 的父模块。
  • Root 在它的依赖部分定义了 junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 作为其依赖。

App-UI-WAR 的 pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.wangzhe.www</groupId><artifactId>App-UI-WAR</artifactId><version>1.0</version><dependencies><!-- 依赖 App-Core-lib--><dependency><groupId>net.wangzhe.www</groupId><artifactId>App-Core-lib</artifactId><version>1.0</version></dependency><!-- 依赖 App-Data-lib--><dependency><groupId>net.wangzhe.www</groupId><artifactId>App-Data-lib</artifactId><version>1.0</version></dependency></dependencies>
</project>

父模块 POM 配置

父模块 Root 的 pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.wangzhe.www</groupId><artifactId>Root</artifactId><version>1.0</version><!--定义的父类 POM 打包类型使pom  --><packaging>pom</packaging><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.9</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.18</version><scope>runtime</scope></dependency><dependency><groupId>c3p0</groupId><artifactId>c3p0</artifactId><version>0.9.1</version></dependency></dependencies>
</project>

在父模块 Root 的 pom.xml 中,其打包类型(packaging)为 pom,并声明了 3 个依赖:junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 。

子模块 POM 配置

App-Core-lib 的 pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.wangzhe.www</groupId><artifactId>App-Core-lib</artifactId><version>1.0</version><parent><groupId>net.wangzhe.www</groupId><artifactId>Root</artifactId><version>1.0</version><relativePath>../Root</relativePath></parent><dependencies><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies>
</project>

App-Data-lib 的 pom.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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>net.wangzhe.www</groupId><artifactId>App-Data-lib</artifactId><version>1.0</version><parent><groupId>net.wangzhe.www</groupId><artifactId>Root</artifactId><version>1.0</version><!-- <relativePath>../Root</relativePath> --></parent>
</project>

App-Core-lib 有 5 个依赖项,其中 junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 是从父模块 Root 中继承的;log4j 1.2.17 是该模块本身的 POM 中声明的;hamcrest 1.1 是 junit 4.9 传递下来的依赖项。

App-Data-lib 有 4 个依赖项,其中 junit 4.9、mysql-connector-java 5.1.18 以及 c3p0 0.9.1 是从父模块 Root 中继承的;hamcrest 1.1 是 junit 4.9 传递下来的依赖项。

子模块的 POM 中,当前模块的 groupId 和 version 元素可以省略,但这并不意味着当前模块没有 groupId 和 version,子模块会隐式的从父模块中继承这两个元素,即由父模块控制子模块的公司组织 id 以及版本,这样可以简化 POM 的配置。


http://www.ppmy.cn/server/36826.html

相关文章

保姆级教程:从 0 到 1 将项目发布到 Maven 中央仓库【2024年5月】

前言 大家好&#xff0c;我叫阿杆&#xff0c;不叫阿轩 最近写了一个参数校验组件&#xff0c;名字叫 spel-validator&#xff0c;是基于 javax.validation 的一个扩展&#xff0c;目的是简化参数校验。 我把项目开源到了GitHub https://github.com/stick-i/spel-validator …

【进程终止】概念理解 | 三种情况 | 退出码

目录 什么是进程终止 进程退出的场景 退出码 0退出码 &#xff01;0系统退出码 非0自定义退出码 进程的终止从三个方面去谈。清楚进程终止是在做什么&#xff1f;进程终止的3中情况&#xff1f;如何终止&#xff1f;注意进程终止不是进程暂停。 什么是进程终止 ❓进程…

语音识别--kNN语音指令识别

⚠申明&#xff1a; 未经许可&#xff0c;禁止以任何形式转载&#xff0c;若要引用&#xff0c;请标注链接地址。 全文共计3077字&#xff0c;阅读大概需要3分钟 &#x1f308;更多学习内容&#xff0c; 欢迎&#x1f44f;关注&#x1f440;【文末】我的个人微信公众号&#xf…

Cocos Creator UICanvas详解与2D游戏配置详解

前言 Cocos Creator是一款强大的2D游戏开发引擎&#xff0c;提供了丰富的工具和组件来帮助开发者快速制作出优秀的游戏作品。其中&#xff0c;UICanvas是Cocos Creator中一个非常重要的组件&#xff0c;用于管理游戏中的UI界面。 在本文中&#xff0c;我们将深入探讨Cocos Cr…

05-06 周一 Shell工程目录划分和开发最佳实践

05-06 周一 Shell工程目录划分和开发最佳实践 时间版本修改人描述2024年5月6日10:34:13V0.1宋全恒新建文档2024年5月6日11:07:12V1.0宋全恒完成 简介 之前楼主曾经完成过一个shell工程的开发&#xff0c;记得当时项目名称叫做campus-shell&#xff0c;主要是用来一键完成多个模…

【Qt QML】Qt Quick Scene Graph

Qt Quick 2是一个用于创建图形界面的库&#xff0c;它使用一个专门的场景图&#xff08;Scene Graph&#xff09;来进行渲染。通过使用OpenGL ES、OpenGL、Vulkan、Metal或Direct 3D等图形API&#xff0c;Qt Quick 2可以有效地优化图形渲染过程。使用场景图而不是传统的命令式绘…

卸载系统自带APP

Firefly RK3588 android 12自动多个系统软件&#xff0c;无法从UI界面进行手动删除。因此&#xff0c;考虑使用shell指令进行处理。 系统自动APP大多都安装在system/app目录下&#xff0c;且该目录多为只读。因此采用如下步骤&#xff0c; //Shell su adb shell su //重新挂载…

一文了解什么是SSL证书?——值得收藏

SSL证书&#xff0c;全称Secure Sockets Layer证书&#xff0c;是一种网络安全协议的实现方式&#xff0c;现在通常指的是其继任者TLS&#xff08;Transport Layer Security&#xff09;证书&#xff0c;不过习惯上仍称为SSL证书。它的主要作用是确保互联网上的数据传输安全&am…