一、项目结构
在开始介绍maven多模块打包前,先介绍一下项目结构。多模块项目就是由多个小的项目组成一个大的项目,它们之间存在父子关系,也就是说每个小的项目是在一个父项目之下的。
├── parent #父模块
│ ├── pom.xml
│ ├── child1 #子模块1
│ │ ├── pom.xml
│ ├── child2 #子模块2
│ │ ├── pom.xml
│ └── child3 #子模块3
│ ├── pom.xml
└── pom.xml
在这个示例中,parent是一个父模块,包含了三个子模块child1、child2和child3。子模块之间是相互独立的,它们都有自己的pom.xml文件,定义了自己的依赖、插件等,并且它们都是在parent项目之下的。
二、maven多模块打包基础
1、子模块的pom.xml文件
在每个子模块的pom.xml文件中,需要定义一些基本信息,例如groupId、artifactId和version。
<groupId>com.example</groupId>
<artifactId>child1</artifactId>
<version>1.0-SNAPSHOT</version>
同时需要引入父模块的依赖,这个可以通过 <parent>
标签来实现。
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
2、父模块的pom.xml文件
在父模块的pom.xml文件中,也需要定义一些基本信息,例如groupId、artifactId和version。
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
重要的是,需要在 <modules>
标签下声明使用哪些子模块。
<modules>
<module>child1</module>
<module>child2</module>
<module>child3</module>
</modules>
3、多模块打包命令
在多模块项目中,就可以使用maven来进行打包了。在父模块下执行以下命令即可打包所有子模块。
mvn clean package
maven会在每个子模块的target目录下生成一个jar包。
三、多模块打包进阶
1、多模块项目中的依赖管理
在多模块项目中,存在多个模块间的依赖关系,如果没有合理地管理这些依赖,就可能会导致一些问题,比如版本冲突等问题。 为了解决这些问题,可以使用Maven的依赖管理机制。在父模块的pom.xml文件中,可以定义一些依赖的版本号等信息,然后在子模块中引用这些依赖时,直接使用父模块中定义的版本号即可。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>dependency1</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.example</groupId>
<artifactId>dependency2</artifactId>
<version>2.0</version>
</dependency>
</dependencies>
</dependencyManagement>
2、多模块项目中的插件管理
与依赖管理类似,多模块项目中也可能有很多相同的插件配置,这时候可以使用Maven的插件管理机制。在父模块的pom.xml文件中,也可以定义一些插件的配置,然后在子模块中引用这些插件时,直接使用父模块中定义的配置即可。
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifestEntries>
<Class-Path>lib/dependency1-1.0.jar lib/dependency2-2.0.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
</plugin>
</plugins>
</build>
3、多模块项目中的聚合打包
在上面的示例中,使用了mvn clean package
命令来打包所有的子模块。实际上,还可以使用聚合打包来一次性打包所有的子模块。在父模块中增加一个 <modules>
标签即可。
<modules>
<module>child1</module>
<module>child2</module>
<module>child3</module>
</modules>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
说明:这里使用了maven-assembly-plugin插件来进行聚合打包。在pom.xml同级目录下面增加一个assembly.xml文件如下:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3 http://maven.apache.org/xsd/assembly-1.1.3.xsd">
<id>all-in-one</id>
<formats>
<format>zip</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<useProjectArtifact>true</useProjectArtifact>
<outputDirectory>lib</outputDirectory>
<scope>runtime</scope>
</dependencySet>
</dependencySets>
</assembly>
说明:使用这个插件可以聚合打包所有的子模块,并将它们的依赖一一打包,生成一个zip文件。