赞
踩
Nexus 是Maven仓库管理器,如果你使用Maven,你可以从Maven中央仓库 下载所需要的构件(artifact),但这通常不是一个好的做法,你应该在本地架设一个Maven仓库服务器,在代理远程仓库的同时维护本地仓库,以节省带宽和时间,Nexus就可以满足这样的需要。此外,他还提供了强大的仓库管理功能,构件搜索功能,它基于REST,友好的UI是一个extjs的REST客户端,它占用较少的内存,基于简单文件系统而非数据库。这些优点使其日趋成为最流行的Maven仓库管理器。
Nexus不是Maven的核心概念,它仅仅是一种衍生出来的特殊的Maven仓库。对于Maven来说,仓库只有两种:本地仓库和远程仓库。
本地仓库就是我们在maven的setting.xml中配置的本地仓库地址。由于最原始的本地仓库是空的,Maven必须知道至少一个可用的远程仓库,才能在执行Maven命令的时候下载到需要的构件。
中央仓库是maven默认的远程仓库。
私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件。有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库;否则,私服请求外部的远程仓库,将构件下载到私服,再提供给本地仓库下载。
jboss是一个开放源代码的应用服务器
java .net是指java仓库和.net仓库
安装要求:
#下载Linux版本
wget https://sonatype-download.global.ssl.fastly.net/repository/repositoryManager/3/nexus-3.14.0-04-unix.tar.gz
mkdir /usr/local/nexus
tar -zxf nexus-3.14.0-04-unix.tar.gz -C /usr/local/nexus/
#启动服务(默认监听8081端口)
/usr/local/nexus-3.14.0-04/bin/nexus start
#访问服务
firefox http://服务ip:8081
默认用户名和密码: admin admin123
nexus-3.14.0-04/ 为应用目录
sonatype-work/ 为工作目录,存放镜像仓库
/usr/local/nexus/nexus-3.14.0-04/etc/nexus-default.properties (修改对应字段即可,默认8081)
/usr/local/nexus/nexus-3.14.0-04/bin/nexus.vmoptions (修改对应字段即可)
/usr/local/nexus/sonatype-work/nexus3/log/ (此目录存放各模块日志文件)
备份仓库:将sonatype-work文件夹整体备份即可,也可以选择只备份最重要的连个文件夹索引(indexer)和仓库(storage)
将备份好的sonatype-work文件拷贝到新的服务器中,然后修改nexus/conf/nexus.properties配置文件,重新指定仓库的目录
vim /usr/local/nexus/sonatype-work/nexus3/db/security/user.pcl
此文件默认的最后一行是这样,如果忘了登陆密码也不要修改它
admin^ZAdministrator^HUser^Lactive"admin@example.org<8c>^B$shiro1$SHA-512$1024$NE+wqQq/TmjZMvfI7ENh/g==$V4yPw8T64UQ6GfJfxYq2hLsVrBY8D1v+bktfOxGdt4b/9BthpWPNUy/CBk6V9iA0nHpzYzJFWO8v/tZFtES8CA==^Aÿÿÿÿÿÿÿÿ
[root@localhost ~]# cd /usr/local/nexus/nexus-3.14.0-04/
[root@localhost nexus-3.14.0-04]# java -jar ./lib/support/nexus-orient-console.jar
OrientDB console v.2.2.36 (build d3beb772c02098ceaea89779a7afd4b7305d3788, branch 2.2.x) https://www.orientdb.com
Type 'help' to display all the supported commands.
orientdb> connect plocal:../sonatype-work/nexus3/db/security admin admin
orientdb {db=security}> update user SET password="$shiro1$SHA-512$1024$NE+wqQq/TmjZMvfI7ENh/g==$V4yPw8T64UQ6GfJfxYq2hLsVrBY8D1v+bktfOxGdt4b/9BthpWPNUy/CBk6V9iA0nHpzYzJFWO8v/tZFtES8CA==" UPSERT WHERE id="admin"
Updated record(s) '1' in 0.023000 sec(s).
orientdb {db=security}> exit
再次登陆即可,默认密码已恢复成admin123
默认开启匿名访问权限
根据需要分配权限,nx-all为全部权限
登陆nexus界面
firefox 192.168.43.249:8081
点击右上角的sign in 输入账号密码进行登陆
点击上边的齿轮,调出菜单,进行角色管理,如下图所示
填写角色信息和权限,如下图所示
完成上述操作之后,在新出现的页面会显示目前系统中已有的角色,如下图
所以自定义构件的仓库组代理仓库的顺序为:Releases,Snapshots,3rd party,Central。也可以把自定义仓库放到Central前面,下载包会更快。
release包不带时间戳,自建或默认release类型仓库,如果哦开发中需要重复发版,则需要开启release类型仓库的对应权限设置 allow redeploy
url为 http://maven.aliyun.com/nexus/content/public/
其他的内容不做修改,直接点击页面下边的create repository
创建完成之后可以看到新增加了阿里云库
在新的页面,下拉,找到group,把aliyun从左侧的框内移动到右侧的框内,如下图,然后点击页面下侧的save,另外,如果想优先使用阿里云仓库,可以调整顺序
重点:
场景一:
自定义新建proxy类型仓库,在maven配置中分别配置不同调用地址,或是将自定义新建的proxy仓库统一加入一个组,在maven配置中调用一个地址
场景二:
使用maven已经建立好的proxy仓库,且使用已建立好的组(maven-public)配置maven调用地址
场景一和场景二本质上讲没有什么变化,根据公司开发习惯和需求进行配置即可
注意:pom文件也可以进行地址配置,成为单独配置,修改settings.xml,为全局配置
[root@localhost ~]# cd /usr/local/apache-maven-3.6.0/conf/
[root@localhost conf]# cp settings.xml settings.xml.bak
[root@localhost conf]# vim settings.xml
找到连个mirrors标签 在</mirrors>这一行上边我们自己来写一对mirror标签,写完之后如下
159 <mirror>
160 <id>nexus-bdqnconf</id>
161 <name>nesux bdqnconf</name>
162 <url>http://192.168.43.249:8081/repository/maven-public/</url>
163 <mirrorOf>*</mirrorOf>
164 </mirror>
165 </mirrors>
关于mirrorof的说明,它指明要拦截谁,比如在maven设置阿里云仓库的时候此处写的事center,表示如果是去中央仓库的请求,会拦截下来,转到阿里云仓库下载。如果填写*则表示所有请求都会被拦截,并去本mirror指定的url下载构建。
演示
清空本地仓库数据
[root@localhost ~]# rm -rf /root/.m2/repository/*
打包maven项目测试
[root@localhost ~]# mvn archetype:generate -DgroupId=cn.bdqn.bdqnweb -DartifactId=bdqnweb -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false [root@localhost ~]# cd /tmp/testdir/bdqnweb/ [root@localhost bdqnweb]# ls pom.xml src target [root@localhost bdqnweb]# mvn clean package ··········· Downloaded from nexus-bdqnconf: http://192.168.43.249:8081/repository/maven-public/com/thoughtworks/xstream/xstream/1.3.1/xstream-1.3.1.jar (431 kB at 265 kB/s) [INFO] Packaging webapp [INFO] Assembling webapp [bdqnweb] in [/tmp/testdir/bdqnweb/target/bdqnweb] [INFO] Processing war project [INFO] Copying webapp resources [/tmp/testdir/bdqnweb/src/main/webapp] [INFO] Webapp assembled in [85 msecs] [INFO] Building war: /tmp/testdir/bdqnweb/target/bdqnweb.war [INFO] WEB-INF/web.xml already added, skipping [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 02:13 min [INFO] Finished at: 2019-02-02T15:46:45+08:00 [INFO] ------------------------------------------------------------------------ [root@localhost bdqnweb]#
打包的过程截个图,可以看到使用了配置的私服进行jar包下载,而且nexus web界面也有了对应数据包
配置服务器认证(上传需要认证),使用我们上述建立的用户
[root@localhost bdqnweb]# vim /usr/local/apache-maven-3.6.0/conf/settings.xml
找到</servers>标签,我们在这个标签上一行写入一些内容,写完后如下:
<server>
<id>nexus</id>
<username>bdqndev</username>
<password>admin123</password>
</server>
</servers>
要说明一点,这里的password就是用户注册时候的密码,不能是别的,否则账号密码不对应,不能上传东西到私服
配置项目pom文件,添加上传项
[root@localhost bdqnweb]# pwd /tmp/testdir/bdqnweb [root@localhost bdqnweb]# ls pom.xml src target [root@localhost bdqnweb]# vim pom.xml 在</build>和</project>之间添加内容,添加内容如下 <distributionManagement> <snapshotRepository> <id>nexus</id> <name>Nexus Snapshot</name> <url>http://192.168.43.249:8081/repository/maven-snapshots/</url> </snapshotRepository> <repository> <id>nexus</id> <name>Nexus Releases</name> <url>http://192.168.43.249:8081/repository/maven-releases/</url> </repository> </distributionManagement>
文件修改如图
[root@localhost bdqnweb]# mvn deploy [WARNING] [WARNING] Some problems were encountered while building the effective settings [WARNING] Unrecognised tag: 'release' (position: START_TAG seen ...</url>\n <release>... @212:20) @ /usr/local/apache-maven-3.6.0/conf/settings.xml, line 212, column 20 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ----------------------< cn.bdqn.bdqnweb:bdqnweb >----------------------- [INFO] Building bdqnweb Maven Webapp 1.0-SNAPSHOT [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ bdqnweb --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] Copying 0 resource [INFO] [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ bdqnweb --- [INFO] No sources to compile [INFO] [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ bdqnweb --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory /tmp/testdir/bdqnweb/src/test/resources [INFO] [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ bdqnweb --- [INFO] No sources to compile [INFO] [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ bdqnweb --- [INFO] No tests to run. [INFO] [INFO] --- maven-war-plugin:2.2:war (default-war) @ bdqnweb --- [INFO] Packaging webapp [INFO] Assembling webapp [bdqnweb] in [/tmp/testdir/bdqnweb/target/bdqnweb] [INFO] Processing war project [INFO] Copying webapp resources [/tmp/testdir/bdqnweb/src/main/webapp] [INFO] Webapp assembled in [71 msecs] [INFO] Building war: /tmp/testdir/bdqnweb/target/bdqnweb.war [INFO] WEB-INF/web.xml already added, skipping [INFO] [INFO] --- maven-install-plugin:2.4:install (default-install) @ bdqnweb --- [INFO] Installing /tmp/testdir/bdqnweb/target/bdqnweb.war to /root/.m2/repository/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-SNAPSHOT.war [INFO] Installing /tmp/testdir/bdqnweb/pom.xml to /root/.m2/repository/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-SNAPSHOT.pom [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy (default-deploy) @ bdqnweb --- Downloading from nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/maven-metadata.xml Uploading to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-20190202.082845-1.war Uploaded to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-20190202.082845-1.war (2.2 kB at 9.4 kB/s) Uploading to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-20190202.082845-1.pom Uploaded to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/bdqnweb-1.0-20190202.082845-1.pom (1.2 kB at 8.9 kB/s) Downloading from nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/maven-metadata.xml Uploading to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/maven-metadata.xml Uploaded to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/1.0-SNAPSHOT/maven-metadata.xml (766 B at 4.2 kB/s) Uploading to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/maven-metadata.xml Uploaded to nexus: http://192.168.43.249:8081/repository/maven-snapshots/cn/bdqn/bdqnweb/bdqnweb/maven-metadata.xml (280 B at 1.8 kB/s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.325 s [INFO] Finished at: 2019-02-02T16:28:45+08:00 [INFO] ------------------------------------------------------------------------ [root@localhost bdqnweb]#
如果mvn deploy遇到报错 返回401的错误,这种错误是因为权限的问题,第一检查一下配置settings.xml的时候账号密码有没有写错,第二个检查一下settings.xml里的id标签和pom.xml文件中的id标签是否相同,不相同会报401的错误
web上查看上传的jwar包
在新的页面依次点击+号 展开目录 可以看到snapshot的war包是有时间戳的
如果要打release包,可以将pom文件中的SNAPSHOT标识去掉
[root@localhost bdqnweb]# vim pom.xml
把这行 <version>1.0-SNAPSHOT</version> 改成 <version>1.0</version>
[root@localhost bdqnweb]# mvn deploy
如图,可以看到上传的是release的war包
web页面如下图
新建一个hosted类型仓库
填写一下name,hosted那里选择可以重复发版,之后点击create
加入maven-public组,方便统一索引引用
将3rd调整到右侧,点击下边的save
maven认证配置(添加一个server认证)
[root@localhost bdqnweb]# vim /usr/local/apache-maven-3.6.0/conf/settings.xml
在</server>这一行的上一行添加内容,添加内容为:
<server>
<id>3rdnexus</id>
<username>bdqndev</username>
<password>admin123</password>
</server>
使用命令上传第三方jar包到上述建立的第三方仓库
[root@localhost bdqnweb]# mkdir /tmp/testdir/nexus3rdjar/ [root@localhost bdqnweb]# cp /root/aliyun-sdk-oss-2.6.1.jar /tmp/testdir/nexus3rdjar/ [root@localhost bdqnweb]# mvn deploy:deploy-file -DgroupId=com.aliyun.oss -DartifactId=aliyun-sdk-oss -Dversion=2.6.1 -Dpackaging=jar -Dfile=/tmp/testdir/nexus3rdjar/aliyun-sdk-oss-2.6.1.jar -Durl=http://192.168.43.249:8081/repository/3rd/ -DrepositoryId=3rdnexus [WARNING] Some problems were encountered while building the effective settings [WARNING] Unrecognised tag: 'release' (position: START_TAG seen ...</url>\n <release>... @218:20) @ /usr/local/apache-maven-3.6.0/conf/settings.xml, line 218, column 20 [WARNING] [INFO] Scanning for projects... [INFO] [INFO] ----------------------< cn.bdqn.bdqnweb:bdqnweb >----------------------- [INFO] Building bdqnweb Maven Webapp 1.0 [INFO] --------------------------------[ war ]--------------------------------- [INFO] [INFO] --- maven-deploy-plugin:2.7:deploy-file (default-cli) @ bdqnweb --- Uploading to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/2.6.1/aliyun-sdk-oss-2.6.1.jar Uploaded to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/2.6.1/aliyun-sdk-oss-2.6.1.jar (470 kB at 555 kB/s) Uploading to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/2.6.1/aliyun-sdk-oss-2.6.1.pom Uploaded to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/2.6.1/aliyun-sdk-oss-2.6.1.pom (401 B at 3.4 kB/s) Downloading from 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/maven-metadata.xml Uploading to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/maven-metadata.xml Uploaded to 3rdnexus: http://192.168.43.249:8081/repository/3rd/com/aliyun/oss/aliyun-sdk-oss/maven-metadata.xml (308 B at 2.5 kB/s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.949 s [INFO] Finished at: 2019-02-02T17:31:04+08:00 [INFO] ------------------------------------------------------------------------ [root@localhost bdqnweb]#
上述命令中-DrepositoryID 是仓库id 对应settings.xml配置文件中的id标签
web查看第三方jar包上传信息
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。