Maven构建模块化工程详细流程
模块化工程是大型项目开发中的常见实践,Maven提供了良好的支持。以下是使用Maven构建模块化工程的详细流程:
1. 创建父工程
-
创建父项目目录结构:
parent-project/ ├── pom.xml ├── module1/ │ └── pom.xml ├── module2/ │ └── pom.xml └── module3/ └── pom.xml -
父项目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.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> <packaging>pom</packaging> <!-- 关键:父项目打包类型为pom --> <modules> <module>module1</module> <module>module2</module> <module>module3</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties> <dependencyManagement> <!-- 统一管理依赖版本 --> </dependencyManagement> <build> <!-- 公共构建配置 --> </build> </project>
2. 创建子模块
-
创建子模块目录结构:
- 在父项目目录下执行:
mvn archetype:generate -DgroupId=com.example -DartifactId=module1 -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 在父项目目录下执行:
-
子模块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.example</groupId> <artifactId>parent-project</artifactId> <version>1.0.0</version> </parent> <artifactId>module1</artifactId> <dependencies> <!-- 模块特定依赖 --> </dependencies> </project>
3. 模块间依赖管理
-
模块间依赖:
- 如果module2需要依赖module1:
<!-- 在module2的pom.xml中添加 --> <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>module1</artifactId> <version>${project.version}</version> <!-- 使用父项目定义的版本 --> </dependency> </dependencies> -
依赖版本统一管理(在父pom中):
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.8</version> </dependency> </dependencies> </dependencyManagement>
4. 构建与打包
-
构建整个项目:
# 在父项目目录下执行 mvn clean install -
构建特定模块:
mvn -pl module1 clean install -
跳过测试:
mvn install -DskipTests
5. 常见模块化结构示例
典型三层架构模块化项目
enterprise-app/
├── pom.xml (packaging: pom)
├── app-api/ (接口定义)
│ └── pom.xml (packaging: jar)
├── app-service/ (业务逻辑)
│ └── pom.xml (packaging: jar)
├── app-dao/ (数据访问)
│ └── pom.xml (packaging: jar)
├── app-web/ (Web层)
│ └── pom.xml (packaging: war)
└── app-integration/ (外部集成)
└── pom.xml (packaging: jar)
微服务架构模块化项目
microservices-platform/
├── pom.xml (packaging: pom)
├── common-core/ (公共核心)
│ └── pom.xml (packaging: jar)
├── user-service/ (用户服务)
│ ├── user-api/
│ ├── user-service/
│ └── user-web/
├── order-service/ (订单服务)
│ ├── order-api/
│ ├── order-service/
│ └── order-web/
└── gateway/ (API网关)
└── pom.xml (packaging: war)
6. 高级配置技巧
-
资源过滤:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> -
多环境配置:
<profiles> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>prod</id> <properties> <env>prod</env> </properties> </profile> </profiles> -
自定义插件配置:
<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> </plugins> </build>
7. 最佳实践
-
模块划分原则:
- 按功能或业务领域划分
- 高内聚、低耦合
- 公共功能单独模块
-
版本管理:
- 使用父POM统一管理版本
- 遵循语义化版本控制(SemVer)
-
依赖管理:
- 最小化依赖范围
- 避免循环依赖
- 使用
<dependencyManagement>统一版本
-
构建优化:
- 并行构建:
mvn -T 4 clean install - 增量构建:
mvn -am -pl module1 install
- 并行构建:
通过以上流程和最佳实践,您可以构建出结构清晰、易于维护的Maven模块化工程。
2416

被折叠的 条评论
为什么被折叠?



