使用Nexus构建Maven私有仓库最佳实践
- 前提
- 访问nexus
- 本地Maven配置
- 项目Maven配置
- 验证
- 持续集成
前提
在实际的项目开发中,随着项目工程体量增大,开发人员的增加,问题开始暴露出来。功能模块耦合严重,修改代码变得非常困难,代码冲突频发,构建速度变慢…
解决这些问题业内通用的方法是:系统组件化,模块化。将系统拆分成不同的小块,分给不同的开发团队维护。
依赖管理成为系统组件化的前提条件,开发小队将自己负责的组建编译构建出来,上传到maven仓库,供其他项目团队使用。
这样也为持续集成创造了条件。
本文关注的是maven仓库在工程应用中的最佳实践,不涉及maven的基础知识和nexus的安装步骤。
访问nexus
安装nexus后,在浏览器中输入 http://ip:port/nexus 打开nexus
登录nexus
仓库类型
-
hosted
:本地仓库,通常我们会部署自己的构件到这一类型的仓库。比如公司的第二方库 -
proxy
:代理远程的仓库,如Maven官方中央仓库。如果构建的Maven项目本地仓库没有依赖包,那么就会去这个代理站点去下载,那么如果代理站点也没有此依赖包,就回去远程中央仓库下载依赖,代理站点下载成功后再下载至本机 -
group
,仓库组,用来合并多个hosted/proxy仓库
nexus默认会创建一些库:
-
Public Repositorie
:是个仓库组,包含多个仓库,在项目中只需要配置这个组就可以了 -
3rd party
:是存放第三方软件库的地方,它是一个由nexus自己维护的一个仓库 -
Apache Snapshots
:apache的代理仓库 -
Central
:Maven Central Repository的代理仓库 -
Releases
:自己的项目发布release版本在这个仓库,由Nexus维护 -
Snapshots
:自己的项目发布snapshots版本在这个仓库,由Nexus维护
本地Maven配置
开发这希望自己负责的组件能够从自己本机上发布到nexus仓库中,需要对maven做一些配置。
找到maven安装路径下的conf/settings.xml文件
在servers
节点下添加远程服务器验证信息
<server>
<server>
<id>snapshots</id>
<username>nexus用户名</username>
<password>nexus密码</password>
</server>
<server>
<id>releases</id>
<username>nexus用户名</username>
<password>nexus密码</password>
</server>
</servers>
在mirros
节点下添加镜像信息
镜像及将的maven依赖请求转发至相应服务器
<mirrors>
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
<mirror>
<id>mirrorId</id>
<name>nexus</name>
<mirrorOf>repositoryId</mirrorOf>
<url>http://ip:port/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</mirror>
</mirrors>
在repositories
节点下添加其他远程仓库信息
<profiles>
<profile>
<id>mirrorId</id>
<repositories>
<repository>
<id>mirrorId</id>
<name>central</name>
<url>http://ip:port/nexus/content/groups/public/</url>
<layout>default</layout>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>mirrorId</id>
<name>nexus</name>
<url>http://ip:port/nexus/content/groups/public/</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
项目Maven配置
项目Pom文件添加
<distributionManagement>
<repository>
<id>releases</id>
<name>Nexus Release Repository</name>
<url>http://ip:port/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://ip:port/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
验证
在需要打包的组件pom根目录下执行
mvn clean package deploy -Dmaven.test.skip=true -U
持续集成
按照如上说明修改安装jenkins的maven配置,在构建命令中输入命令集成到jenkins上即可