5

maven | 多模块配置

 1 year ago
source link: https://benpaodewoniu.github.io/2022/11/09/maven6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

maven 多模块之间 pom 的配置。

将一个项目进行功能化拆分,后续方便使用微服务。

搭建多模块项目,需要使用 maven 的继承和聚合,其中聚合负责将多个模块集中在一起进行管理,继承则负责各子模块中的公共配置。

单独的项目 pom

先看一个单独项目的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<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.rhino</groupId>
<artifactId>analysis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>analysis</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>14</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>

多模块项目

目录结构如下

- modules
- module
- pom.xml
- pom.xml

其中 projectmodule 都是 maven 项目。

6_0.png

创建 maven 要如图中所示。

这个时候,我们看 modulespom.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>

<groupId>org.example</groupId>
<artifactId>modules</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>module</module>
</modules>
</project>

其多了一个

<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>module</module>
</modules>

pom 是最简单的打包类型。不像 jarwar,它生成的构件只有它本身。将 packaging 申明为 pom 则意味着没有代码需要测试或者编译,也没有资源需要处理。

由于我们使用了聚合,所以打包方式必须为 pom ,否则无法构建。

module 的值是子模块相对于当前 POM 的路径。

接下来看子模块的 pom.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">
<parent>
<artifactId>modules</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>module</artifactId>
</project>
<parent>
<artifactId>modules</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>

声明了该模块继承自 org.example:modules:1.0-SNAPSHOT,其实这里面还省略了<relativePath></relativePath> 由于 relativePath 默认是 ../pom.xml 而我们的子项目确实在父项目的下一级目录中,所以是可以不用填写的。

Maven首先在当前构建项目的环境中查找父pom,然后项目所在的文件系统查找,然后是本地存储库,最后是远程repo。

artifactId 是子模块的组件 id,由于继承了父pom,所以groupIdversion 也可以不写,不写的话就默认继承自父pom

子模块间的依赖

如果子模块间相互依赖,需要在 dependency 中引入要依赖的子模块,如图

6_1.png

module2 中的 pom.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">
<parent>
<artifactId>modules</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>module2</artifactId>

<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>module1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

</project>

我们可以看到其依赖于 module1,但是,也写了版本,想象一下,如果 module1 更新了版本,module2pom.xml 也需要变化,如果,还有 module3 同样依赖于 module1 的话,那后续修改起来就非常麻烦了。

所以,我们将各个模块的版本放在父项目的 pom.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>

<groupId>org.example</groupId>
<artifactId>modules</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>module2</module>
<module>module1</module>
</modules>

<properties>
<module1.version>1.0-SNAPSHOT</module1.version>
</properties>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.example</groupId>
<artifactId>module2</artifactId>
<version>${module1.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

我们就可以把 module2pom.xml 中的 module1version 给删掉了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK