一、多模块项目的组织
当我们需要管理一个复杂的项目时,我们通常需要将这个项目划分成几个子模块。这些子模块可以是独立的,也可以是相互依赖的。例如,在一个Web应用程序中,我们可以分别为Service、Web和DAO层创建三个子模块。 下面是一个简单的多模块项目的结构示例:
example-project/
|-- pom.xml(父模块pom)
|-- example-service/
| |-- pom.xml(服务模块pom)
| |-- src/
| |-- main/
| | |-- java/...
| | |-- resources/...
| |-- test/...
|-- example-web/
| |-- pom.xml(Web模块pom)
| |-- src/
| |-- main/
| | |-- webapp/
| | | |-- WEB-INF/...
| | |-- java/...
| | |-- resources/...
| |-- test/...
|-- example-dao/
| |-- pom.xml(DAO模块pom)
| |-- src/
| |-- main/
| | |-- java/...
| | |-- resources/...
| |-- test/...
在上面的示例中,example-project是整个项目的父模块,example-service、example-web和example-dao分别是三个子模块。父模块有一个pom.xml文件来定义子模块的依赖关系和通用的配置。每个子模块都有自己的pom.xml文件和源代码目录。
二、基本的Maven多模块配置
下面是一个简单的Maven多模块配置文件,包括一个父pom和两个子模块pom:
1. 父模块pom.xml
<project>
<groupId>com.example</groupId>
<artifactId>example-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>example-service</module>
<module>example-web</module>
</modules>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
上面的pom.xml文件定义了整个项目的groupId、artifactId和version。dependency元素定义了整个项目的依赖项,例如guava和postgresql库。modules元素定义了父模块所包括的子模块。build元素定义了整个项目的编译配置,其中包括编译插件的设置。由于这些设置是对整个项目有效的,因此它们应该定义在父模块中。
2. 服务模块example-service的pom.xml
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>example-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>example-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>29.0-jre</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
上面的pom.xml文件定义了服务模块的groupId、artifactId和version。parent元素指定了父模块。dependencies元素定义了子模块的依赖项。build元素定义了子模块的编译配置,其中包括编译插件的设置。
3. Web模块example-web的pom.xml
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>example-project</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>example-web</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>example-service</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<webXml>src/main/webapp/WEB-INF/web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>
上面的pom.xml文件定义了Web模块的groupId、artifactId和version。parent元素指定了父模块。dependencies元素定义了子模块的依赖项。build元素定义了子模块的编译配置,其中包括编译插件的设置。
三、多模块项目的实战
我们可以使用多模块Maven项目的大多数功能来管理我们的项目。在下面的示例中,我们将创建一个简单的多模块项目,该项目包含一个主模块和两个子模块。主模块(name-parent)将作为父模块,其中一个子模块(name-app)将是我们的主要应用程序,另一个子模块(name-lib)将是我们的库。
1. 创建主模块pom.xml文件
在我们开始之前,让我们先创建父模块的pom.xml文件。我们可以按照下面的结构编写:
<project>
<groupId>com.example</groupId>
<artifactId>name-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>name-app</module>
<module>name-lib</module>
</modules>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.2.6.RELEASE</spring.boot.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我们使用了dependencyManagement
元素来管理所有的依赖项。我们可以从Spring Boot开发人员页面下载主要版本,然后从Dependency Information部分中获取最新版本。properties元素定义了我们使用的Java和Spring Boot版本。最后,build元素定义了我们使用的Spring Boot插件。
2. 创建子模块name-app的pom.xml文件
我们还需要为我们的Spring Boot应用程序创建一个子模块,其中包含应用程序的代码和依赖项。创建子模块的过程与父模块类似:
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>name-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>name-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>name-lib</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
我们在父模块中指定了dependencyManagement
元素,所以我们不需要在这里重复定义。我们将Spring Boot和我们的库作为依赖项添加到此模块中。