文章目录
- 下载 Nexus 服务器安装包
- 安装 Nexus 服务器
- 配置环境变量/启动服务器/停止服务器
- Nexus 服务器管理后台
- 登录管理后台
- Blob Stores
- Nexus 初始密码无法登录的问题
- 仓库(Repository)
- Nexus 内置的仓库
- Repository 的 Type 属性
- 创建代理仓库
- 创建本地仓库(也叫宿主仓库/私有仓库)
- 配置 Nexus 服务器
- Nexus 命令介绍
- 迁移 Nexus 服务器中的数据
- 删除 Nexus 服务器中的数据
- 升级 Nexus
- 部署工件到服务器
- 通过配置文件发布工件到 Nexus 服务器(第一种配置方式)
- 通过配置文件发布工件到 Nexus 服务器(第二种配置方式)
- 通过 Maven 命令发布工件到 Nexus 服务器
- 选择本地工件上传到 Nexus 宿主仓库
- 从 Nexus 服务器自动下载依赖工件
- settings.xml 配置私服
- 配置 profile 元素
- 配置 mirror 元素(镜像仓库配置)
- pom.xml 配置私服
- Nexus 3 和 Nexus 2 的区别
下载 Nexus 服务器安装包
目前的版本有 2.X 和 3.X ,2.X 对 Maven 的支持更友好一点,3.X 的支持范围更广,支持 ruby 和 docker。但是 3.X 要求 JDK 的版本是1.8,而且貌似还需要做特殊配制。有人下载下来解压后,运行不起来,看 sonatype-work 里面的 logs 报的是缺某个 jar 包,所以如果没有别的需要,只是搭建单纯的 Maven 私服,建议直接使用版本 2.x。
官网地址:https://www.sonatype.com/。
我在下面也给出了直接下载的地址:
点击下载 Nexus Repository Manager OSS 3.x /点击下载 Nexus Repository Manager OSS 2.x /点击下载 Nexus Repository 3.x。
通过官网下载 nexus 服务器安装包可能因为网速的问题会导致下载失败,我已经把安装包上传到百度云盘:
链接: https://pan.baidu.com/s/11E8lMR3-j71aUbYeK__Tyg
提取码: e1fd
安装 Nexus 服务器
下载安装包后,将其解压到你需要的目录下(例如:用户主目录/Applications/),我就是解压到用户主目录下的 Applications 目录下,如下图所示:
解压后你可以看到目录 nexus-3.20.1-01-mac,在这个目录下有两个子目录 nexus-3.20.1-01 和 sonatype-work。nexus-3.20.1-01 是程序,sonatype-work 是工作目录,如果以后要备份数据,只要备份 sonatype-work 就行。
在与 nexus-3.20.1-01 目录同级的目录下创建一个指向 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01
的软链接,以方便日后的升级:
[~/Applications/nexus-3.20.1-01-mac]$ ln -s /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-latest
[~/Applications/nexus-3.20.1-01-mac]$ ls
nexus-3.20.1-01 nexus-latest sonatype-work
配置环境变量/启动服务器/停止服务器
在配置有关环境变量之前,要启动 nexus 服务器,你需要打开命令终端,切换到 ~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin
目录下,再执行启动命令,如下所示:
[~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin]$ ./nexus start
Starting nexus
[~/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/bin]$ ./nexus status
nexus is running.
配置环境变量后则不必进入 nexus 的 bin 目录下执行启动命令。下面介绍下如何配置 nexus 的环境变量。
编辑用户主目录下 .bash_profile
文件:
[~/Applications/nexus-3.20.1-01-mac]$ vim ~/.bash_profile
在其中添加如下内容:
# 创建环境变量NEXUS_HOME
export NEXUS_HOME=/Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-latest
# 在环境变量PATH的值后面追加nexus的bin目录路径
PATH=$PATH:$NEXUS_HOME/bin
执行下面的命令,使 .bash_profile
文件的配置立即生效:
[~/Applications/nexus-3.20.1-01-mac]$ source ~/.bash_profile
因为前面已经配置 nexus 的环境变量,所以可以在任意目录执行 nexus 的命令,例如,我在用户主目录下执行下面的停止服务器的命令:
[~]$ nexus stop
Shutting down nexus
Stopped.
[~]$ nexus start
Starting nexus
Nexus 服务器管理后台
nexus 服务器启动后,在浏览器地址栏输入:http://localhost:8081,你可以打开 Nexus Repository 的管理后台的首页。
登录管理后台
通过右侧的 Sign In 登录管理后台。对于未登录的用户,只能查询和浏览相关资源。用户登录后就能看到管理相关的菜单了。
早期版本的 Nexus 的管理后台默认登录用户名是 admin,默认登录密码 admin123,最新版的 Nexus 管理后台默认登录密码不再是 admin123,具体参见下面的章节。
Blob Stores
Blob Stores 是用来配置资源的保存位置的,可以将不同的资源保存到服务器的不同位置上,类似于 Nexus 2 的资源保存路径。
Nexus 初始密码无法登录的问题
之前版本的 nexus 系统管理后台默认的登录用户名和密码是:admin/admin123,新版的 nexus 的管理后台默认登录密码不再是 admin123。初始密码保存在 nexus 安装目录下的 sonatype-work/nexus3/admin.password 文件中,打开文件可以看到如下内容:
上面的内容即为密码明文,而非加密后的结果,直接复制到密码输入框即可。
登录成功之后会要求修改密码,密码修改成功后,admin.password 文件会自动被删除。
仓库(Repository)
Nexus 内置的仓库
上图中的 Format 列:maven2 是Java 仓库;nuget 是 .net 仓库。
Repository 的 Type 属性
Repository 的 Type 有三种:proxy、hosted、group。
proxy:代理仓库。即你可以设置代理远程的仓库,设置了代理之后,在你的 Nexus 仓库中找不到需要的工件就会到代理的远程仓库中查找
hosted:本地仓库。即本地的私有仓库。通常用来存放和管理非中央仓库或者你自己开发的工件包。这仓库有 release 和 snapshots 两种类型,自己在构建工件包的时候,就需要指定 release(正式版/发行版) 还是 snapshots(开发版)。
group:组合仓库。这种类型的仓库可以组合多个仓库,然后用户只要访问这个组合仓库,就可以同时访问被组合的其它成员仓库。一般用来给用户一个访问 Nexus 仓库的统一地址。组合仓库中可以看到成员仓库中的工件资源,但是无权删除。
实际的使用场景通常是这样的,你可以上传私有的工件到 hosted 仓库中,代理仓库中配置被代理的远程仓库的 URL 以获取第三方的依赖(比如可以配置中央仓库的地址)。本地私有仓和代理仓都弄好了之后,再通过 group 仓库进行组合,这样就可以将组合仓库的 URL 对外提供使用了。
总结:
Maven 可以直接从宿主仓库(本地私有仓库)下载构件;Maven 也可以从代理仓库下载构件,而代理仓库中若没有请求的构件,则会自动地从被代理的远程仓库下载构件并缓存构件;Maven 可以从组合仓库下载构件,而组合仓库没有请求的构件,组合仓库会转向其包含的宿主仓库或者代理仓库获取请求的构件。
组合仓库的详情界面如下图所示:
特别注意,组仓库会按从上到下的顺序访问成员仓库。
创建代理仓库
Repository Name 和 Remote storage 填写好之后,就可以直接点击下方的 Create repository 创建仓库了。
代理仓库创建好之后就可以复制仓库的 URL,在 settings.xml 或者项目的 pom.xml 中进行私服仓库的配置,具体请参见下面的章节。
创建本地仓库(也叫宿主仓库/私有仓库)
配置 Nexus 服务器
打开 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.20.1-01/etc 目录下的属性文件 nexus-default.properties,打开后默认的内容如下:
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
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=/# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
nexus-pro-featurenexus.hazelcast.discovery.isEnabled=true
application-host:是 nexus 服务器的 IP 地址,默认为 0.0.0.0,表示只要是本机上的 IP,就能连上 nexus 服务器。为了安全和屏蔽掉其他IP,建议改成 localhost 默认指向的 IP,即 127.0.0.1。
application-port:是 IP 的端口号,默认为 8081,如果端口号被占用,可以换成其他端口号。
nexus-context-path:上下文路径,默认 /
,类似 Java Web 项目的虚拟目录,如果添加上下文路径,需要在访问地址后面加上下文路径,即访问地址变为:application-host:application-port/nexus-context-path。
配置后的内容如下:
## DO NOT EDIT - CUSTOMIZATIONS BELONG IN $data-dir/etc/nexus.properties
##
# Jetty section
application-port=8081
application-host=127.0.0.1
nexus-args=${jetty.etc}/jetty.xml,${jetty.etc}/jetty-http.xml,${jetty.etc}/jetty-requestlog.xml
nexus-context-path=/nexus/content # http://127.0.0.1:8081/nexus/content# Nexus section
nexus-edition=nexus-pro-edition
nexus-features=\
nexus-pro-featurenexus.hazelcast.discovery.isEnabled=true
配置好之后需要重启 nexus:
[/etc]$ nexus restart
Shutting down nexus
Stopped.
Restarting nexus
然后在浏览器地址栏输入:http://localhost:8081/nexus/content/
Nexus 命令介绍
nexus start
nexus stop
nexus status
nexus restart
nexus run
nexus run-redirect
nexus force-reload
迁移 Nexus 服务器中的数据
将 sonatype-work/nexus3 迁移到其它地方
删除 Nexus 服务器中的数据
sonatype-work/nexus3 整个目录删除,以前缓存的数据就全部删除了。
接着你可以重启 Nexus,等几秒钟,再输入之前的访问地址,nexus3 目录又会自动重新生成,之前的登录密码作废了,你需要再重新打开 sonatype-work/nexus3/admin.password 文件,将里面的密码粘贴到密码输入框即可完成登录。
升级 Nexus
下载最新版的 nexus 包,解压后仅保留 nexus-3.{new-version} 目录,并移动到 /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac
目录下,然后重新创建软链接 nexus-latest,指向新版本的 nexus 目录即可:
$ cd /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac
$ rm nexus-latest
$ ln -s /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-3.{new-version} /Users/liaowenxiong/Applications/nexus-3.20.1-01-mac/nexus-latest
以上升级方法适用于从 nexus-3.1.0+ 升级到 nexus3 的最新版本,官方文档为:《Upgrading Nexus Repository Manager 3.1.0 and Newer》
部署工件到服务器
通过配置文件发布工件到 Nexus 服务器(第一种配置方式)
第一步:配置 settings.xml
在 settings.xml 配置 nexus 服务器的登录用户名和密码,以及连接的仓库名称。
<!--配置访问存储类型为release的仓库时需要的认证信息 -->
<server><!-- id建议写仓库的名称,要与pom.xml中repository节点的id子节点的内容保持一致 --><id>release</id><!--Nexus的登录用户名--><username>admin</username><!--Nexus的登录密码--><password>123456</password>
</server><!--配置访问存储类型为snapshot的仓库时需要的认证信息 -->
<server><id>snapshot</id><username>admin</username><password>123456</password>
</server>
第二步:在项目的 pom.xml 文件中配置仓库的名称和访问地址
直接在 project 根节点的 distributionManagement 子节点中配置如下:
<distributionManagement><!--配置仓库类型为host(宿主仓库)的储存类型为release的仓库--><repository><!-- id的名字可以任意取,但是在settings.xml文件中的<server>元素的ID子元素的内容与这里一致 --><id>release</id><!--部署(即上传)工件的仓库地址--><url>http://localhost:8081/nexus/content/repository/maven-releases/</url></repository><!--配置仓库类型为host(宿主仓库)的储存类型为Snapshot的仓库--><snapshotRepository><!-- id的名字可以任意取,但是在settings.xml文件中的<server>元素的ID子元素的内容与这里一致 --><id>snapshot</id><!--部署(即上传)工件的仓库地址--><url>http://localhost:8081/nexus/content/repository/maven-snapshots</url></snapshotRepository>
</distributionManagement>
问题:代理仓库和组仓库能否部署工件呢?没有测试过!有时间再测试。
第三步:将项目打成指定格式的包,发布到仓库中
项目的 pom.xml 文件中可以指定项目的打包格式,如下图所示:
通过配置文件发布工件到 Nexus 服务器(第二种配置方式)
第一步:在 settings.xml 文件中配置
<settings>...<servers><!-- account and password for deploy to nexus --><server><id>lan</id><username>admin</username><password>admin123</password></server></servers><profiles><profile><id>lan</id><!--定义属性(即变量)--><properties><lan-release-url>http://localhost:8081/repository/maven-releases</lan-release-url><lan-snapshot-url>http://localhost:8081/repository/maven-snapshots</lan-snapshot-url></properties></profile></profiles>
</settings>
第二步:在 pom.xml 中配置部署(即上传,upload)工件的目标仓库
<project><profiles><!-- configuration for deploy to nexus repository --><profile><!-- 这个id值要和settings.xml中定义属性的profile的id值一致 --><id>lan</id><distributionManagement><!-- 这是配置版本类型为发布版的仓库 --><repository><!-- 这个id值要和settings.xml中的server的id值一致 --><id>lan</id><url>${lan-release-url}</url></repository><!-- 这是配置版本类型为开发版的仓库 --><snapshotRepository><!-- 这个id值要和settings.xml中的server的id值一致 --><id>lan</id><url>${lan-snapshot-url}</url></snapshotRepository></distributionManagement><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-source-plugin</artifactId></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-javadoc-plugin</artifactId></plugin></plugins></build></profile></profiles>
</project>
第三步:命令行执行 mvn clean deploy -P lan
即可将项目打包发布到 nexus 私服,命令中的 lan 是 pom.xml 文件中的 profile 的 ID 值。
通过 Maven 命令发布工件到 Nexus 服务器
第一步:配置 settings.xml
在 settings.xml 配置 nexus 服务器的登录用户名和密码,以及连接的仓库名称。
<server><id>maven-snapshots</id><username>admin</username><password>123456</password>
</server>
第二步:在 IDEA 的 Maven 命令输入框执行部署命令
将下面的命令语句填写到 Maven 输入框中:
deploy:deploy-file -DgroupId=com.example -DartifactId=demo02 -Dversion=1.0-SNAPSHOT -Dpackaging=jar -Dfile=/Users/liaowenxiong/Documents/IdeaProjects/demo02/target/demo02-1.0-SNAPSHOT.war -Durl=http://localhost:8081/nexus/content/repository/maven-snapshots/ -DrepositoryId=maven-snapshots
输入完命令语句,按回车键会执行命令语句。执行完成后,你再到 Nexus 服务器的管理后台查看是否发布成功,如下图所示:
选项说明:
DgroupId 和 DartifactId 构成了该 jar 包在 pom.xml 的坐标,项目就是依靠这两个属性定位。可以在项目的 pom.xml 文件中查看,如下图所示:
Dfile 表示需要上传的工件的绝对路径。
Durl 私服上仓库的位置,可以在 Nexus 服务器的管理后台查看仓库的 URL,如下图所示:
DrepositoryId 就是服务器的名称。
Dversion 工件的版本信息。在项目的 pom.xml 中可以看到。
选择本地工件上传到 Nexus 宿主仓库
如果通过包名和jar文件名来确定工件的坐标?
如上的 jar 文件,按 Maven 工件的命名规则,应该是 artifactId+version,解压 jar 文件,看下图:
所以上面的 c3p0-0.9.5.2.jar 的坐标是:
groupId=com.mchange.v2
artifactId=c3p0
version=0.9.5.2
从 Nexus 服务器自动下载依赖工件
无论是自己安装 Maven,还是 IDEA 自带的 Maven 默认是没有配置仓库信息,这种情况下 Maven 默认去远程的中心仓库下载所依赖的工件(也叫构件)。如果希望从自己搭建的私服下载依赖的工件,就需要明确告诉 Maven 去哪里下载,可以在三个地方进行配置,分别是:
1.Maven 的安装目录下的 conf 目录下的 settings.xml 文件,这个全局配置;
2.再一个是在 ~/.m2
目录下的 settings.xml,如果不存在该文件,可以复制 conf 目录下的 settings.xml,这个是针对当前用户的;
3.还有一个是在 pom.xml 中指定,这个只对 pom.xml 所属的项目有效。
以上三个文件的优先级是 pom.xml > ~/.m2/settings.xml
> conf/settings.xml
。
配置好私服的仓库信息后,当项目执行 Maven 操作时,如果本地库中没有依赖的构件,Maven 会去指定的私服仓库下载,如果指定的私服仓库也没有,私服仓库会去指定的远程仓库下载(注:私服仓库是 proxy 类型才会到指定的远程仓库下载),下载的构件会缓存在私服仓库中,这样当其它项目需要使用这些构件时,私服仓库就可以直接提供下载了。
settings.xml 配置私服
如果在 settings.xml 中设置了其它的镜像仓库,需要将其注释掉,我们自己配置的私服仓库才会生效。
配置 profile 元素
在 profiles
标签体中添加下面的元素:
<profile><!--id标签体的内容随意,但是要确保唯一性--><id>nexus</id><repositories><repository><!--id标签体的内容随意,但是要确保唯一性--><id>nexus</id><!--定义仓库的名称,但是在这里仓库的名称可以随便命名,不必与真实的仓库名称相同,即name标签体的内容随意--><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>nexus-snapshots</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>nexus</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository><pluginRepository><id>nexus-snapshots</id><name>local private nexus</name><url>http://localhost:8081/nexus/content/repository/maven-snapshots/</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories></profile>
上述的元素 id、name 可以根据自己的喜好来定义,保证 id 不重复即可,主要是仓库的 url 的配置,不要写错了,仓库的 url 可以在 nexus 服务器管理后台的 repositories 列表直接复制,如下图所示:
仓库中主要存放两种类型的构件,第一种构件被用作其它构件的依赖,这是中央仓库中存储的大部分构件的类型;另外一种构件类型是插件。如果不配置 pluginRepositories,那么 Maven 在下载项目所需的依赖构件时,还是会直接去远程中央仓库下载需要的插件构件,所以这里一定要加上这个。
另外还要在 activeProfiles
标签体中添加下面的元素:
<activeProfile>nexus</activeProfile>
activeProfile
标签体中的内容就上面定义 profile
的 id
。表示激活指定 id
的 profile
。
配置 mirror 元素(镜像仓库配置)
还可以在 mirrors 标签体中添加 mirror 元素来配置私服。
<!--自己搭建的Nexus服务器的仓库地址--><mirror><!-- id表示镜像的唯一标识,随意填写,但是要确保唯一性 --><id>private nexus</id><!--这里填写被镜像的仓库的id,如果你没有配置其它仓库,只有默认的中央仓库,而默认中央仓的id是central,所以你可以填写central--><mirrorOf>central</mirrorOf><!-- 定义镜像名称,具体名称随意 --><name>private proxy nexus repository</name><!--私服仓库的地址--><url>http://localhost:8081/nexus/content/repository/maven-aliyun/</url></mirror>
关于 mirror 元素的配置另请参见:https://maven.apache.org/guides/mini/guide-mirror-settings.html
pom.xml 配置私服
<repositories><repository><!--id根据自己的需要命名--><id>nexus</id><!--name根据自己的需要命名--><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></repository><repository><id>nexus-snapshots</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>nexus</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository><pluginRepository><id>nexus-snapshots</id><name>local private nexus</name><url>http://192.168.1.68:8081/nexus/content/groups/public-snapshots</url><releases><enabled>false</enabled></releases><snapshots><enabled>true</enabled></snapshots></pluginRepository></pluginRepositories>
pom.xml 文件中简单配置如下:
<!--配置私服--><repositories><repository><!--id标签体的内容随意,只要确保唯一即可--><id>maven-aliyun</id><!--name标签体的内容随意--><name>local private nexus</name><!--私服仓库的地址--><url>http://localhost:8081/nexus/content/repository/maven-aliyun/</url></repository></repositories>
Nexus 3 和 Nexus 2 的区别
Nexus 2 能够下载远程仓库资源的索引到 Nexus 服务器本地,供搜索查询使用,但是 Nexus 3 暂时还不支持。在 Nexus 3 中查询的都是已经从远程仓库下载并缓存到服务器本地的资源,对于远程仓库中没有缓存到 Nexus 本地的资源是无法查询到的。所以在 Nexus 3 搭建好后若没有被访问过,那么在 Nexus 的代理仓库是查询不到任何资源的。对于代理仓库而言,需要程序先访问 Nexus 代理仓库,然后由仓库向被代理的远程仓库发起请求,下载对应的资源并缓存到代理仓库中,然后各个项目客户端再从 Nexus 代理仓请求下载依赖的资源。
所以对于 Nexus 代理仓中没有的资源,必须项目客户端先发起请求,然后 Nexus 仓库才会去代理的远程中央仓库下载资源缓存到代理仓库中,以供其它用户直接下载使用。