Maven多模块与依赖管理
<module>标签
module,即模块。
之所以有module的概念,是因为一个大型的软件系统,往往由多个子系统构成。
这些子系统并不是以一个工程的形式存在的,而是多个工程并行存在,从而满足项目管理和并行开发的需要。
模块之间的依赖关系会很复杂,可能的情况是:
如果不通过module管理上述复杂的依赖关系,你的做法可能是:
每个模块单独一个maven项目,单独构建每个maven项目,然后当你需要的时候在<dependency>中添加相应项目的依赖。
问题在于:
每个模块的构建都要单独进行,如果你的项目有几十个模块,那么每次构建项目将需要运行大量的命令。并且项目之间的依赖将影响构建的顺序,如模块4依赖模块1和模块2,那么模块1和模块2必须在模块4之前被构建。
最佳实践:
创建一个parent项目,在parent.pom中定义所有的<module>,并定义所有<dependency>的版本号,从而一次构建parent项目,就可以完成所有模块项目的构建。
<dependencyManagement>标签
这个标签的存在,是为了解决jar包依赖的版本冲突问题。
可能的情况是:
module1的依赖:module1->XXversion1
module2的依赖:module2->XXversion2
此时会出现jar包版本冲突,使用<dependencyManagement>解决方式如下:
在多模块项目中,将在parent.pom的<dependencyManagement>中定义所有依赖的版本号,从而防止版本号冲突。
eclipse中构建多模块项目的过程
具体操作的过程,因为过于繁琐,所以这里引用一篇博文来介绍
最终,我的目录结构是:
各个模块pom.xml
1.parant
<?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.lin.many</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging> <!--注意打包方式为pom,主要负责子模块的构建-->
<modules>
<module>module1</module>
<module>module2</module>
<module>modules-all</module> <!-- 整合了module1和module2的模块 -->
</modules>
<properties> <!-- 所有依赖的版本管理 -->
<junit-version>3.8.1</junit-version>
<netty-version>4.1.1.Final</netty-version>
<jackson-version>2.8.8</jackson-version>
<jexcel-version>2.4.2_4</jexcel-version>
</properties>
<dependencyManagement>
<dependencies>
<!-- 本项目模块的依赖 -->
<dependency>
<groupId>org.lin.many</groupId>
<artifactId>module1</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.lin.many</groupId>
<artifactId>module2</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.lin.many</groupId>
<artifactId>modules-all</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 第三方依赖 -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit-version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jexcelapi</artifactId>
<version>${jexcel-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.module1(module1和module2差不多,只贴一个)
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent> <!-- 继承parent -->
<groupId>org.lin.many</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>module1</artifactId>
<packaging>jar</packaging>
<name>module1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies> <!-- 注意,所有依赖没有写版本号 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<!-- excel读取 -->
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jexcelapi</artifactId>
</dependency>
</dependencies>
</project>
3.modules-all
这个模块整合了module1和module2
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.lin.many</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>modules-all</artifactId>
<name>modules-all</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency> <!--注意,依赖了两个项目内部模块-->
<groupId>org.lin.many</groupId>
<artifactId>module1</artifactId>
</dependency>
<dependency>
<groupId>org.lin.many</groupId>
<artifactId>module2</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<!-- excel读取 -->
<dependency>
<groupId>org.apache.servicemix.bundles</groupId>
<artifactId>org.apache.servicemix.bundles.jexcelapi</artifactId>
</dependency>
</dependencies>
</project>
总结
这样去构建maven项目应该是最佳实践了,因为开源项目就是这样去做的,如apache的activeMQ。
吐槽一下自己的博文,别人都是图文并茂逻辑清晰,而我自己写的,自问自己也看不下去,权当一个总结吧!