当前位置: 首页 > news >正文

Maven模块化开发与设计笔记

1. 模块化开发

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

2. 聚合模块

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

示例代码

父模块 pom.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

<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

<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

<properties><project.basedir>${project.basedir}</project.basedir>
<properties>

子模块 module-a/pom.xml

<build><resources><resource><directory>${project.basedir}/src/main/resources</directory><filtering>false</filtering></resource></resources>
</build>

5. 版本管理

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

父模块 pom.xml

<dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>${spring.version}</version></dependency></dependencies>
</dependencyManagement>

6. 多环境配置

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

父模块 pom.xml

<profiles><profile><id>dev</id><properties><active.profile>dev</active.profile></properties></profile>
</profiles>

子模块 application-dev.yml

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

<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

<servers><server><id>nexus-releases</id><username>admin</username><password>admin123</password></server>
</servers>

pom.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

<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 提供的多环境配置、跳过测试、私服配置等功能,使得项目可以更好地适应不同的开发和运行环境。

http://www.dtcms.com/a/308489.html

相关文章:

  • 深入解析 Spring AI 系列:剖析OpenAI接口接入组件
  • WEditor:高效的移动端UI自动化脚本可视化编辑器
  • Vibe Coding:AI驱动开发的安全暗礁与防护体系
  • MySql 知识大汇总
  • 架构实战——架构重构内功心法第三式(运筹帷幄)
  • 行业热点丨仿真历史数据难以使用?如何利用几何深度学习破局,加速汽车工程创新
  • Ubuntu 18.04 repo sync报错:line 0: Bad configuration option: setenv
  • 三维火灾调查重建:科技赋能,探寻真相
  • 网络安全-同形异义字攻击:眼见并非为实(附案例详解)
  • 什么是 MySQL 的慢查询日志?如何优化慢查询?
  • FastAPI docs接口文档打不开怎么解决
  • 活到老学到老之AES加密
  • CentOS 7 上使用 Docker 安装 Jenkins 完整教程
  • 有公网ip还要端口映射不?只有内网ip怎么做映射端口到外网访问?
  • Electron 作品【AI聊天】桌面应用 —— 系列教程(含开源地址)
  • 守护金融核心业务 | 博睿数据《金融业务全景与全链路智能可观测体系建设白皮书》发布!
  • ORACLE基本DML操作
  • ShimetaPi M4-R1:国产高性能嵌入式平台的异构计算架构与OpenHarmony生态实践
  • 如何在 Ubuntu 24.04 或 22.04 LTS 上安装 OpenShot 视频编辑器
  • 【支持Ubuntu22】Ambari3.0.0+Bigtop3.2.0——Step6—本地apt源
  • MELF电阻的原理,特性和应用
  • 视觉图像处理中级篇 [2]—— 外观检查 / 伤痕模式的原理与优化设置方法
  • 从入门到精通:Git全面指南(下)
  • 【01】大恒相机SDK C++开发 —— 初始化相机,采集第一帧图像、回调采集、关闭相机
  • R语言空间分析、模拟预测与可视化
  • 垂直元素均匀分布
  • 【第四章自定义编辑器窗口_Game窗口中的GUI_运行时控制台窗口(10/12)】
  • 深度解析领域特定语言(DSL)第七章:语法分析器组合子 - 用乐高思维构建解析器
  • go2sky的封装及使用
  • LeetCode 刷题【23. 合并 K 个升序链表】