公司级 Maven 多模块项目完整案例

88 阅读3分钟

公司级 Maven 多模块项目完整案例

下面提供一个完整的公司级 Maven 多模块项目案例,包括项目架构设计、模块划分、配置文件和最佳实践。

一、项目架构概述

1. 项目结构设计

company-project/
├── company-parent/          # 公司级父POM,统一管理依赖版本和构建配置
│   └── pom.xml
├── company-common/          # 公共工具模块
│   ├── pom.xml
│   └── src/
├── company-framework/       # 框架级模块,提供基础框架功能
│   ├── pom.xml
│   └── src/
├── company-business/        # 业务核心模块
│   ├── pom.xml
│   └── src/
├── company-service/         # 服务层模块
│   ├── pom.xml
│   └── src/
├── company-api/             # API接口模块
│   ├── pom.xml
│   └── src/
├── company-web/             # Web应用模块,可独立运行
│   ├── pom.xml
│   └── src/
└── company-test/            # 测试工具模块
    ├── pom.xml
    └── src/

2. 模块依赖关系

company-web → company-api → company-service → company-business → company-framework → company-common

二、详细配置示例

1. 公司级父POM (company-parent/pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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.company</groupId>
    <artifactId>company-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>Company Parent POM</name>
    <description>Company-wide parent POM for dependency management and build configuration</description>

    <!-- 模块声明 -->
    <modules>
        <module>../company-common</module>
        <module>../company-framework</module>
        <module>../company-business</module>
        <module>../company-service</module>
        <module>../company-api</module>
        <module>../company-web</module>
        <module>../company-test</module>
    </modules>

    <!-- 版本属性定义 -->
    <properties>
        <!-- 基础配置 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        
        <!-- Spring 版本 -->
        <spring-boot.version>2.7.10</spring-boot.version>
        <spring.version>5.3.20</spring.version>
        
        <!-- 数据库相关 -->
        <mybatis.version>3.5.9</mybatis.version>
        <mysql-connector.version>8.0.30</mysql-connector.version>
        
        <!-- 日志相关 -->
        <slf4j.version>1.7.36</slf4j.version>
        <logback.version>1.2.11</logback.version>
        
        <!-- 测试相关 -->
        <junit.version>4.13.2</junit.version>
        <mockito.version>4.5.1</mockito.version>
        
        <!-- 插件版本 -->
        <maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
        <maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
        <maven-jar-plugin.version>3.2.2</maven-jar-plugin.version>
        <maven-assembly-plugin.version>3.4.2</maven-assembly-plugin.version>
        
        <!-- 公司项目版本 -->
        <company-common.version>${project.version}</company-common.version>
        <company-framework.version>${project.version}</company-framework.version>
        <company-business.version>${project.version}</company-business.version>
        <company-service.version>${project.version}</company-service.version>
        <company-api.version>${project.version}</company-api.version>
    </properties>

    <!-- 依赖管理 -->
    <dependencyManagement>
        <dependencies>
            <!-- Spring Boot 依赖管理 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            
            <!-- 公司内部模块版本管理 -->
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>company-common</artifactId>
                <version>${company-common.version}</version>
            </dependency>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>company-framework</artifactId>
                <version>${company-framework.version}</version>
            </dependency>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>company-business</artifactId>
                <version>${company-business.version}</version>
            </dependency>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>company-service</artifactId>
                <version>${company-service.version}</version>
            </dependency>
            <dependency>
                <groupId>com.company</groupId>
                <artifactId>company-api</artifactId>
                <version>${company-api.version}</version>
            </dependency>
            
            <!-- 数据库相关依赖 -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql-connector.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!-- 构建配置 -->
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>${maven-compiler-plugin.version}</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>${project.build.sourceEncoding}</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>${maven-surefire-plugin.version}</version>
                    <configuration>
                        <skipTests>${skipTests}</skipTests>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <!-- 多环境配置 -->
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <env>dev</env>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <env>prod</env>
            </properties>
        </profile>
    </profiles>
</project>

2. 公共工具模块 (company-common/pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.company</groupId>
        <artifactId>company-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../company-parent/pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>company-common</artifactId>
    <name>Company Common</name>
    <description>Common utilities and constants for company projects</description>

    <dependencies>
        <!-- 日志依赖 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
        </dependency>
        
        <!-- 工具库 -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    </dependencies>
</project>

3. 业务核心模块 (company-business/pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.company</groupId>
        <artifactId>company-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../company-parent/pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>company-business</artifactId>
    <name>Company Business</name>
    <description>Core business logic implementation</description>

    <dependencies>
        <!-- 依赖框架模块 -->
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>company-framework</artifactId>
        </dependency>
        
        <!-- 数据访问 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
</project>

4. Web应用模块 (company-web/pom.xml)

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.company</groupId>
        <artifactId>company-parent</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../company-parent/pom.xml</relativePath>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>company-web</artifactId>
    <packaging>jar</packaging>
    <name>Company Web Application</name>
    <description>Web application module</description>

    <dependencies>
        <!-- 依赖API模块 -->
        <dependency>
            <groupId>com.company</groupId>
            <artifactId>company-api</artifactId>
        </dependency>
        
        <!-- Spring Boot Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <!-- 测试依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <mainClass>com.company.web.Application</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

三、项目实施最佳实践

1. 模块职责划分

  • company-parent:只负责版本管理和构建配置,不包含业务代码
  • company-common:提供基础工具类、常量定义、通用异常等
  • company-framework:提供业务无关的框架功能,如缓存、安全、消息队列等
  • company-business:核心业务逻辑,领域模型和业务规则
  • company-service:服务层,协调多个业务模块完成业务流程
  • company-api:接口层,定义API接口和DTO对象
  • company-web:Web应用层,处理HTTP请求,集成API层

2. 依赖管理策略

  • 版本锁定:所有依赖版本在父POM中统一定义
  • 依赖传递:通过模块层级结构自动传递依赖
  • 依赖排除:在必要时排除冲突的传递依赖
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 构建与发布流程

  1. 版本控制:使用SNAPSHOT表示开发版本,RELEASE表示稳定版本
  2. 构建顺序:按照依赖关系顺序构建模块
  3. 发布策略
    • 开发环境:自动构建SNAPSHOT版本
    • 测试环境:构建特定版本号
    • 生产环境:构建RELEASE版本

4. 多环境配置管理

使用Spring Boot的多环境配置:

company-web/src/main/resources/
├── application.yml        # 公共配置
├── application-dev.yml    # 开发环境配置
├── application-test.yml   # 测试环境配置
└── application-prod.yml   # 生产环境配置

5. 代码质量管理

在父POM中集成代码质量检查插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.2.1</version>
    <configuration>
        <configLocation>checkstyle.xml</configLocation>
        <failOnViolation>true</failOnViolation>
    </configuration>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
</plugin>

四、扩展建议

  1. 集成CI/CD:配置Jenkins流水线实现自动构建、测试和部署
  2. 容器化部署:添加Dockerfile实现容器化部署
  3. 微服务演进:在业务复杂度增长时,可以平滑过渡到微服务架构
  4. API文档自动生成:集成Swagger/OpenAPI自动生成API文档
  5. 性能监控:添加Actuator和Prometheus实现性能监控

这个公司级Maven多模块项目结构遵循了高内聚、低耦合的设计原则,通过合理的模块划分和依赖管理,实现了代码复用、标准化构建和灵活的部署策略,适用于中大型企业级应用的开发和维护。