文章目录
- 下载 Nexus 服务器安装包
- 安装 Nexus 服务器
- 配置环境变量/启动服务器/停止服务器
- Nexus 服务器管理后台
- Nexus 初始密码无法登录的问题
- 配置 Nexus 服务器
- Nexus 命令介绍
- Repository 的 type 属性
- 迁移 Nexus 服务器中的数据
- 升级 Nexus
- 部署工件到服务器
- 从 Nexus 服务器自动下载依赖工件
下载 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 的管理后台的首页。
登录管理后台系统的默认用户名 admin,默认登录密码 admin123。
Nexus 初始密码无法登录的问题
之前版本的 nexus 系统管理后台默认的登录用户名和密码是:admin/admin123,新版的 nexus 的管理后台默认登录密码不再是 admin123。初始密码保存在 nexus 安装目录下的 sonatype-work/nexus3/admin.password 文件中,打开文件可以看到如下内容:
上面的内容即为密码明文,而非加密后的结果,直接复制到密码输入框即可。
配置 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-feature
nexus.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-feature
nexus.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
Repository 的 type 属性
Repository 的 type 有三种:proxy、hosted、group。
proxy:即你可以设置代理,设置了代理之后,在你的nexus中找不到的依赖就会去配置的代理地址中找
hosted:你可以上传你自己的项目到这里面
group:它可以包含前面两个,是一个聚合体。一般用来给用户一个访问 nexus 的统一地址。
总的来说,就是你可以上传私有的项目到 hosted,以及配置 proxy 以获取第三方的依赖(比如可以配置中央仓库的地址)。前面两个都弄好了之后,再通过 group 聚合一个访问 nexus 的统一地址。
迁移 Nexus 服务器中的数据
将 sonatype-work/nexus3 迁移到其它地方
升级 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 服务器的登录用户名和密码,以及连接的仓库名称。
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>123456</password>
</server>
上面元素 id 是仓库的名称。
第二步:在项目的 pom.xml 文件中配置仓库的名称和访问地址
<distributionManagement>
<repository>
<id>maven-snapshots</id>
<url>http://localhost:8081/nexus/content/repository/maven-snapshots/</url>
</repository>
</distributionManagement>
第三步:将项目打成指定格式的包,发布到仓库中
通过 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 中可以看到。
解压该包,会发现一个叫 MANIFEST.MF 的文件,这个文件就有描述该包的版本信息。
从 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 配置私服
在 profiles
标签体中添加下面的元素:
<profile>
<id>nexus</id>
<repositories>
<repository>
<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>
</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
。
pom.xml 配置私服
<repositories>
<repository>
<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>
</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>