微服务----Nexus

Nexus简介

Nexus 是一个强大的仓库管理器,极大地简化了内部仓库的维护和外部仓库的访问。取代maven仓库,可以搭建在局域网中

2016 年 4 月 6 日 Nexus 3.0 版本发布,相较 2.x 版本有了很大的改变:

  • 对低层代码进行了大规模重构,提升性能,增加可扩展性以及改善用户体验。
  • 升级界面,极大的简化了用户界面的操作和管理。
  • 提供新的安装包,让部署更加简单。
  • 增加对 Docker, NeGet, npm, Bower 的支持。
  • 提供新的管理接口,以及增强对自动任务的管理。

我们平常访问的:https://mvnrepository.com/ 是maven公服,而Nexus是maven厂库管理的私服

使用

Nexus3:https://hub.docker.com/r/sonatype/nexus3 

docker pull sonatype/nexus3

我们使用 Docker 来安装和运行 Nexus,docker-compose.yml 配置如下:

version: '3.1'
services:
  nexus:
    restart: always
    image: sonatype/nexus3
    container_name: nexus
    ports:
      - 8081:8081
    volumes:
      - /usr/local/docker/nexus/data:/nexus-data   //数据卷用来管理images

启动Nexus

docker-compose up

启动后,会出现一个data的目录,但是可能启动后会直接退出。

docker-compose down   //关闭Nexus

给data目录加上权限:chmod 777 /usr/local/docker/nexus/data 赋予数据卷目录可读可写的权限,重新启动

docker-compose up

 启动成功后,如果需要登录,登录的初始用户名:admin,密码:admin123

在项目中使用 Maven 私服

1.配置认证信息

在 Maven settings.xml 中添加 Nexus 认证信息(servers 节点下):

注意IDEA自带有maven。

<server>
  <id>nexus-releases</id>              //发行版仓库
  <username>admin</username>           //私服的用户名
  <password>admin123</password>        //私服的密码(Nexus默认密码)
</server>

<server>
  <id>nexus-snapshots</id>             //快照版仓库
  <username>admin</username>
  <password>admin123</password>
</server> 

Snapshots 与 Releases 的区别

  • nexus-releases: 用于发布 Release 版本
  • nexus-snapshots: 用于发布 Snapshot 版本(快照版)

Release 版本与 Snapshot 定义如下:

Release: 1.0.0/1.0.0-RELEASE
Snapshot: 1.0.0-SNAPSHOT
  • 在项目 pom.xml 中设置的版本号添加 SNAPSHOT 标识的都会发布为 SNAPSHOT 版本,没有 SNAPSHOT 标识的都会发布为 RELEASE 版本。

  • SNAPSHOT 版本会自动加一个时间作为标识,如:1.0.0-SNAPSHOT 发布后为变成 1.0.0-SNAPSHOT-20180522.123456-1.jar,开发过程中使用快照版,是最好的,可以随时更改代码,并重新上传到Nexus。而发行版不能更改。

2.配置自动化部署

在 pom.xml 中添加如下代码:

<distributionManagement>  
  <repository>  
    <id>nexus-releases</id>  
    <name>Nexus Release Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-releases/</url>  
  </repository>  
  <snapshotRepository>  
    <id>nexus-snapshots</id>  
    <name>Nexus Snapshot Repository</name>  
    <url>http://127.0.0.1:8081/repository/maven-snapshots/</url>//地址获取看下图
  </snapshotRepository>  
</distributionManagement> 

注意事项:

  • ID 名称必须要与 settings.xml 中 Servers 配置的 ID 名称保持一致。
  • 项目版本号中有 SNAPSHOT 标识的,会发布到 Nexus Snapshots Repository, 否则发布到 Nexus Release Repository,并根据 ID 去匹配授权账号。

3.部署到仓库

mvn deploy 

 

成功后登录,如果出现这个代表成功

4.配置代理仓库(让maven可以通过私服下载jar包,需要配置仓库地址)

pom.xml

<repositories>
    <repository>
        <id>nexus</id>
        <name>Nexus Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nexus</id>
        <name>Nexus Plugin Repository</name>
        <url>http://127.0.0.1:8081/repository/maven-public/</url>  //配置公共仓库地址,公共仓库包含了发行版仓库和快照版仓库的所有的jar包
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
        <releases>
            <enabled>true</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>

  

扩展使用

4.上传 官服 中没有 JAR 包

使用 maven 命令上传第三方jar包:

mvn deploy:deploy-file
  -DgroupId=com.google.code.kaptcha
  -DartifactId=kaptcha
  -Dversion=2.3
  -Dpackaging=jar
  -Dfile=F:谷歌浏览器下载kaptcha-2.3.jar
  -Durl=http://192.168.2.104:8081/repository/maven-releases/   //可以自己创建一个专门存放第三名jar包的地址
  -DrepositoryId=nexus-releases                                //名字和之前配置认证信息中的id一样,用来登录用的

注意事项:

  • 建议在上传第三方 JAR 包时,创建单独的第三方 JAR 包管理仓库,便于管理有维护。(maven-3rd)
  • -DrepositoryId=nexus-releases 对应的是 settings.xml 中 Servers 配置的 ID 名称。(授权)

  

配置了私服,下载依赖流程

修改maven配置,方便下载的快照总是最新的

原文地址:https://www.cnblogs.com/yanxiaoge/p/11023213.html