Nexus Repository OSS 3是一个开源的仓库管理系统,提供了更加丰富的功能,而且安装、配置、使用起来也更加简单方便。OSS 3版本主要支持的仓库(Repository)包括如下:
- bower
- docker
- maven
- npm
- nuget
- pypi
- raw
- rubygems
- yum
其中,对于上述每种类型的Nexus仓库,都分别具有如下主要3种类型:
hosted:本地仓库,可以将我们内部使用的一些Maven项目,发布到该类型仓库,供内部开发人员使用。
proxy:代理仓库,用来代理远程公共仓库,比如Maven中央仓库。
group:仓库组,用来合并多个类型(hosted/proxy)的仓库。
这里,我们主要以支持Java编程的Maven项目依赖管理和构建进行实践,Nexus版本为nexus-3.7.0-04。
安装配置
下载Nexus Repository Manager软件包:
wget https://sonatype-download.global.ssl.fastly.net/nexus/3/nexus-3.7.0-04-unix.tar.gz
tar xvzf nexus-3.7.0-04-unix.tar.gz
解压缩后可以看到,生成nexus-3.7.0-04和sonatype-work两个目录:
[root@ali-bj01-tst-cluster-004 nexus]# ls
nexus-3.7.0-04 sonatype-work
是这两个目录在同一个目录下,例如我这里是在nexus目录下面。
可以在etc/nexus-default.properties配置文件中,修改对应的配置项,满足实际需要,这里我直接使用默认的,其它可以使用的配置可以参考官网说明。
如果使用自定义的JDK,可以增加如下配置:
export JAVA_HOME=/usr/local/java/jdk1.8.0_144/
启动Nexus Repository Manager,执行如下命令:
bin/nexus start
然后,可以通过Web页面登录浏览,链接如下所示:
http://172.16.117.65:8081
默认用户admin,默认密码为admin123,根据自己的需要修改密码,保证Nexus的安全。
Nexus Repository Manager也提供了方便的管理用户和权限的基础功能,可以登录到Web管理页面上进行管理配置,如下图所示:
发布管理
我们需要将自己的Maven项目构建后,发布到Nexus中,供其它项目使用,所以需要进行相关配置,并且具有一定权限才能向Nexus仓库中发布。
首先,在settings.xml文件中进行配置,配置内容如下所示:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/Users/yanjun/.m2/repository</localRepository>
<servers>
<server>
<id>maven-releases</id>
<username>admin</username>
<password>xxxxxxxxxxxxxxxx</password>
</server>
<server>
<id>maven-snapshots</id>
<username>admin</username>
<password>xxxxxxxxxxxxxxxx</password>
</server>
</servers>
</settings>
然后,在待要发布到Nexus仓库的Maven项目的pom.xml文件中,增加发布管理配置,如下所示:
<distributionManagement>
<repository>
<id>maven-releases</id>
<name>Nexus ReleaseRepository</name>
<url>http://172.16.117.65:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>maven-snapshots</id>
<name>Nexus SnapshotRepository</name>
<url>http://172.16.117.65:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
需要保证,pom.xml中distributionManagement中的id与setting.xml中配置的server中的id必须相同,才能发布。
最后,执行如下命令发布到Nexus的Snapshots仓库:
cd /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer
mvn -s ~/settings.xml deploy
执行上述命令,发布到Nexus的Snapshots仓库的过程,如下所示:
➜ azkaban-app-deployer git:(master) ✗ mvn -s ~/settings.xml deploy
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building azkaban-app-deployer 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ azkaban-app-deployer ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:2.5:compile (default-compile) @ azkaban-app-deployer ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ azkaban-app-deployer ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/src/test/resources