maven具有多模块聚合功能,即可以以父子关系体现模块间的关系,并关联各模块。
首先介绍一下具有继承依赖关系模块(多个模块不仅与公共模块关联,彼此间也存在相互依赖的关系)。
父模块的定义:
1.packaging必须是pom类型
2.modules部分声明子模块
3.properties部分声明公用变量,可被直接继承
4.dependencies部分是公用的依赖,可被直接继承
5.dependencyManagement部分用于管理依赖,被dependencyManagement配置的元素既不会引入父项目的依赖,也不会给子项目引入依赖,只是表示该部分是可被继承的,但不直接被引用。如果需要引用dependencyManagement部分内容,只需申明groupId和artifactId即可,不需要版本号。maven或自动继承dependencyManagement中相同名称的依赖。
6.repositoties是用来定义私服仓库的。
7.distributionManagement是用来将打包后的项目发布到私服上的。
下面的例子定义了一个iscp_esb_components父项目,其中包括了iscp_esp_components_web和iscp_esb_components_dqms 2个模块,iscp_esp_components_web依赖于iscp_esb_components_dqms模块。
<modelVersion>4.0.0</modelVersion>
<groupId>com.dhcc.iscp.components</groupId>
<packaging>pom</packaging>
<artifactId>iscp_esb_components</artifactId>
<version>2.1.0</version>
<name>iscp_esb_components</name>
<modules>
<module>iscp_esb_components_web</module>
<module>iscp_esb_components_dqms</module>
</modules>
<properties>
<package.environment>src/main/resources</package.environment>
<struts2.version>2.3.16.3</struts2.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>mvn-repo</id>
<url>http://maven.ansj.org/</url>
</repository>
</repositories>
<!-- deploy到私服 -->
<distributionManagement>
<repository>
<id>releases</id>
<name>dhcc_releases</name>
<url>http://113.140.81.70:8081/nexus/content/repositories/dhcc_releases/</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>dhcc_snapshots</name>
<url>http://113.140.81.70:8081/nexus/content/repositories/dhcc_snapshots/</url>
</snapshotRepository>
</distributionManagement>
parent部分声明父模块
dependencies部分添加iscp_esb_components_dqms模块的依赖
iscp_esb_components_web模块定义
<parent>
<groupId>com.dhcc.iscp.components</groupId>
<artifactId>iscp_esb_components</artifactId>
<version>2.1.0</version>
</parent>
<groupId>com.dhcc.iscp.components.web</groupId>
<artifactId>iscp_esb_components_web</artifactId>
<version>2.1.0</version>
<packaging>war</packaging>
<name>iscp_components_web</name>
<dependencies>
<dependency>
<groupId>com.dhcc.iscp.components</groupId>
<artifactId>iscp_esb_components_dqms</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>iscp_components_web</finalName>
<sourceDirectory>src/main/java</sourceDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
<resource>
<directory>src/main/baseResources</directory>
</resource>
</resources>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.1</version>
<configuration>
<webappDirectory>webContent</webappDirectory>
<failOnMissingWebXml>false</failOnMissingWebXml>
<packagingExcludes>
WEB-INF/lib/spring-2.5.**.jar,
WEB-INF/lib/javassist-3.11.0.GA.jar,
WEB-INF/lib/xml-apis-1.0.b2.jar,
WEB-INF/lib/jackson-core-asl-1.9.11.jar,
WEB-INF/lib/jackson-mapper-asl-1.9.11.jar,
WEB-INF/lib/c3p0-0.9.1.1.jar,
WEB-INF/lib/commons-fileupload-1.3.jar,
</packagingExcludes>
</configuration>
</plugin>
</plugins>
</build>
iscp_esb_components_dqms模块定义
1.不声明packaging,默认以jar形式打包。
2.不声明version,groupId默认继承父模块的version,groupId。
<span style="font-size:18px;"><parent>
<groupId>com.dhcc.iscp.components</groupId>
<artifactId>iscp_esb_components</artifactId>
<version>${iscp-components}</version>
</parent>
<artifactId>iscp_esb_components_dqms</artifactId>
<name>iscp_esb_components_dqms</name></span>
maven编译过程
对于这个项目,我们有2种编译方式。
第一种是直接maven clean install 父项目(iscp_esb_components)。maven会通过Reactor机制制定编译顺序,依次编译所有项目。在本例中maven会先编译iscp_esb_components项目,然后发现iscp_esb_components_web依赖于iscp_esb_components_dqms,所以会先编译iscp_esb_components_dqms,最后编译iscp_esb_components_web。
在父模块下编译,maven知道父模块下的所有子模块,并会按照依赖关系依次处理执行。
第二种是分别编译打包。即分别对个项目编译,但是这样做存在一个问题。当我们首先打包iscp_esb_components_web时,maven会发现iscp_esb_components_web依赖于iscp_esb_components_dqms,那么就会去本地仓库查找iscp_esb_components_dqms的jar,但是现在iscp_esb_components_dqms并未编译还没有jar,本地仓库还没有该jar,maven又去远程仓库查找,你也为将该jar发布到私服上,仍然找不到,那么就会报错,你需要去手动再编译iscp_esb_components_dqms。
编译maven子项目时,maven不仅对子项目编译执行,也编译执行了父项目,但不会编译兄弟项目及其兄弟项目的子项目。
不具有依赖关系的多模块项目
即兄弟项目间互不关联,都只与父项目存在依赖关系。
对于这类型的项目,我们可以删除父模块的<modules>...</modules>部分,而子模块保留parent部分。此时项目不再是一个多模块项目,而是多工程项目的一个集合。
以上例说明,去掉iscp_esb_components中的<modules>部分,那么编译iscp_esb_components时,只会编译iscp_esb_components本身,不会编译web和dqms模块。如果编译iscp_esb_components_web时,由于父子关系还存在,maven会先到仓库查找iscp_esb_components和iscp_esb_components_dqms依赖然后依次执行。
此时发布web模块时需依次"maven clean install" iscp_esb_components ——>iscp_esb_components_dqms——>iscp_esb_components_web。
如果保留父模块的<modules>...</modules>部分而删除子模块的parent部分,那么项目不再是父子项目,只是一个多模块项目,每个子项目独享自己的资源,不能共享。