例如有三个模块,聚合模块Parent,和模块Son1以及模块Son2,其中Son2又依赖Son1。
Parent.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>../Son1</module>
<module>../Son2</module>
</modules>
<name>Parent</name>
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
Son1.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Son1</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Son1</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Son2.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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>Son2</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Son2</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>Son1</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
分别编译这三个模块,会是下面的现象:
1.编译Parent,同时会编译Son1和Son2。
[INFO] Reactor Build Order:
[INFO]
[INFO] Son1 [jar]
[INFO] Son2 [jar]
[INFO] Parent [pom]
2.编译Son1,则只会编译Son1。
[INFO] Building Son1 1.0-SNAPSHOT
3.编译Son2,如果先Son1先install,再编译Son2则成功。
如果不先install Son1,那么编译Son2的时候会因为找不到Son1失败。
[INFO] Building Son2 1.0-SNAPSHOT
[WARNING] The POM for org.example:Son1:jar:1.0-SNAPSHOT is missing, no dependency information available
[INFO] BUILD FAILURE
所以针对这种情况可以在Parent.pom层级下执行:
mvn clean install -pl ../Son2 -am 或者 mvn clean install -pl :Son2 -am
[INFO] Reactor Build Order:
[INFO]
[INFO] Son1 [jar]
[INFO] Son2 [jar]
-pl, --projects
Build specified reactor projects instead of all projects
-am, --also-make
If project list is specified, also build projects required by the list
(Maven 将构建指定项目所依赖的所有项目(直接或间接))
Ref:
java - Maven Modules + Building a Single Specific Module - Stack Overflowhttps://stackoverflow.com/questions/1114026/maven-modules-building-a-single-specific-moduleMaven Tips and Tricks: Advanced Reactor Optionshttps://blog.sonatype.com/2009/10/maven-tips-and-tricks-advanced-reactor-options/