多个子模块还有公共模块怎么打包
时间: 2023-08-10 16:08:57 浏览: 34
如果您的 Spring Boot 项目中包含多个子模块和一个公共模块,可以按照以下步骤进行打包:
1. 确保每个子模块的 `pom.xml` 文件中都声明了对公共模块的依赖,例如:
```xml
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
```
2. 在公共模块的 `pom.xml` 文件中添加以下配置,使得公共模块可以被其他模块所依赖:
```xml
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<dependencies>
<!-- 公共模块的依赖 -->
</dependencies>
<build>
<plugins>
<!-- 公共模块的插件配置 -->
</plugins>
</build>
```
3. 在每个子模块的 `pom.xml` 文件中添加以下配置,指定需要打包的模块及其依赖:
```xml
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>${project.version}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module1</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
<!-- 子模块的其他依赖 -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 子模块的其他插件配置 -->
</plugins>
</build>
```
4. 在父项目的 `pom.xml` 文件中添加以下配置,指定需要打包的模块及其依赖:
```xml
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>${project.version}</version>
<packaging>pom</packaging>
<modules>
<module>common</module>
<module>module1</module>
<module>module2</module>
<!-- 其他子模块 -->
</modules>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 父项目的其他插件配置 -->
</plugins>
</build>
```
5. 在父项目的根目录下执行 `mvn clean package` 命令,即可打包所有子模块和公共模块。打包结果包含一个可执行的 Jar 文件和一个 lib 目录,其中 Jar 文件包含所有子模块和公共模块的代码和依赖。