搭建步骤(以windows系统搭建为例)

1、nexus下载


Download Repository OSSwww.sonatype.com



nexus3安装 配置 docker nexus windows安装_nexus3安装 配置 docker


2、安装服务

修改配置:


nexus3安装 配置 docker nexus windows安装_maven_02


启动服务:cmd 进入nexus-3.16.1-02bin,输入nexus.exe/run


nexus3安装 配置 docker nexus windows安装_xml_03


启动成功:http://localhost:8088/


nexus3安装 配置 docker nexus windows安装_windows nexus安装_04


补充说明:
1. 在之前的版本中,启动nexus服务都是在cmd中输入 
2. nexus install来安装服务,nexus start来启动服务。 
3. 在nexus-3.2.0-01中,直接在nexus根目录下的bin下,
4. 输入 nexus.exe/run 即会启动服务。
注意:需要在nexus-3.16.1-02etcnexus-default.properties配置ip地址和端口号;

使用管理员权限进入cmd,进入到D:softwarenexusnexus-3.16.1-02bin 输入nexus.exe/run启动服务


3、配置nexus

  • 登录admin/admin123


nexus3安装 配置 docker nexus windows安装_maven_05


  • 自定义用户并授权


nexus3安装 配置 docker nexus windows安装_windows nexus安装_06


部分配置可自己手动尝试:如建仓库、角色(授权仓库访问权限)、用户(授权)等,管理后台使用比较简单直观,这里就不做赘述。

  • 去除匿名登录(用账号密码登录拉取上传jar更安全)


nexus3安装 配置 docker nexus windows安装_windows nexus安装_07


  • 仓库类型
hosted,本地仓库(也叫宿主仓库),通常我们会部署自己的构件到这一类型的仓库或者是第三方的包(如:oracel的)。
proxy,代理仓库,它们被用来代理远程的公共仓库,如maven中央仓库。
group,仓库组,用来合并多个hosted/proxy仓库,通常我们配置maven依赖仓库组


maven-central:maven中央库,默认从https://repo1.maven.org/maven2/拉取jar 
maven-releases:私库发行版jar 
maven-snapshots:私库快照(调试版本)jar 
maven-public:仓库分组,把上面三个仓库组合在一起对外提供服务,在本地maven基础配置settings.xml中使用。


  • 自定义仓库


nexus3安装 配置 docker nexus windows安装_maven_08


nexus3安装 配置 docker nexus windows安装_maven_09


4、关联私有仓库下载jar

  • maven setting.xml文件配置

<server>:定义jar包下载的Maven仓库、定义部署服务器。

用来下载和部署的仓库是用POM中的repositories和distributionManagement元素来定义的。但是某些配置例如username和password就不应该随着pom.xml来分配了。这种类型的信息应该保存在构建服务器中的settings.xml中。

  • id:server 的id,用于匹配distributionManagement库id,比较重要。
  • username, password:用于登陆此服务器的用户名和密码
  • privateKey, passphrase:设置private key,以及passphrase
  • filePermissions, directoryPermissions:当库文件或者目录创建后,需要使用权限进行访问。参照unix文件许可,如664和775

<mirrors>:表示镜像库,指定库的镜像,用于增加其他库。mirror相当于一个拦截器,它会拦截maven对remote repository的相关请求,把请求里的remote repository地址,重定向到mirror里配置的地址。

  • id,name:唯一的标志,用于区别镜像
  • url:镜像的url
  • mirrorOf:此镜像指向的服务id

<profiles>:主要包括activation,repositories,pluginRepositories 和properties元素,用于个性化配置。

  • repositories,pluginRepositories
  • releases, snapshots:每个产品的版本的Release或者snapshot(注:release和snapshot的区别,release一般是比较稳定的版本,而snapshot基本上不稳定,只是作为快照)

<activeProfiles>:表示激活的profile,通过profile id来指定。


<?xml version="1.0" encoding="UTF-8"?>

<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">
  <!-- 本地jar包存储位置-->
   <localRepository>D:softwarenexusrepo</localRepository>

  <servers>
   <server>
        <id>nexus-releases</id>  
        <username>admin</username>  
        <password>admin123</password>  
    </server>  
    <server>
        <id>nexus-snapshots</id>  
        <username>admin</username>  
        <password>admin123</password>  
    </server>
    <server>
      <id>nexus-dev</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
 <!--可自定义用户-->
  <server>
      <id>nexus-dev-cux</id>
      <username>raphal</username>
      <password>123456</password>
    </server>
  </servers>
  
  <mirrors>
    <mirror>
      <id>nexus-dev</id>
      <mirrorOf>*</mirrorOf>
      <url>http://localhost:8088/repository/my-repo-public/</url>
    </mirror>
  </mirrors>
 
</settings>


注:关联私服只需配置setting.xml中的server和mirrors,且server 的id = mirror 的id。

无需配置pom.xml


调用链路:项目maven install->先找本地localRepository,找不到则通过setting.xml 中mirror配置的url->调用maven-public仓库组->仓库组调用组内的仓库进行jar包下载(存放在私服)-》返回jar下载到开发电脑并存放到setting中localRepository的目录中。


nexus3安装 配置 docker nexus windows安装_maven_10

使用idea下载

nexus3安装 配置 docker nexus windows安装_maven_11

下载日志

nexus3安装 配置 docker nexus windows安装_windows nexus安装_12

远程下载jar包到私服

nexus3安装 配置 docker nexus windows安装_xml_13

下载到本地

mirror和repository加载顺序:


1、在mirrorOf与repositoryId相同的时候优先是使用mirror的地址
2、mirrorOf等于*的时候覆盖所有repository配置
3、存在多个mirror配置的时候mirrorOf等于*放到最后
4、只配置mirrorOf为central的时候可以不用配置repository


配置优先级从高到低:pom.xml> user settings > global settings。

5、发布自己的快照版本jar到私有仓库

  • setting.xml配置


nexus3安装 配置 docker nexus windows安装_nexus3安装 配置 docker_14


  • pom.xml配置


nexus3安装 配置 docker nexus windows安装_jar_15


  • 使用idea打包部署并上传到私服


nexus3安装 配置 docker nexus windows安装_xml_16

打包、部署

nexus3安装 配置 docker nexus windows安装_nexus3安装 配置 docker_17

部署成功日志

nexus3安装 配置 docker nexus windows安装_windows nexus安装_18

上传jar到snapshots

<version>0.0.1-SNAPSHOT</version>系统会根据项目版本后缀SNAPSHOT将jar上传到对应的私服中。


<version>0.0.1-SNAPSHOT</version> 如果版本后缀是SNAPSHOT会自动识别上传到SNAPSHOT仓库。
<version>0.0.1</version>上传到自定义仓库