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 [INFO] [INFO] --- maven-compiler-plugin:2.5:testCompile (default-testCompile) @ azkaban-app-deployer --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ azkaban-app-deployer --- [INFO] No tests to run. [INFO] [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ azkaban-app-deployer --- [INFO] Building jar: /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- maven-assembly-plugin:2.4.1:single (make-assembly) @ azkaban-app-deployer --- [INFO] Building jar: /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ azkaban-app-deployer --- [INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT.jar to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT.jar [INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/pom.xml to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT.pom [INFO] Installing /Users/yanjun/Workspaces/idea-workspace/azkaban-app-deployer/target/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar to /Users/yanjun/.m2/repository/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-SNAPSHOT-jar-with-dependencies.jar [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ azkaban-app-deployer --- Downloading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.jar Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.jar (36 kB at 3.7 kB/s) Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.pom Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1.pom (3.8 kB at 416 B/s) Downloading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml (780 B at 85 B/s) Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/maven-metadata.xml (290 B at 31 B/s) Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1-jar-with-dependencies.jar Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/azkaban-app-deployer-0.0.1-20180102.065956-1-jar-with-dependencies.jar (3.8 MB at 172 kB/s) Uploading: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml Uploaded: http://172.16.117.65:8081/repository/maven-snapshots/cn/xiweiai/azkaban-app-deployer/0.0.1-SNAPSHOT/maven-metadata.xml (1.0 kB at 110 B/s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 01:13 min [INFO] Finished at: 2018-01-02T15:01:05+08:00 [INFO] Final Memory: 20M/274M [INFO] ------------------------------------------------------------------------
可以登录到Nexus Repository Manager中,查看已经发布项目jar的Snapshots文件,示例如下图所示:
需要说明的是,我们在Maven项目的pom.xml文件中 ,如果版本号中含有SNAPSHOT,则会自动发布到Snapshots仓库中,例如:
<groupId>cn.xiweiai</groupId> <artifactId>azkaban-app-deployer</artifactId> <version>0.0.1-SNAPSHOT</version>
如果将version的值改为不带SNAPSHOT后缀,则会发布到releases仓库中,根据实际需要进行选择。
使用仓库
安装配置好我们私有的Maven仓库,如果我们想使用我们内部发布到Nexus中的Maven依赖,可以直接配置Release Repository:
<repositories> <repository> <id>maven-releases</id> <name>Nexus ReleaseRepository</name> <url>http://172.16.117.65:8081/repository/maven-releases/</url> </repository> </repositories>
也可以通过使用group类型的仓库,只需要在Maven项目的pom.xml文件中增加如下配置即可:
<repositories> <repository> <id>nexus</id> <url>http://172.16.117.65:8081/repository/maven-public/</url> </repository> </repositories>
上述配置的该仓库是group类型的仓库组,它会将hosted和proxy类型的仓库合并起来,即:我们在pom.xml中配置依赖时,如果是我们内部的依赖,则会从hosted类型的仓库中去加载;如果是一些开源的依赖,则会通过proxy类型的仓库,代理转发到外部仓库中下载。如果有其它更多非group类型的仓库,也会从这些仓库中查找依赖资源。
参考链接
- https://www.sonatype.com/download-oss-sonatype
- http://books.sonatype.com/nexus-book/3.5/reference/index.html
本文基于署名-非商业性使用-相同方式共享 4.0许可协议发布,欢迎转载、使用、重新发布,但务必保留文章署名时延军(包含链接:http://shiyanjun.cn),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系。