Nexus安装和搭建Maven私服
一、什么是Maven私服?
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当Maven需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载
二、安装Nexus
1)、安装JDK和Maven
参考:https://blog.****.net/qq_40378034/article/details/86685680
2)、下载Nexus
https://www.sonatype.com/download-oss-sonatype
3)、解压
[[email protected] ~]# tar -zxf nexus-3.16.1-02-unix.tar.gz -C /usr/local/
4)、修改配置文件
[[email protected] local]# cd /usr/local/nexus-3.16.1-02/etc/
[[email protected] etc]# ls
fabric jetty karaf logback nexus-default.properties ssl
[[email protected] etc]# vi nexus-default.properties
修改对应端口号
5)、关闭防火墙
启动: systemctl start firewalld
关闭: systemctl stop firewalld
查看状态: systemctl status firewalld
开机禁用 : systemctl disable firewalld
开机启用 : systemctl enable firewalld
6)、启动Nexus
[[email protected] etc]# cd /usr/local/nexus-3.16.1-02/bin/
[[email protected] bin]# ./nexus start
WARING:不建议使用root账户启动这个软件
[[email protected] bin]# useradd nexus
[[email protected] bin]# chown -R nexus:nexus /usr/local/nexus-3.16.1-02/
[[email protected] bin]# chown -R nexus:nexus /usr/local/sonatype-work/
[[email protected] bin]# su nexus
[[email protected] bin]$ nohup ./nexus run &
7)、访问http://IP:8081/
使用默认的账户名、密码登录:admin/admin123
8)、修改ulimit
[[email protected] bin]$ su root
[[email protected] bin]$ vi /etc/security/limits.conf
添加如下配置:
* soft nofile 65536
* hard nofile 65536
重启服务器
9)、修改密码
改为123456
三、Nexus的使用
1)、仓库类型
- proxy:代理仓库,用于代理远程仓库
- group:仓库组,通常包含了多个代理仓库和宿主仓库,在项目中只要引入仓库组就可以下载到代理仓库和宿主仓库中的包
- hosted:宿主仓库,内部项目、付费jar
2)、新建仓库
3)、将刚才新建的仓库加入到仓库组中
4)、配置代理
改为阿里云地址:http://maven.aliyun.com/nexus/content/groups/public/
5)、本地maven配置
修改本地Maven的conf目录中的settings.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>D:\Maven\repository</localRepository>
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>hxt-releases</id>
<username>admin</username>
<password>123456</password>
</server>
<server>
<id>hxt-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
</servers>
<mirrors>
</mirrors>
<profiles>
<profile>
<id>hxt</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<!-- 私有库地址-->
<repositories>
<repository>
<id>hxt</id>
<url>http://192.168.126.159:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<!--插件库地址-->
<pluginRepositories>
<pluginRepository>
<id>hxt</id>
<url>http://192.168.126.159:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--**profile activeProfile与profile中id相对应-->
<activeProfiles>
<activeProfile>hxt</activeProfile>
</activeProfiles>
</settings>
url在Nexus中复制即可
6)、pom.xml添加一个本地没有的依赖进行测试
这里我在项目的pom.xml中添加了如下依赖
<dependency>
<groupId>org.xwiki.commons</groupId>
<artifactId>xwiki-commons-component-api</artifactId>
<version>11.3</version>
</dependency>
然后去Nexus中查看效果如下,证明私服配置成功:
7)、本地项目打包后发到私服
项目的pom.xml中添加配置
<!--本地项目打包后发到私服 id要跟本地maven的setting.xml相同 -->
<distributionManagement>
<repository>
<id>hxt-releases</id>
<name>Ruizhi Release Repository</name>
<url>http://192.168.126.159:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>hxt-snapshots</id>
<name>Ruizhi Snapshot Repository</name>
<url>http://192.168.126.159:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
使用mvn clean deploy命令将本地项目打包后发到私服
因为项目的pom.xml中的版本为SNAPSHOT,所以会发布到maven-snapshots
8)、直接上传jar包到私服