Fork me on GitHub

Jenkins之Nexus搭建Maven私有仓库

在开发过程中,有时候会使用到公司内部的一些开发包,显然把这些包放在外部是不合适的。另外,由于项目一直在开发中,这些内部的依赖可能也在不断的更新。可以通过搭建公司内部的Maven服务器,将第三方和内部的依赖统一管理,同时也可以节省网络带宽,当然前提是项目所需要的构件在私服中已经存在。如何在公司内部部署一个自己的Maven私有仓库呢?

私服介绍

私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
搭建私服前后Maven工作流程:


可以使用专门的Maven仓库管理软件来搭建私服,比如:Apache Archiva,Artifactory,Sonatype Nexus。这里我们使用 Sonatype Nexus。

安装Nexus

安装JDK

openjdk比较精简,会出现各种奇葩问题,故使用Oracle JDK版本
下载地址 下载jdk-8u151-linux-x64.rpm

1
2
3
4
5
6
7
8
9
10
11
# rpm -ivh jdk-8u151-linux-x64.rpm
# vim /etc/profile
# JAVA bin PATH setup
export JAVA_HOME=/usr/java/jdk1.8.0_151/
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
# source /etc/profile
# java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

安装Maven

下载地址下载apache-maven-3.5.2-bin.tar.gz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# wget http://mirrors.hust.edu.cn/apache/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
# tar -zxf apache-maven-3.5.2-bin.tar.gz
# mv apache-maven-3.5.2
# mv apache-maven-3.5.2 /usr/local/
# ln -s apache-maven-3.5.2/ maven
# vim /etc/profile
export M2_HOME=/usr/local/maven
export PATH=$PATH:$M2_HOME/bin
# source /etc/profile
# mvn -version
Apache Maven 3.5.2 (138edd61fd100ec658bfa2d307c43b76940a5d7d; 2017-10-18T03:58:13-04:00)
Maven home: /usr/local/maven
Java version: 1.8.0_151, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.10.0-327.el7.x86_64", arch: "amd64", family: "unix"

安装Nexus

下载地址 下载版本nexus-2.12.0-01-bundle.tar.gz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# wget http://download.sonatype.com/nexus/oss/nexus-2.12.0-01-bundle.tar.gz
# tar -zxf nexus-2.12.0-01-bundle.tar.gz
# cp -r nexus-2.12.0-01 /usr/local/
# cd /usr/local/
# ln -s nexus-2.12.0-01/ nexus
# cd /usr/local/nexus/bin/
# ./nexus start
****************************************
WARNING - NOT RECOMMENDED TO RUN AS ROOT
****************************************
If you insist running as root, then set the environment variable RUN_AS_USER=root before running this script.
# vim /usr/local/nexus/bin/nexus
RUN_AS_USER=root
# ./nexus start # 重启启动
# tail -f /usr/local/nexus/logs/wrapper.log
.........
jvm 1 | 2017-12-26 01:44:44,958-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.rest.NexusApplication_UriMatching - Strict URI matching: true
jvm 1 | 2017-12-26 01:44:45,163-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.configuration.ModelUtils - Loading model /usr/local/sonatype-work/nexus/conf/lvo-plugin.xml
jvm 1 | 2017-12-26 01:44:45,171-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.configuration.ModelUtils - Loading model /usr/local/sonatype-work/nexus/conf/ldap.xml
jvm 1 | 2017-12-26 01:44:45,808-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.webresources.internal.WebResourceServiceImpl - Discovered 1971 resources
jvm 1 | 2017-12-26 01:44:45,809-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.webresources.internal.WebResourceServlet - Max-age: 30 days (2592000 seconds)
jvm 1 | 2017-12-26 01:44:45,858-0500 INFO [ar-4-thread-1] *SYSTEM org.sonatype.nexus.proxy.storage.remote.httpclient.HttpClientRemoteStorage - Initializing remote transport for proxy repository "Apache Snapshots" [id=apache-snapshots]...
jvm 1 | 2017-12-26 01:44:46,461-0500 INFO [ar-4-thread-3] *SYSTEM org.sonatype.nexus.proxy.storage.remote.httpclient.HttpClientRemoteStorage - Initializing remote transport for proxy repository "Central" [id=central]...
jvm 1 | 2017-12-26 01:44:46,522-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.InstrumentedSelectChannelConnector - Metrics enabled
jvm 1 | 2017-12-26 01:44:46,545-0500 INFO [jetty-main-1] *SYSTEM org.eclipse.jetty.server.AbstractConnector - Started [email protected]:8081
jvm 1 | 2017-12-26 01:44:46,545-0500 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - Running
jvm 1 | 2017-12-26 01:44:46,545-0500 INFO [WrapperListener_start_runner] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer - Started
jvm 1 | 2017-12-26 01:44:47,543-0500 INFO [ar-4-thread-3] *SYSTEM org.sonatype.nexus.proxy.maven.routing.internal.RemoteContentDiscovererImpl - Remote strategy prefix-file on M2Repository(id=central) detected invalid input, results discarded: Prefix file size exceeds maximum allowed size (100000), refusing to load it.
# netstat -tunlp|grep 8081
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 21544/java

将nexus设置为Linux系统的服务,并开机自动启动
1、复制$NEXUS_HOME/bin/jsw/linux-x86-64/nexus 到/etc/init.d/nexus
2、授于nexus脚本有可执行的权限:

1
chmod 755 /etc/init.d/nexus

3、修改nexus文件,配置以下参数:
修改NEXUS_HOME 绝对路径,如:NEXUS_HOME=”/usr/local/nexus”
设置RUN_AS_USER=nexus,或都其它的用户,前提是创建了此用户。

1
2
3
4
5
6
NEXUS_HOME=/usr/local/nexus
PLATFORM=linux-x86-64
PLATFORM_DIR="${NEXUS_HOME}/bin/jsw/${PLATFORM}"
WRAPPER_CMD="${PLATFORM_DIR}/wrapper"
WRAPPER_CONF="${PLATFORM_DIR}/../conf/wrapper.conf"
PIDDIR="${NEXUS_HOME}"

4、Red Hat, Fedora, CentOS增加nexus服务

1
2
3
4
5
cd /etc/init.d
chkconfig --add nexus
chkconfig --levels 345 nexus on
service nexus start
tail -f /usr/local/nexus/logs/wrapper.log

配置完成后,您可以访问web界面 http://{IP}:8081/nexus/ ,进入系统进行设置
默认管理账号:admin 密码:admin123

Nexus管理

仓库类型

登录Nexus在左边菜单栏里选择Repositories,列出已有的所有仓库,可以看到type有几种类型划分

  • group 仓库组:Nexus 通过仓库组的概念统一管理多个仓库,这样我们在项目中直接请求仓库组即可请求到仓库组管理的多个仓库
  • hosted 宿主仓库:主要用于部署无法从公共仓库获取的构件(如oracle的JDBC驱动)以及自己或第三方的项目构件
  • proxy 代理仓库:代理公共的远程仓库
  • virtual 虚拟仓库:用于适配 Maven1

仓库常见操作

  • 修改默认存储路径
  • 同步索引
  • 修改远程同步地址为aliyun镜像地址

参考资料

https://www.sonatype.com/

======================================================
希望各位朋友支持一下

本文作者:dongsheng
本文地址https://mds1455975151.github.io/archives/d406f18d.html
版权声明:转载请注明出处!

坚持技术分享,您的支持将鼓励我继续创作!