Maven模块化开发与设计笔记

1. 模块化开发

模块化开发是将大型应用程序拆分成多个小模块的过程,每个模块负责不同的功能。这有助于降低系统复杂性,提高代码的可维护性和可扩展性。

2. 聚合模块

聚合模块(父模块)用于组织和管理多个子模块。它定义了项目的全局配置,如依赖、插件等,使得子模块可以继承这些配置。

示例代码

父模块 pom.xml

XML 复制代码
<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>parent-module</artifactId>
    <packaging>pom</packaging>

    <modules>
        <module>module-a</module>
        <module>module-b</module>
    </modules>

    <properties>
        <java.version>1.8</java.version>
        <spring.version>5.3.10</spring.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!-- 定义版本号,子模块继承后无需再定义版本号 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

子模块 module-a/pom.xml

XML 复制代码
<project>
    <parent>
        <groupId>com.example</groupId>
        <artifactId>parent-module</artifactId>
    </parent>

    <modelVersion>4.0.0</modelVersion>
    <artifactId>module-a</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

3. Maven 插件管理

在父模块中定义 <pluginManagement>,子模块可以引用这些插件,而不需要重复定义插件版本。

父模块 pom.xml

XML 复制代码
<pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>2.4.5</version>
        </plugin>
    </plugins>
</pluginManagement>

子模块将自动继承这些插件配置。

4. 自定义属性

在父模块中定义 <properties> 标签,可以在所有子模块中使用这些属性。

父模块 pom.xml

XML 复制代码
<properties>
    <project.basedir>${project.basedir}</project.basedir>
<properties>

子模块 module-a/pom.xml

XML 复制代码
<build>
    <resources>
        <resource>
            <directory>${project.basedir}/src/main/resources</directory>
        <filtering>false</filtering>
        </resource>
    </resources>
</build>

5. 版本管理

在父模块中统一管理依赖的版本号。

父模块 pom.xml

XML 复制代码
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</dependencyManagement>

6. 多环境配置

通过定义不同的配置文件,可以适应不同的运行环境。

父模块 pom.xml

XML 复制代码
<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <active.profile>dev</active.profile>
        </properties>
    </profile>
</profiles>

子模块 application-dev.yml

XML 复制代码
server:
  port: 8081
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/dev_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false

7. 跳过测试

在 Maven 中,可以通过配置 maven-surefire-plugin 来跳过测试。

父模块 pom.xml

XML 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-sure-plugin</artifactId>
            <configuration>
                <skipTests>true</skipTests>
            </configuration>
        </plugin>
    </plugins>
</build>

8. 私服配置

在 Maven 中配置私服(如 Nexus)可以方便管理依赖和插件。

settings.xml

XML 复制代码
<servers>
    <server>
        <id>nexus-releases</id>
        <username>admin</username>
        <password>admin123</password>
    </server>
</servers>

pom.xml

XML 复制代码
<repositories>
    <repository>
        <id>nexus-releases</id>
        <url><http://localhost:8081/nexus/content/repositories/releases/></url>
    </repository>
<repositories>

9. 资源上传

将构建的 JAR 文件上传到私服,可以在 pom.xml 中配置 maven-deploy-plugin 插件。

pom.xml

XML 复制代码
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-deploy-plugin</artifactId>
            <configuration>
                <repositoryId>nexus-releases</repositoryId>
                <url><http://localhost:8081/nexus/content/repositories/releases/></url>
            </configuration>
        </plugin>
    </plugins>
</build>

总结

模块化开发和 Maven 的使用可以提高项目的可维护性和可扩展性。通过聚合和继承,可以简化项目的构建和配置管理。同时,Maven 提供的多环境配置、跳过测试、私服配置等功能,使得项目可以更好地适应不同的开发和运行环境。

相关推荐
ka2x4 小时前
Mac 电脑 IDEA 执行 Maven 出现 No route to host 问题
macos·maven·intellij-idea
admiraldeworm5 小时前
Spring Boot + Spring AI 最小可运行 Demo
java·人工智能·ai
chenglin0165 小时前
ES_数据存储知识
java·服务器·elasticsearch
fs哆哆6 小时前
在VB.net中一维数组,与VBA有什么区别
java·开发语言·数据结构·算法·.net
johnZhangqi6 小时前
深圳大学-计算机信息管理课程实验 C++ 自考模拟题
java·开发语言·c++
David爱编程6 小时前
并发编程三大特性全解析:原子性、可见性、有序性,一文讲透!
java·后端
Sally璐璐6 小时前
Go语言变量声明与初始化详解
java·开发语言·golang
C4程序员7 小时前
北京JAVA基础面试30天打卡14
java·开发语言·面试
LGL6030A8 小时前
Java学习历程14——制作一款五子棋游戏(4)
java
qq_5895681010 小时前
javaweb开发笔记—— 前端工程化
java·前端