Spring Boot 多模块与 Maven 私有仓库
前言
系统复杂了,抽离单一职责的模块几乎是必须的;若需维护多个项目,抽离公用包上传私有仓库管理也几乎是必须的。其优点无需赘述,以下将记录操作过程。

1. 多模块拆分
在.NET 中由于其统一性,实现上更自然一点。Spring Boot 通过 Maven 构建多模块工程也不麻烦,假如我的项目中包含以下几个包:

我需要将他们分别拆分成独立模块,首先要修改的是根目录下的 pom.xml,packaging 类型改为 pom,并添加 modules 节点:
<?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.youclk.multi-package</groupId>
<artifactId>parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<modules>
<module>api</module>
<module>service</module>
<module>dao</module>
</modules>
<packaging>pom</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<lombok>1.16.20</lombok>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok}</version>
</dependency>
</dependencies>
</project>
之后新建一个个 Module,将对应的代码移植过去:

需要注意的是在启动模块的 pom.xml 中需要指定启动类:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.youclk.multipackage.api.MultiApplication</mainClass>
<layout>ZIP</layout>
</configuration>
</plugin>
</plugins>
</build>
统一升级版本命令:mvn versions:set -DnewVersion=0.0.1-SNAPSHOT,到此差不多完成了,引用方式与普通的依赖包一致:
<dependency>
<groupId>com.youclk.multi-package</groupId>
<artifactId>service</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
补充
外层 pom :
<?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.youclk</groupId>
<artifactId>et2</artifactId>
<packaging>pom</packaging>
<version>${own-project.version}</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>
<module>start</module>
<module>service</module>
<module>client</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>
<own-project.version>1.0.0</own-project.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.youclk</groupId>
<artifactId>start</artifactId>
<version>${own-project.version}</version>
</dependency>
<dependency>
<groupId>com.youclk</groupId>
<artifactId>service</artifactId>
<version>${own-project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
内层 pom
<?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>
<parent>
<groupId>com.youclk</groupId>
<artifactId>et2</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>service</artifactId>
<packaging>jar</packaging>
<version>${own-project.version}</version>
</project>
<?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>
<parent>
<groupId>com.youclk</groupId>
<artifactId>et2</artifactId>
<version>1.0.0</version>
</parent>
<artifactId>start</artifactId>
<packaging>jar</packaging>
<version>${own-project.version}</version>
<dependencies>
<dependency>
<groupId>com.youclk</groupId>
<artifactId>service</artifactId>
</dependency>
</dependencies>
</project>

注意 start 模块下的包结构为根结构,不要加 .start,否则要指定需要扫描的包。
2. Nexus3 私有仓库搭建
Docker 时代一切都变得异常简单,Compose 配置如下:
version: '3.5'
services:
nexus:
image: sonatype/nexus3:3.10.0
networks:
- proxy
- youclk
volumes:
- /mnt/nas/db/nexus-data:/nexus-data
deploy:
mode: replicated
labels:
- com.df.notify=true
- com.df.port=8081
- com.df.serviceDomain=nexus.youclk.com
restart_policy:
condition: any
max_attempts: 3
update_config:
delay: 5s
order: start-first
resources:
limits:
cpus: '0.50'
memory: 1g
networks:
proxy:
external: true
youclk:
external: true
启动过程需要一分钟左右:


需要注意的是如果你的 ssl 是在负载均衡或者其他的反向代理之上,那么必须在 HTTP 头中指定 X-Forwarded-Proto 传输协议为 HTTPS,然后,就可以愉快地玩耍了。
3. 上传与引用
3.1 上传
首先需要在 Nexus 创建私有仓库,例如我的:


其次在本地 maven 设置中添加 server 节点,默认在 ~/.m2/settings.xml:
<servers>
<server>
<id>youclk</id>
<username>admin</username>
<password>youclk</password>
</server>
</servers>
pom.xml 中添加上传地址:
<distributionManagement>
<repository>
<id>nexus</id>
<name>Releases Repository</name>
<url>https://nexus.youclk.com/repository/youclk-releases/</url>
</repository>
<snapshotRepository>
<id>nexus</id>
<name>Snapshot Repository</name>
<url>https://nexus.youclk.com/repository/youclk-snapshots/</url>
</snapshotRepository>
</distributionManagement>
最后执行 mvn clean deploy 便会上传至私有仓库,单独传包命令如下:
mvn deploy:deploy-file -DgroupId=com.youclk -DartifactId=utils -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=target/utils-0.0.1-SNAPSHOT.jar -Durl=https://nexus.youclk.com/repository/youclk/ -DrepositoryId=youclk
管理和查看:

3.1 引用
Finally,最后的最后就是怎么使用啦~ 如果需要全局引用的话需要在 settings.xml 添加和激活仓库:
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<mirrors>
<mirror>
<id>aliyun</id>
<mirrorOf>central</mirrorOf>
<name>central mirror</name>
<url>http://maven.aliyun.com/mvn/repository</url>
</mirror>
<mirror>
<id>nexus</id>
<mirrorOf>maven-public</mirrorOf>
<name>private mirror</name>
<url>http://local-nexus.youclk.com/repository/maven-public/</url>
</mirror>
</mirrors>
<servers>
<server>
<id>nexus</id>
<username>admin</username>
<password>youclk</password>
</server>
</servers>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>maven</id>
<name>local private nexus</name>
<url>http://local-nexus.youclk.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>maven</id>
<name>local private nexus</name>
<url>http://local-nexus.youclk.com/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>
不过一般不推荐这么写,settings.xml 应该尽可能保持简洁,精简配置,此处留下代理和权限认证即可,其余的可以移植到 pom.xml 中:
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
<repository>
<id>nexus</id>
<url>http://local-nexus.youclk.com/repository/maven-public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>central</id>
<url>
http://maven.aliyun.com/nexus/content/groups/public/
</url>
</pluginRepository>
<pluginRepository>
<id>maven-public</id>
<url>http://local-nexus.youclk.com/repository/maven-public/</url>
</pluginRepository>
</pluginRepositories>
小结
我的公众号《有刻》,我们共同成长!

Spring Boot 多模块与 Maven 私有仓库的更多相关文章
- Maven 搭建spring boot多模块项目(附源码),亲测可以,感谢原创
原创地址:https://segmentfault.com/a/1190000005020589 我的DEMO码云地址,持续添加新功能: https://gitee.com/itbase/Spring ...
- Maven 搭建spring boot多模块项目
Maven 搭建spring boot多模块项目 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom ...
- 实战maven私有仓库三部曲之一:搭建和使用
在局域网内搭建maven私有仓库,可避免每次都从中央仓库下载公共jar包,另外将A模块作为二方库发布到私有仓库后,B模块可以很方便的引用,今天我们就来实战maven私有仓库的搭建和使用: 原文地址:h ...
- 使用Nexus配置Maven私有仓库
使用Nexus配置Maven私有仓库 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装配置Nexus 1>.下载nexus 下载地址:https://www.sonat ...
- Spring Boot 多模块项目创建与配置 (一) (转)
Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...
- Spring Boot 多模块项目创建与配置 (一)
最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...
- 安装Maven并搭建Maven私有仓库
一.说明 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具.我们在进行Java代码开发的时候,Eclipse+Maven+Jetty是一个十 ...
- 【图文并茂】 做开发这么久了,还不会搭建服务器Maven私有仓库?这也太Low了吧
大家好,我是冰河~~ 最近不少小伙伴想在自己公司的内网搭建一套Maven私服环境,可自己搭建的过程中,或多过少的总会出现一些问题,问我可不可以出一篇如何搭建Maven私服的文章.这不,就有了这篇文章嘛 ...
- Spring Boot 多模块项目创建与配置 (转)
转载:https://www.cnblogs.com/MaxElephant/p/8205234.html 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多 ...
随机推荐
- Oracle使用游标查询指定数据表的所有字段名称组合而成的字符串
应用场合:参考网上查询数据表的所有字段名代码,使用游标生成指定单个表的所有字段名跟逗号组成的用于select 逗号隔开的字段名列表 from字符串等场合. 查询结果输出如下: 当前数据表TB_UD_ ...
- Nginx模块之SessionSticky
0 工作原理 Session Sticky 模块在upstream 返回响应后,向客户的浏览器写入 Cookie ,默认名为route ,保存的内容是一个 md5 码. 之后,模块接收到客户浏览器的请 ...
- Cocos2D中Node的userObject实例变量使用时一个要注意的地方
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在Cocos2D中,CCNode对象有一个ivar为us ...
- 【一天一道LeetCode】#136. Single Number
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- SDL2源代码分析7:显示(SDL_RenderPresent())
===================================================== SDL源代码分析系列文章列表: SDL2源代码分析1:初始化(SDL_Init()) SDL ...
- native2ascii命令
native2ascii命令 native2ascii的命令行的命名格式: native2ascii -[options] [inputfile [outputfile]] 说明: ...
- oracle ebs应用产品安全性-定义访问权限集
定义 定义访问权限集是一项分配至责任层的可选的安全功能,是对Oracle 11i应用产品弹性域安全性定义的功能扩展,对总帐管理模块的一些内容进行安全性定义和权限分配的集合,以控制不同的责任对一些内容的 ...
- 动态创建VIEW
很多人都应该知道 global temporary table 的用法,这里也提出一个动态VIEW的用法,在实际过程中有着很好的独特之处 具体如下: /***************创建PACKAGE ...
- Linux0.11中对文本文件进行修改的策略
现在,假设 hello.txt 是硬盘上已有的一个文件,而且内容为 "hello, world" ,在文件的当前指针设置完毕后,我们来介绍 sys_read , sys_write ...
- 《java入门第一季》之面向对象(包概述)
由于eclipse等ide的强大功能,使得建包,导包用一些快捷键就能完成.这里对包的概念做稍微的叙述,了解即可: 分包后使得项目更加清晰,提高代码维护性. 包: A:其实就是文件夹 ...