前言
maven多模块管理其实就是让其子模块的pom文件继承父工程的pom文件。
maven父工程必须遵循以下两点:
packageing
标签的文本内容必须设置为pom
- 把
src
目录删除
多模块管理
首先创建一个maven父工程(包名为maven),删除src
目录,只有pom
文件:
<?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>
<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>child</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
然后创建一个新的子模块(包名为child),New-->Module;
创建项目时,指定父模块(Parent),GroupId和父工程保持一致即可:
创建完成后,可以看到子模块的pom文件如下:
<?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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
子模块成为父工程
同理,删除src
路径,将package
标签内容设为pom
:
<?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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<packaging>pom</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
创建一个(child模块的)子项目(包名为child-web),创建项目时,指定父模块(Parent),GroupId和父工程保持一致即可:
此时可以看到child
模块的pom
多了一个modules
标签:
<?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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<packaging>pom</packaging>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child-web</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
子模块继承父模块依赖
在父模块中添加一个依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
可以看到所有子模块及子模块的子模块都会拥有此依赖:
父工程管理子模块依赖
由于可能出现部分子模块不需要引入父工程依赖的情况,所以需要在父工程中进行管理。
使用dependencyManagement
标签加强管理子模块所有依赖
<!-- 父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
当子工程需要父工程的某个依赖时,只需要声明此依赖即可,无需指定版本,示例(child模块)
<?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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<packaging>pom</packaging>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child-web</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 声明式依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!--也可以选择不用父工程的版本
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
<version>4.11</version>
</dependency>
-->
</dependencies>
</project>
优化
同样也可以在父项目(maven)中这样指明版本:
<?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>
<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child</module>
<module>maven-java</module>
</modules>
<!-- 父工程管理依赖的版本号-->
<properties>
<!-- maven项目源文件的版本-->
<maven.compiler.source>8</maven.compiler.source>
<!-- maven项目编译时的版本-->
<maven.compiler.target>8</maven.compiler.target>
<!-- 父工程管理的版本标签名称支持自定义
通常为:项目名称+字段veersion-->
<junit-version>4.13.2</junit-version>
<!-- 也可以用下面这种写法:-->
<!-- <junit.version>4.13.2</junit.version>-->
<mysql-connector-java-version>5.1.47</mysql-connector-java-version>
</properties>
<!-- 父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 将版本号替换为自定义的标签-->
<version>${junit-version}</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
父工程管理子模块插件
和管理依赖同理,不在赘述。
在父模块的pom中管理插件:
<?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>
<groupId>org.example</groupId>
<artifactId>maven</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!-- 父工程包含的所有子模块-->
<modules>
<module>child</module>
<module>maven-java</module>
</modules>
<!-- 父工程管理依赖的版本号-->
<properties>
<!-- maven项目源文件的版本-->
<maven.compiler.source>8</maven.compiler.source>
<!-- maven项目编译时的版本-->
<maven.compiler.target>8</maven.compiler.target>
<!-- 父工程管理的版本标签名称支持自定义
通常为:项目名称+字段veersion-->
<junit-version>4.13.2</junit-version>
<!-- 也可以用下面这种写法:-->
<!-- <junit.version>4.13.2</junit.version>-->
<mysql-connector-java-version>5.1.47</mysql-connector-java-version>
</properties>
<!-- 父模块加强管理子模块依赖-->
<dependencyManagement>
<dependencies>
<!-- 在父工程添加依赖,所以子模块都会继承此依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<!-- 将版本号替换为自定义的标签-->
<version>${junit-version}</version>
<!-- scope标签用于标识maven依赖,test表示当前依赖仅参与测试环境,不参与打包-->
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector-java-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<finalName>child-web</finalName>
<!-- 和dependencyManagement同理-->
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
在子模块中(child)引用继承插件:
<?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">
<!-- 指向父工程的GAV坐标-->
<parent>
<artifactId>maven</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>child</artifactId>
<packaging>pom</packaging>
<modules>
<module>child-web</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<!-- 声明式依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- 声明式插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
本文依据 CC-BY-NC-SA 4.0许可协议授权,请您在转载时注明文章来源为 Z次元 ,若本文涉及转载第三方内容,请您一同注明。
知识积累
将Maven项目组件上传到Maven中心仓(借助sonatype)
2024/7/21
String.valueOf()踩坑记录
2024/7/21
使用Mapstruct轻松实现实体类转换
2024/7/21
发表评论