创建coffee-parent项目

New->Maven Project

Maven多模块构建实例-LMLPHP

Maven多模块构建实例-LMLPHP

创建coffee-web项目

右键coffee-parent项目->New->Project...

Maven多模块构建实例-LMLPHP

Maven多模块构建实例-LMLPHP

Maven多模块构建实例-LMLPHP

注意:需要在src/main/webapp下创建WEB-INF文件夹,以及在WEB-INF下创建web.xml。

创建coffee-api项目

右键coffee-parent项目->New->Project...

注意:前面步骤同coffee-web项目

Maven多模块构建实例-LMLPHP

项目结构:

Maven多模块构建实例-LMLPHP

pom.xml文件

coffee-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.coffee</groupId>
<artifactId>coffee-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>coffee-web</module>
<module>coffee-api</module>
</modules> <!-- 管理依赖,供子项目选择使用 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.coffee</groupId>
<artifactId>coffee-api</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</dependencyManagement> <build>
<!-- 配置使用jdk1.7编译,所有子项目都会继承此配置,如需配置为子项目可选继承,则需要配置到 pluginManagement下-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

coffee-web项目的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.coffee</groupId>
<artifactId>coffee-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>coffee-web</artifactId>
<packaging>war</packaging> <!-- coffee-web依赖coffee-api,无需配置version,使用父项目coffee-parent中配置的version-->
<dependencies>
<dependency>
<groupId>com.coffee</groupId>
<artifactId>coffee-api</artifactId>
</dependency>
</dependencies>
</project>

coffee-api项目的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.coffee</groupId>
<artifactId>coffee-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>coffee-api</artifactId>
</project>

构建

右键coffee-parent项目->Run As->Maven install

Maven多模块构建实例-LMLPHP

构建成功:

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary for coffee-parent 0.0.1-SNAPSHOT:
[INFO]
[INFO] coffee-parent ...................................... SUCCESS [ 0.754 s]
[INFO] coffee-api ......................................... SUCCESS [ 2.085 s]
[INFO] coffee-web ......................................... SUCCESS [ 0.888 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.980 s
[INFO] Finished at: 2019-06-03T21:44:43+08:00
[INFO] ------------------------------------------------------------------------
05-07 15:17