本文将在以下三部分进行编写:
Maven聚合即可以将多个子模块聚合在一起进行构建,继承即所有子模块均可以继承父模块的属性、依赖和插件等,可消除重复配置。
Maven基础性的知识不在此赘述,本文中涉及的Maven关键标签包括:
举一个比较常见的例子,假设某项目project包含三个模块,分别是:project_a、project_b、project_c,其文件结构如下,三个子模块与pom.xml在同一级,与父模块是父子目录结构:
/project
/project_a
/project_b
/project_c
pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>com.test</groupId> 5 <artifactId>project</artifactId> 6 <version>0.0.1-SNAPSHOT</version> 7 <packaging>pom</packaging> 8 <modules> 9 <module>project_a</module> 10 <module>project_b</module> 11 <module>project_c</module> 12 </modules> 13 14 <properties> 15 <spring.version>4.3.1.RELEASE</spring.version> 16 </properties> 17 18 <dependencyManagement> 19 <dependencies> 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-aop</artifactId> 23 <version>${spring.version}</version> 24 </dependency> 25 ...... 26 <dependency> 27 <groupId>org.springframework</groupId> 28 <artifactId>spring-core</artifactId> 29 <version>${spring.version}</version> 30 </dependency> 31 </dependencies> 32 </dependencyManagement> 33 34 <build> 35 <pluginManagement> 36 <plugins> 37 <plugin> 38 <groupId>org.apache.maven.plugins</groupId> 39 <artifactId>maven-compiler-plugin</artifactId> 40 <version>3.1</version> 41 <configuration> 42 <source>1.7</source> 43 <target>1.7</target> 44 </configuration> 45 </plugin> 46 ...... 47 <plugin> 48 <groupId>org.apache.maven.plugins</groupId> 49 <artifactId>maven-surefire-plugin</artifactId> 50 <version>2.12.4</version> 51 <configuration> 52 <skipTests>true</skipTests> 53 </configuration> 54 </plugin> 55 </plugins> 56 </pluginManagement> 57 </build> 58 59 <repositories> 60 <repository> 61 <id>nexus</id> 62 <name>maven repository</name> 63 <url>http://serverip:port/nexus/content/groups/public/</url> 64 <snapshots> 65 <enabled>true</enabled> 66 </snapshots> 67 <releases> 68 <enabled>true</enabled> 69 </releases> 70 </repository> 71 </repositories> 72 73 <!-- 发布 构件 到 私服 --> 74 <distributionManagement> 75 <repository> 76 <id>nexus</id> 77 <name>releases</name> 78 <url>http://serverip:port/nexus/content/repositories/releases/</url> 79 </repository> 80 81 <snapshotRepository> 82 <id>nexus</id> 83 <name>snapshots</name> 84 <url>http://serverip:port/nexus/content/repositories/snapshots/</url> 85 </snapshotRepository> 86 </distributionManagement> 87 </project>
<servers> <server> <id>nexus</id> <username>admin</username> <password>admin123</password> </server> </servers>
1 <?xml version="1.0"?> 2 <project 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 4 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 5 <modelVersion>4.0.0</modelVersion> 6 <parent> 7 <groupId>com.test</groupId> 8 <artifactId>project</artifactId> 9 <version>0.0.1-SNAPSHOT</version> 10 </parent> 11 <artifactId>project_a</artifactId> 12 <name>project_a</name> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework</groupId> 17 <artifactId>spring-aop</artifactId> 18 </dependency> 19 ...... 20 <dependency> 21 <groupId>org.springframework</groupId> 22 <artifactId>spring-core</artifactId> 23 </dependency> 24 </dependencies> 25 26 <build> 27 <plugins> 28 <plugin> 29 <groupId>org.apache.maven.plugins</groupId> 30 <artifactId>maven-compiler-plugin</artifactId> 31 </plugin> 32 ...... 33 <plugin> 34 <groupId>org.apache.maven.plugins</groupId> 35 <artifactId>maven-surefire-plugin</artifactId> 36 </plugin> 37 </plugins> 38 </build> 39 </project>
在对工程只需 mvn clean install等命令的时候,Maven会首先解析聚合模块的POM、分析要构建的模块、并计算出一个反应堆构建顺序(Reactor Build Order),然后根据这个顺序依次构建模块。其中反应堆是所有模块组成的一个构建结构,包含各模块之间的继承与依赖关系,计算合理的模块构建顺序。(模块构建具体经历过哪些过程,可学习Maven中的生命周期)。
假设三个模块之间存在一定的依赖关系,如图:
DBAC2D.png" alt="">
本工程的构建顺序如下,聚合模块project、project_a、project_c、project_b:
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] project
[INFO] project_a
[INFO] project_c
[INFO] project_b
[INFO]
[INFO] ------------------------------------------------------------------------
还可以通过命令行对反应堆进行裁剪,包括:
举例:mvn clean install -pl project_a,project_b
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] project_a
[INFO] project_b
[INFO]
[INFO] ------------------------------------------------------------------------
mvn clean install -pl project_a -am
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] project
[INFO] project_a
[INFO]
[INFO] ------------------------------------------------------------------------
mvn clean install -pl project_a -amd
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] project_a
[INFO] project_c
[INFO] project_b
[INFO]
[INFO] ------------------------------------------------------------------------
mvn clean install -pl project_a -amd -rf project_c
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Build Order:
[INFO]
[INFO] project_c
[INFO] project_b
[INFO]
[INFO] ------------------------------------------------------------------------
在父模块中声明了distributionManagement,在执行 mvn deploy的时候,会将所有的模块部署到远程服务器上。如何做到有选择性的的部署呢?比如不想将project_a部署到Maven仓库,有两种方式:
(1)在properties元素中声明 maven.deploy.skip
在project_a模块的POM文件中添加如下配置:
<properties> <maven.deploy.skip>true</maven.deploy.skip> </properties>
(2)声明子模块插件,覆盖父模块插件
在project_a模块的POM文件中,添加属于自己的部署插件:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.4</version> <configuration> <skip>true</skip> </configuration> </plugin>
《Maven实战》