maven中如何配置多模块呢?
下文笔者讲述maven中配置多模块的方法及示例分享,如下所示
3.子模块`pom.xml`示例
(如`module-a/pom.xml`)
maven配置多模块项目的实现思路
Maven中配置多模块项目 通常需要一个 父项目(Parent Project) 和 多个子模块(Modules)
maven多模块的项目结构
my-project/ ├── pom.xml (父项目) ├── module-a/ │ └── pom.xml ├── module-b/ │ └── pom.xml └── module-c/ └── pom.xml
2.父项目`pom.xml`配置
<project> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>my-project</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <!-- 定义子模块 --> <modules> <module>module-a</module> <module>module-b</module> <module>module-c</module> </modules> <!-- 可选:统一管理依赖版本 --> <dependencyManagement> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> </dependencyManagement> <!-- 可选:统一的插件配置 --> <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>
3.子模块`pom.xml`示例
(如`module-a/pom.xml`)
<project>
<modelVersion>4.0.0</modelVersion>
<!-- 父项目信息 -->
<parent>
<groupId>com.example</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>module-a</artifactId>
<dependencies>
<!-- 可以引用其他模块 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>module-b</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
4.常见命令操作
| 操作 | 命令 |
|------|------|
| 构建所有模块 | `mvn clean install` |
| 只构建某个模块及其依赖 | `mvn -pl module-a clean install` |
| 构建多个模块 | `mvn -pl module-a,module-b clean install` |
| 跳过测试构建 | `mvn clean install -DskipTests` |
注意事项
- 所有模块的 `<packaging>` 必须为 `jar` 或 `war`,父项目一般为 `pom`。
- 模块之间可以互相依赖,但不能形成循环依赖。
- 推荐使用 IDE(如 IntelliJ IDEA)来管理模块关系和依赖。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。