本文介绍如何在在windows下用vagrant + virtualbox启动centOS 7虚拟机,搭建Nexus仓库。本文代码已开源。
步骤
前置条件:在windows下安装vagrant和virtualbox
vagrant版本2.2.18
virtualbox版本6.1.28
步骤1:在任意目录下新建vagrant-nexus3目录,cmd到vagrant-nexus3目录下运行vagrant init初始化vagrant工程,会发现多了一个Vagrantfile文件
D:\sandbox\code>cd vagrant-nexus3
D:\sandbox\code\vagrant-nexus3>vagrant init
A `Vagrantfile` has been placed in this directory. You are now
ready to `vagrant up` your first virtual environment! Please read
the comments in the Vagrantfile as well as documentation on
`vagrantup.com` for more information on using Vagrant.
D:\sandbox\code\vagrant-nexus3>
步骤2:初始化的Vagrantfile文件只是一个模板,修改Vagrantfile文件如下:
Vagrant.configure("2") do |config|
# https://app.vagrantup.com/centos/boxes/7
config.vm.box = "centos/7"
config.vm.network "forwarded_port", guest: 8081, host: 18081
end
config.vm.box = "centos/7"
指定操作系统为centos 7
config.vm.network "forwarded_port", guest: 8081, host: 18081
因为nexus的默认端口是8081,所以指定将虚拟机的8081端口转换到主机的18081端口,以便我们通过localhost:18081的方式访问nexus
步骤3:启动vagrant虚拟机
vagrant up
步骤4:登录到虚拟机
D:\sandbox\code\vagrant-nexus3>vagrant ssh
[vagrant@localhost ~]$
步骤5:从这一步开始,所有操作都是在虚拟机上进行的,安装JDK 1.8
sudo yum install java-1.8.0-openjdk.x86_64 -y
步骤6:解压nexus的安装包到/opt目录
sudo tar -xvf /vagrant/nexus-3.39.0-01-unix.tar.gz -C /opt
步骤7:cd到bin目录下,启动nexus
cd /opt/nexus-3.39.0-01/bin
sudo ./nexus run
步骤8:启动过程很慢,等待15分钟左右,查看日志/opt/sonatype-work/nexus3/log/nexus.log发现如下关键词表示nexus启动成功
2022-11-17 10:26:46,668+0000 INFO [jetty-main-1] *SYSTEM org.eclipse.jetty.server.Server - Started @1130631ms
2022-11-17 10:26:46,710+0000 INFO [jetty-main-1] *SYSTEM org.sonatype.nexus.bootstrap.jetty.JettyServer -
-------------------------------------------------
Started Sonatype Nexus OSS 3.39.0-01
-------------------------------------------------
在windows下用浏览器打开http://localhost:18081/
步骤9:登录,账户admin,初始密码查看/opt/sonatype-work/nexus3/admin.password
[vagrant@localhost opt]$ cat /opt/sonatype-work/nexus3/admin.password
e5f9259a-8f4e-4869-ad2c-efa1ad8cc02a
步骤10:成功登录后需要做一系列设置。
变更密码,这里我用admin123
设置权限
完成设置
发布制品到Nexus
步骤1:找到 Maven 安装目录的settings.xml文件,添加如下配置
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
此处,是设置的账号和密码都是 Nexus 服务的登录的账号和密码,我用初始账户admin和我修改的密码admin123
步骤2:在maven项目的 pom.xml 文件添加如下配置
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://localhost:18081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://localhost:18081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
步骤3:用maven进行打包发布
mvn clean deploy
日志里有uploaded to nexus和BUILD SUCCESS等关键词,表示发布成功。
[INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ flyway-db-maintainer ---
Downloading from nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/maven-metadata.xml
Uploading to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/flyway-db-maintainer-1.0-20221117.070849-1.jar
Uploaded to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/flyway-db-maintainer-1.0-20221117.070849-1.jar (3.2 MB at 564 kB/s)
Uploading to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/flyway-db-maintainer-1.0-20221117.070849-1.pom
Uploaded to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/flyway-db-maintainer-1.0-20221117.070849-1.pom (2.0 kB at 414 B/s)
Downloading from nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/maven-metadata.xml
Uploading to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/maven-metadata.xml
Uploaded to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/1.0-SNAPSHOT/maven-metadata.xml (775 B at 401 B/s)
Uploading to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/maven-metadata.xml
Uploaded to nexus-snapshots: http://localhost:18081/repository/maven-snapshots/org/example/flyway-db-maintainer/maven-metadata.xml (289 B at 121 B/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 22.097 s
[INFO] Finished at: 2022-11-17T15:09:05+08:00
[INFO] ------------------------------------------------------------------------
步骤4:此时在nexus中可以找到刚发布的jar包
Github
文末附上代码链接https://github.com/bin9wei/vagrant-nexus3
参考
https://help.sonatype.com/repomanager3/installation-and-upgrades/installation-methods
How To Install Latest Sonatype Nexus 3 On Linux (Redhat/Centos/ec2)