一.Maven私服简介
Maven私服是Maven仓库的一种,Maven仓库有三种,分别是本地仓库,远程仓库(私服),中央仓库。
Maven私服一般都是由公司或团体为方便内部使用而搭建的,一般部署在局域网中,所以在局域网中使用私服下载jar包的速度比中央仓库方便。而且公司或组织的不方便公开的项目部署在私服中也比较安全。正因为这些优点,私服在企业中的应用非常广泛
二.Maven私服搭建
2.1 下载nexus
从官网下载nexus的最新版本,nexus是用Java语言开发的用来搭建Maven私服的一款BS结构的软件,即nexus是通过浏览器来管理jar包的
如果官网下载速度过慢,也可以在CSDN上下载nexus下载
解压nexus的压缩包,其内容如下,我是用的是nexus的2.12版本
如上图,进入nexus的bin目录下,使用管理员方式打开命令行,因为nexus在安全方面做了很多工作,所以安装使用必须要有管理员权限
2.2 运行命令安装软件并开启服务
nexus.bat install命令
安装nexus
nexus.bat start命令
开启nexus服务
2.3 使用浏览器访问nexus
打开浏览器,输入地址http://localhost:8081/nexus/ (nexus的默认端口是8081),登录nexus,点击右上角的login(nexus有一个默认管理员:用户名admin,密码admin123),登录成功后如下图所示,即Maven私服的管理界面
三.Maven私服使用
3.1 将Maven项目打包并上传到到Maven私服
3.1.1 修改Maven配置文件
在Maven的配置文件setting.xml中配置管理员用户名和密码。
在<servers>
标签下添加如下配置:
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
3.1.2 修改项目配置文件pom.xml
添加Maven私服的相关信息
<distributionManagement>
<repository>
<id>releases</id>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
3.1.3 发布项目到私服
只用执行Maven命令deploy即可完成项目的打包和发布等一系列动作
3.2 从Maven私服下载jar包
如何从Maven私服自动下载所需的依赖文件呢?需要进行一些配置
修改Maven配置文件
在Maven的配置文件setting.xml中配置Maven私服的相关信息
在<profile>
标签下添加如下配置:
<profile>
<!--profile的id-->
<id>dev</id>
<repositories>
<repository>
<!--仓库id,repositories可以配置多个仓库,保证id不重复-->
<id>nexus</id>
<!--仓库地址,即nexus仓库组的地址-->
<url>http://localhost:8081/nexus/content/groups/public/</url>
<!--是否下载releases构件-->
<releases>
<enabled>true</enabled>
</releases>
<!--是否下载snapshots构件-->
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!-- 插件仓库,maven的运行依赖插件,也需要从私服下载插件 -->
<pluginRepository>
<!-- 插件仓库的id不允许重复,如果重复后边配置会覆盖前边 -->
<id>public</id>
<name>Public Repositories</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
</profile>
<activeProfiles>
<activeProfile>dev</activeProfile>
</activeProfiles>