基于前两篇《Maven浅窥一:基础》,已经可以成功地创建Maven项目了。
复杂项目的模块管理
复杂项目结构下,通常包括多个模块。而这多个模块必然有一些公共依赖。这时,可以将公共部分提出来以简化模块管理。以下是实现复杂项目管理的几种方式。
复杂项目的模块管理方式一
方式一:用根目录下的POM中维护公共依赖。
查看代码 https://github.com/alvachien/learning-notes/tree/master/maven-tutorial-2/maven-way-1
complex-project
├──── pom.xml
├──── module-a
│ ├──── pom.xml
│ └──── src
├──── module-b
│ ├──── pom.xml
│ └──── src
└──── module-c
├──── pom.xml
└──── src
根目录的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>maven-way-1</artifactId>
<version>1.1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>maven-tutorial-2</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.release>15</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>module-a</module>
</modules>
</project>
而子目录,如module-a下的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>maven-way-1</artifactId>
<version>1.1.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<artifactId>module-a</artifactId>
<packaging>jar</packaging>
<name>module-a</name>
</project>
复杂项目的模块管理方式二
方式二:将放在一个公用目录,下例中,该公共目录是\parent\。
查看代码 https://github.com/alvachien/learning-notes/tree/master/maven-tutorial-2/maven-way-2
复杂项目结构下,通常包括多个模块。而这多个模块必然有一些公共依赖。这时,可以将公共部分提出来放在一个公用目录,下例中,该公共目录是\parent\。示例如下:
complex-project
├──── pom.xml
├──── parent
│ └──── pom.xml
├──── module-a
│ ├──── pom.xml
│ └──── src
├──── module-b
│ ├──── pom.xml
│ └──── src
└──── module-c
├──── pom.xml
└──── src
根目录下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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>maven-way-2</artifactId>
<version>1.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<url>http://maven.apache.org</url>
<name>build</name>
<modules>
<module>parent</module>
<module>module-a</module>
</modules>
</project>
子目录parent下的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>parent</artifactId>
<version>1.2.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>maven-tutorial</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.release>15</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
子目录下module-a的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>parent</artifactId>
<version>1.2.0-SNAPSHOT</version>
<relativePath>../parent</relativePath>
</parent>
<artifactId>module-a</artifactId>
<packaging>jar</packaging>
<name>module-a</name>
</project>
复杂项目的模块管理方式三
方式三,Bill of materials
官方文档见: 链接
代码 https://github.com/alvachien/learning-notes/tree/master/maven-tutorial-2/maven-way-3
complex-project
├──── pom.xml
└──── parent
├──── pom.xml
├──── module-a
│ ├──── pom.xml
│ └──── src
├──── module-b
│ ├──── pom.xml
│ └──── src
└──── module-c
├──── pom.xml
└──── src
根目录下的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>bom</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<properties>
<module1Version>1.0.0</module1Version>
<maven.compiler.release>15</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>module-a</artifactId>
<version>${module1Version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<modules>
<module>parent</module>
</modules>
</project>
子目录parent下的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>bom</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.7.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<modules>
<module>module-a</module>
</modules>
</project>
子目录module-a下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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>parent</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module-a</artifactId>
<version>${module1Version}</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
</dependencies>
</project>
如想要使用上面定义的library,则目标项目的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.consumer</groupId>
<artifactId>use</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>bom</artifactId>
<version>1.0.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alvachien.learning.maven-tutorial-2</groupId>
<artifactId>module-a</artifactId>
</dependency>
</dependencies>
</project>