文章目录
前言
我们在使用maven时,在pom.xml文件里面有个repositories的配置,这个配置可以来指定我们的依赖包下载的地址,我们常用的比如有阿里云镜像、华为云镜像地址等。这些地址能给我们提供更好的网络访问,但是当有时我们也需要自己的镜像库,比如我们自己开发的一个工具库只提供给公司内部使用,而又不想共享到公网上去,那这时就需要我们自己搭建一套maven的私有库。或者我们的开发环境下是不能上外网,只能链接公司的内网,这种情况也就需要我们的私有库。今天给大家分享如何大家自己的maven私有库。
一、nexus下载与安装
nexus的全称是Sonatype Nexus Repository这个貌似只是他们产品线上的一个,Sonatype的产品更新很快,各种文档的地址也变更很快,这里建议大家先进入官网文档中心,一步一步点过去,Nexus官网点击这里。
1、本地安装Sonatype Nexus Repository
如果要进行本地安装,Nexus需要jdk8的环境,请确保本机已安装jdk8,并且只能是jdk8,当然如果你的机器本地环境不是jdk8,也有解决方案,看下面。
第一步
第二步
然后选择合适平台版本进行下载,这里我们下载Linux版本,也就是Unix archive,下载完成后,然后进行解压:
mkdir -p /opt/nexus
tar -xzvf nexus-3.64.0-04-unix.tar.gz -C /opt/nexus
配置Java环境
如果你本机的环境是不jdk8,那么你需要去下载jdk8的开发包,然后解压的某个目录,然后修改nexus的启动文件:
vim /opt/nexus/nexus-3.64.0-04/bin/nexus
在文件最上面增加INSTALL4J_JAVA_HOME_OVERRIDE,添加一行:
INSTALL4J_JAVA_HOME_OVERRIDE=/usr/java/jdk1.8.0_77
我这里是单独配置的jdk。将目录改成你自己的目录即可
创建启动用户
默认情况下nexus是不能用root用户启动的,当我们使用root用户启动时会报错:
WARNING: ************************************************************
WARNING: Detected execution as "root" user. This is NOT recommended!
WARNING: ************************************************************
我们需要创建一个用户来专门运行nexus
创建用户:
useradd nexus && passwd nexus
添加一个nexus的用户并设置好密码,然后我们还有修改一下vim /etc/security/limits.conf这个文件
vim /etc/security/limits.conf
在文件末尾加上:
nexus - nofile 65536
赋予权限:
chmod -R 755 /opt/nexus/
chown -R nexus:nexus /opt/nexus/
切换用户:
su - nexus
运行nexus,有两种方式来运行,第一种是前台运行,可以查看nexus的执行情况:
/opt/nexus/nexus-3.64.0-04/bin/nexus run
在没有异常情况发生时,可以切换到后台运行:
/opt/nexus/nexus-3.64.0-04/bin/nexus start
查看nexus状态:
/opt/nexus/nexus-3.64.0-04/bin/nexus status
制作系统服务
上面是通过命令来启动,还是稍显麻烦,下面我们把nexus做成系统服务,这样就可以很方便操作,我们在/etc/init.d/下创建一个nexus的文件:
cat > /etc/init.d/nexus << EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides: nexus
# Required-Start: \$local_fs \$network
# Required-Stop: \$local_fs \$network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: nexus
### END INIT INFO
start() {
echo "Starting nexus..."
su - nexus -c "/opt/nexus/nexus-3.64.0-04/bin/nexus start"
}
stop() {
echo "Stoping nexus..."
su - nexus -c "/opt/nexus/nexus-3.64.0-04/bin/nexus stop"
}
restart() {
echo "Restarting nexus..."
su - nexus -c "/opt/nexus/nexus-3.64.0-04/bin/nexus restart"
}
status() {
echo "Status nexus..."
su - nexus -c "/opt/nexus/nexus-3.64.0-04/bin/nexus status"
}
reload() {
echo "Reloading nexus..."
su - nexus -c "/opt/nexus/nexus-3.64.0-04/bin/nexus force-reload"
}
case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
reload)
reload
;;
*)
echo "Usage: \$0 {start|stop|restart|status|reload}"
exit 1
;;
esac
exit 0
EOF
修改执行权限:
chmod +x /etc/init.d/nexus
然后我们就可以使用这些命令了:
service nexus start
service nexus stop
service nexus restart
service nexus status
service nexus reload
至此Linux本地安装已经完成,
2、docker安装Sonatype Nexus Repository
在docker中安装nexus相比在本地安装就非常简单了,唯一前提就是要安装docker环境,关于docker的安装请参考
我的文章这里给出了一键安装docker的脚本可以参照。
到dockerHub上寻找镜像文件,这里我们选择一个版本:sonatype/nexus3:3.64.0
首先我们要创建一个目录,用来挂载nexus的数据
mkdir -p /usr/local/nexus/data
然后执行下面命令:
docker run -d --restart=always --privileged -u root -p 8081:8081 -v /usr/local/nexus/data:/nexus-data -v /etc/localtime:/etc/localtime --name nexus3 sonatype/nexus3:3.64.0
至此docker安装就完成了。
二、nexus配置说明
1、启动端口修改
如果我们是在本地运行,修改端口的文件位置在:
vim /opt/nexus/nexus-3.64.0-04/etc/nexus-default.properties
如下所示:
application-port=8081
application-host=0.0.0.0
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/
将里面的8081修改成你要的端口就行。
如果在docker环境中,将本地和docker映射的端口修改即可:
-p 18081:8081
这样就把端口改成18081了
2、后台配置
启动成功后,我们访问:http://ip:port比如我这里是:http://192.168.101.170:8081点击登录:
初次登录,这里都会提示默认密码文件存放的地址,默认用户:admin
我们查看密码:
cat /usr/local/nexus/data/admin.password
需要注意的是,如果是用docker环境启动,他这里是docker容器中的路径,我们把他容器数据目录挂载到了本机,在本机中绝对路径就是上面的/usr/local/nexus/data/
输入用户名,初始密码登录后,会要求你修改密码,修改密码后,进入如下:
选择:Disable anonymous access
然后进入到首页如下:
这里除了maven,还有nuget,我们这里先只看maven部分,
nexus仓库的概念:仓库有三种类型,分别是hosted、proxy、group。
hosted类型:其实就是专门来管理我们自己的jar包的,而且系统默认分了snapshots和release版本,比如我们自己编写的库文件,就可以上传到这里来。
proxy类型:代理仓库,就是当在我们的项目中使用了的jar包,在nexus上找不到是,他就通过代理仓库去查找,代理仓库可以配置代理地址,系统默认是maven的:https://repo1.maven.org/maven2 可以把这里修改成阿里云或者其他的地址,但一般我们不去修改,因为nexus通过代理服务器去拉取一次后就会保存到本地,下次直接去本地拉取。
group类型:这个类型其实就是我们要发布出去的仓库地址,这里可以把签名的proxy和hosted类型的全部添加进去,项目在获取依赖库的时候,nexus就按照我们添加的顺序依次去查找
这里nexus已经给我们初始化了这几个,我们就直接用他的,还有我们自己也可以去创建新的仓库类型,比如对应Android平台来说,jcenter需要加进去。如下:
选择类型:
然后完善信息,jcenter地址如下:https://jcenter.bintray.com/
点击保存,就创建结束
最后我们需要把这个maven-jcenter加入到我们的group仓库中:
点击如图所示:
把jcenter加入到右边
至此我们配置就完成了,下面我们进入测试
三、在项目中使用
在初始化的时候,我们设置了Disable anonymous access,表示这个私有库是不允许匿名访问和使用的,因此我们要配置用户,在maven的settings.xml中我们进行如下配置:
在servers标签里面添加一个server:
<server>
<id>user-release-nexus</id>
<username>admin</username>
<password>123456</password>
</server>
然后我们进入页面,我们点击group仓库的copy按钮:
这个地址就是我们使用的地址,现在我们来创建一个项目试试,创建一个springboot项目,在pom.xml加入下面配置:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>user-release-nexus</id>
<name>test-nexus</name>
<url>http://192.168.101.170:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
请注意,这里的ID必须要和settings.xml中的server的id一致,不然是无法使用的。如果不想管理员被大面积使用,可以为每个用户创建一个nexus用户。
下面我们运行打包命令:
mvn clean -DskipTests package
看到如下结果:
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< org.example:test-nexus >-----------------------
[INFO] Building test-nexus 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from test-nexus: http://192.168.101.170:8081/repository/maven-public/org/springframework/spring-webmvc/5.3.31/spring-webmvc-5.3.31.jar
Downloading from test-nexus: http://192.168.101.170:8081/repository/maven-public/org/springframework/boot/spring-boot-starter-data-jpa/2.7.18/spring-boot-starter-data-jpa-2.7.18.jar
Downloading from test-nexus: http://192.168.101.170:8081/repository/maven-public/org/springframework/spring-expression/5.3.31/spring-expression-5.3.31.jar
Downloading from test-nexus: http://192.168.101.170:8081/repository/maven-public/org/apache/tomcat/embed/tomcat-embed-core/9.0.83/tomcat-embed-core-9.0.83.jar
说明它已经去我们配置的nexus上去下载依赖了
总结
1、Sonatype Nexus
Repository的功能很强大,除了能当做maven私有库来使用,还可以当初npm/docker等的私有库,后续再给大家分享怎么使用。
2、搭建私有库对公司内部研发管理还是有一定帮助,至少能统一进行外部库管理,另外如果公司内部有自己的开发框架,可以上传到私有库中,这对公司的知识也是一种保护。
3、后续将给大家分享,如何一键部署自己的私有库以及maven自定义插件的开发。希望大家关注点赞。