一、多模块项目架构设计
1.1 复杂项目结构设计
enterprise-project/
├── parent-pom/ # 顶层父POM(BOM管理)
├── common/
│ ├── common-core/ # 核心工具类
│ ├── common-utils/ # 工具模块
│ └── common-security/ # 安全模块
├── business/
│ ├── business-api/ # 业务API接口
│ ├── business-service/ # 业务服务实现
│ └── business-dao/ # 数据访问层
├── service/
│ ├── user-service/ # 用户服务
│ ├── order-service/ # 订单服务
│ └── payment-service/ # 支付服务
├── web/
│ ├── web-admin/ # 管理后台
│ ├── web-portal/ # 门户网站
│ └── web-api-gateway/ # API网关
├── infrastructure/
│ ├── config-server/ # 配置中心
│ ├── eureka-server/ # 服务注册中心
│ └── zipkin-server/ # 链路追踪
└── build/ # 构建配置
├── jenkins/ # Jenkins流水线
├── docker/ # Docker配置
└── kubernetes/ # K8s配置
1.2 继承与聚合的深度应用
顶级父POM (parent-pom/pom.xml):
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>
<!-- 统一版本管理 -->
<properties>
<!-- Java版本 -->
<java.version>11</java.version>
<maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-boot.version>2.6.3</spring-boot.version>
<spring-cloud.version>2021.0.1</spring-cloud.version>
<!-- 数据库 -->
<mysql.version>8.0.28</mysql.version>
<mybatis.version>2.2.2</mybatis.version>
<!-- 中间件 -->
<redis.version>3.17.1</redis.version>
<kafka.version>3.0.0</kafka.version>
<!-- 工具 -->
<lombok.version>1.18.22</lombok.version>
<mapstruct.version>1.4.2.Final</mapstruct.version>
<guava.version>31.0.1-jre</guava.version>
</properties>
<!-- 依赖管理:企业级BOM -->
<dependencyManagement>
<dependencies>
<!-- Spring Boot BOM -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- Spring Cloud BOM -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 公司内部依赖管理 -->
<dependency>
<groupId>com.company</groupId>
<artifactId>company-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 构建配置继承 -->
<build>
<pluginManagement>
<plugins>
<!-- 统一编译器配置 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.10.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</path>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<!-- 资源过滤 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<delimiters>
<delimiter>@</delimiter>
</delimiters>
<useDefaultDelimiters>false</useDefaultDelimiters>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<!-- 仓库配置 -->
<repositories>
<repository>
<id>company-nexus</id>
<name>Company Nexus Repository</name>
<url>https://nexus.company.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>fail</checksumPolicy>
</releases>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
<checksumPolicy>warn</checksumPolicy>
</snapshots>
</repository>
</repositories>
<!-- 分发管理 -->
<distributionManagement>
<repository>
<id>company-releases</id>
<name>Company Releases Repository</name>
<url>https://nexus.company.com/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>company-snapshots</id>
<name>Company Snapshots Repository</name>
<url>https://nexus.company.com/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<!-- 报告配置 -->
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.2.2</version>
</plugin>
</plugins>
</reporting>
</project>
1.3 模块间依赖优化
使用dependencyManagement集中管理:
XML
<!-- company-dependencies/pom.xml -->
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>company-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>pom</packaging>
<dependencyManagement>
<dependencies>
<!-- 数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<!-- 工具链 -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<!-- 内部共享库 -->
<dependency>
<groupId>com.company.common</groupId>
<artifactId>common-core</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>
二、高级依赖管理策略
2.1 依赖范围深度解析
XML
<dependencies>
<!-- compile(默认):编译、测试、运行 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<!-- provided:编译、测试时使用,运行时由容器提供 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- runtime:运行时需要,编译时不需要 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- test:仅测试使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- system:系统依赖 -->
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.8</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
<!-- import:导入BOM -->
<dependency>
<groupId>com.company</groupId>
<artifactId>company-dependencies</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
2.2 依赖传递与冲突解决
XML
<!-- 精确控制依赖传递 -->
<dependency>
<groupId>com.example</groupId>
<artifactId>complex-library</artifactId>
<version>1.0.0</version>
<!-- 排除特定传递依赖 -->
<exclusions>
<exclusion>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
</exclusion>
<exclusion>
<groupId>*</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
<!-- 可选依赖:不强制传递 -->
<optional>true</optional>
</dependency>
2.3 依赖锁定文件
maven-lockfile插件:
XML
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-lockfile-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<lockFileName>maven-lockfile.xml</lockFileName>
<includeTransitive>true</includeTransitive>
<excludedScopes>
<excludedScope>test</excludedScope>
<excludedScope>provided</excludedScope>
</excludedScopes>
</configuration>
<executions>
<execution>
<id>generate-lockfile</id>
<phase>package</phase>
<goals>
<goal>generate-lockfile</goal>
</goals>
</execution>
</executions>
</plugin>
三、高级构建配置
3.1 Profile策略设计
XML
<!-- 多环境配置 -->
<profiles>
<!-- 开发环境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
<property>
<name>env</name>
<value>dev</value>
</property>
</activation>
<properties>
<env.type>dev</env.type>
<logging.level>DEBUG</logging.level>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application-${env.type}.yml</include>
<include>application.yml</include>
</includes>
</resource>
</resources>
</build>
</profile>
<!-- 测试环境 -->
<profile>
<id>test</id>
<activation>
<property>
<name>env</name>
<value>test</value>
</property>
</activation>
<properties>
<env.type>test</env.type>
<logging.level>INFO</logging.level>
<spring.profiles.active>test</spring.profiles.active>
</properties>
<dependencies>
<!-- 测试环境特有依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
</profile>
<!-- 生产环境 -->
<profile>
<id>prod</id>
<activation>
<property>
<name>env</name>
<value>prod</value>
</property>
</activation>
<properties>
<env.type>prod</env.type>
<logging.level>WARN</logging.level>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
<build>
<plugins>
<!-- 生产环境压缩优化 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<!-- Docker构建Profile -->
<profile>
<id>docker</id>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
<executions>
<execution>
<id>default</id>
<phase>install</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>push</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
3.2 自定义资源过滤
XML
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<excludes>
<exclude>**/*.jks</exclude>
<exclude>**/*.p12</exclude>
</excludes>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<!-- 属性替换配置 -->
<filters>
<filter>src/main/filters/filter-${env.type}.properties</filter>
</filters>
<!-- 测试资源 -->
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>