Maven的聚合工程(多模块工程)

2023-04-07编程技术86022

在开发2个以上模块的时候,每个模块都是一个 Maven Project。比如搜索平台,学习平台,考试平台。开发的时候可以自己管自己独立编译,测试,运行。但如果想要将他们整合起来,我们就需要一个聚合工程。

(1) 父模块的创建.

父模块一般承担聚合模块和统一管理依赖的作用,没有实际代码和资源文件.

父模块就是创建一个普通的 Maven Project , 此处省略.

(2) 子模块的创建

① 子模块需要创建为 Maven Module 项目.

② 选择该子模块所属的父模块

打包方式 : web 项目需要打 war 包,其他的比如 dao 层, service 层, entity 层都可以打 jar 包.

实际的目录结构:子模块其实是包含在父模块文件夹里面的.

(4) 父模块的 pom 文件.

     <modules>
<module>PYG-pojo</module>
<module>PYG-dao</module>
<module>PYG-Commons</module>
...
</modules>

(5) 子模块的 pom 文件

指明它的父模块是谁

     <modelVersion>4.0.0</modelVersion>
<!-- 指定它的父模块是谁 -->
<parent>
<groupId>it.com.pyg</groupId>
<artifactId>PYG-Parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<!-- 当前子模块的名字 -->
<artifactId>PYG-dao</artifactId>

(6) 聚合工程的依赖传递

一般都是在父模块的 pom 中定义项目用到的依赖以及版本,
然后在子模块的 pom 中, 需要什么依赖就直接引入, 不引入版本号, 依赖会自动从父模块中传递到子模块中.

① 父模块中定义依赖

     <!-- 统一定义版本号 -->
<properties>
<spring.version>4.3.7.RELEASE</spring.version>
</properties>

<!-- 父模块统一管理依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

② 子模块中使用依赖

子模块中使用的话, 不需要定义版本号.

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>

(7) 聚合工程的安装.

聚合工程不需要每个模块分别安装, 只要对父工程安装即可.

① 在 打包方式为 pom 的模块中, 添加插件.

<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>

② 在父模块上, 右键 -> Run As -> Maven install 

③ 执行结果 : 父工程和子模块都Build成功了

(7) 多模块的 WEB 项目运行.

在 打包方式为 pom 的模块上, 右键

maven clean tomcat7:run

 

Maven的聚合工程(多模块工程)的相关教程结束。

本文地址:https://www.ufcn.cn/tutorials/2423585.html

如非特殊说明,本站内容均来自于网友自主分享,概不代表本站观点,如有任何问题我们都将在收到反馈后的第一时间进行处理!