Nexus Repository 是搭建maven的镜像的工具之一,在全球范围内使用挺广的。

一、Nexus 搭建过程

  Nexus 镜像的搭建还是相对简单的,将下载的文件解压到相应的目录下,然后进入./nexus-version/bin/下,启动nexus即可将Nexus Repository运行起来(其中Nexus是基于Jetty来运行的)。

  启动起来后,通过浏览器访问http://ip:8081就可以访问了。界面如下所示:

  其中通过右侧的Sign In可以登录用户来管理Nexus。对于未登录的用户,只能查询和浏览相关资源。用户登录后就能看到管理相关的菜单了,在管理菜单中主要是配置Blob Stores和Repositories,其中Blob Stores 是用来配置资源的保存位置的,可以将不同的远程资源保存到服务器的不同位置上(类似于Nexus 2的远程资源保存路径配置,在Nexus 3中进行了分组管理),Repositories 则用来配置远程资源和本地资源,其中支持Proxy类型资源,表示远程资源的镜像,host类型的资源表示本地资源,用于保存第三方资源或团队内部共享的资源等。group类型资源是组,可以将多个proxy或host类型的资源构建成一个组,供Maven使用等,如下图所示:

注意事项:

1.  Nexus 2 能够支持资源信息的拷贝,提供xml格式,而Nexus 3 暂时还不支持该xml格式拷贝。

2.  Nexus 2 能够下载远程的索引到本地,供搜索查询使用,但是Nexus 3 暂时还不支持,在Nexus 3 中查询的都是已经有请求该镜像库的资源,对于未从该镜像库下载过的资源是无法查询到的。那么在Nexus 3 搭建初期,没有使用前,在该系统是查询不到任何资源的,需要先使用程序去调用该镜像,才会有资源使用。

3.  Nexus 3 暂时不支持下载远程镜像的索引。只有当Maven中设置了Nexus 3 的资源库,Nexus 3 会在具体编译项目的时候先从远程下载对应的资源到本地,然后传输给各个客户端使用。 这样也就不用跟Nexus 2 中那样再【Repair Index】来更新索引了。

 

二、Maven 配置Nexus 镜像

   Nexus 镜像搭建起来后,那么如何在Maven中使用该镜像资源呢? 主要是通过调整maven配置来识别该镜像资源,从而将maven资源请求转发到该镜像资源库上。通常的做法为在当前用户的目录下.m2文件夹中创建settings.xml文件,其中指定mirror,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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">

   <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
  <mirrors>
      <mirror>
      <id>mirror_name</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus_ip:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>
</settings>

  上述配置信息是所有的远程资源镜像的访问都会通过该镜像来访问,这也是大部分公司采用的方案,因为这样能够保证公司对远程资源的访问次数是有限的,公司内部的开发人员访问的都是公司内部的资源。开发人员不需要链接外网去下载远程镜像服务器上的资源,只需要该公司内部镜像去访问一次即可。当然了,mirrorOf可以指定为代理部分远程资源,详情见官方说明:http://maven.apache.org/guides/mini/guide-mirror-settings.html

 三、Nexus 启用上传功能

  团队有了私有的镜像服务器以后,团队内部代码的依赖也就不再需要源码依赖了,大家可以通过发布不同版本的jar到nexus镜像上来供调用者直接通过Maven下载使用,这样不同研发人员直接的依赖也就没有那么强了,大家可以基于已经发布的版本进行各自的开发。

  那么如何发布个人的jar资源到团队内部镜像上呢? 

  1. 在Nexus 中创建一个developer的角色,拥有的权利为【nx-repository-view-maven2-*-edit】和【nx-repository-view-maven2-*-add】权利,如果该角色将来可能还有nuget,npm相关上传权利,则将其权利改为【nx-repository-view-*-*-edit】和【nx-repository-view-*-*-add】权利。

  2. 创建用户,用户拥有的角色为【nx-anonymous】和刚创建的【developer】角色。其中nx-anonymous角色是nexus默认自带的角色。

  3. 在.m2文件夹下的settings.xml配置文件中增加<servers>的配置。

<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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">

    <!-- 配置Maven服务器的账号信息,自动化部署的时候需要用到 -->
  <servers>
    <server>
      <id>server_id</id>
      <!-- 使用公共的developer/password账号进行日常的发布管理 -->
      <username>developer</username>
      <password>password</password>
    </server>
  </servers>

   <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
  <mirrors>
      <mirror>
      <id>mirror_name</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus_ip:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>
</settings>

  4.  在需要上传jar资源的项目的pom.xml中增加<distributionManagement>配置,其中<id>设置需要跟.m2文件夹下的settings.xml中<servers>下的id相同。<url>需要指定nexus中配置的hosts Repository资源的地址。

    <distributionManagement>
        <repository>
            <id>server_id</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus_ip:8081/repository/host-releases/</url>
        </repository>
    </distributionManagement>

  5.  通过maven 编译项目,并通过mvn deploy 来发布jar资源到团队内部的镜像服务器即可。

 

 四、Nexus 启用SNAPSHOTS

  团队内部在开发过程中为了相互可以互不影响的开发,需要时常将未稳定版的jar发布出来供团队其他人员调用,这时候建议使用SNAPSHOT版本,那么SNAPSHOT版本怎么发布到Nexus上呢。如果以当前的配置,发布SNAPSHOT过程会失败,因为Nexus默认是不启用SNAPSHOT的。那么怎么启用SNAPSHOT及如何上传SNAPSHOT版本资源呢?

  启用SNAPSHOT的方式为在.m2文件夹下的settings.xml中增加<profile>设置

 <!-- 这个默认配置决定了我们的Maven服务器开启snapshot配置,否则不能下载SNAPSHOTS的相关资源 --> 
 <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
          <id>repository_name</id>
          <name>Nexus Public Repository</name>
          <url>http://nexus_ip:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

    增加这个配置后,那么是否就可以直接上传SNAPSHOT版本的jar资源了呢?经过测试还是不行,还需要有其他配置。

  1.  Nexus Repository中增加一个hosted类型的Repository,Maven的资源类型为SNAPSHOT(Nexus默认已经存在一个这种类型的资源)。

  2.  settings.xml的<servers>增加snapshot server的配置,settings.xml的最终配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- for full reference, see also http://maven.apache.org/ref/3.2.3/maven-settings/settings.html -->
<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">

  <!-- 配置Maven服务器的账号信息,自动化部署的时候需要用到 -->
  <servers>
   <server>
      <id>nexus-releases</id>
      <!-- 使用公共的developer/password账号进行日常的发布管理 -->
      <username>developer</username>
      <password>password</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <!-- 使用公共的developer/password账号进行日常的发布管理 -->
      <username>developer</username>
      <password>password</password>
    </server>
  </servers>

  <!-- 使用Mirror配置节可以强制所有包请求都会被转向内网Nexus服务器的地址 -->
  <mirrors>
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <url>http://nexus_ip:8081/repository/maven-public/</url>
    </mirror>
  </mirrors>

  <!-- 这个默认配置决定了我们的Maven服务器开启snapshot配置,否则不能下载SNAPSHOTS的相关资源 --> 
 <profiles>
    <profile>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <repositories>
        <repository>
          <id>repository_name</id>
          <name>Nexus Public Repository</name>
          <url>http://nexus_ip:8081/repository/maven-public/</url>
          <releases>
            <enabled>true</enabled>
          </releases>
          <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>
</settings>

  3. 需要发布jar资源到团队内部镜像服务器的项目的pom.xml配置<distributionManagement>增加snapshot的支持,最终的pom.xml增加的<distributionManagement>如下:

    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://nexus_ip:8081/repository/yoyi-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://nexus_ip:8081/repository/yoyi-snapshots/</url>
        </snapshotRepository>
    </distributionManagement>

  

  通过上述各种配置,即可以发布jar到团队内部的镜像服务器了。 其中distributionManagement需要配置在各个项目的pom.xml文件中,如果多个项目都需要使用,是否可以将<distributionManagement>放到.m2文件夹下的settings.xml中呢?经过测试,放到settings.xml中是不起作用的。那么如果在多个项目中使用的方案为多个项目创建一个父项目,将各个项目作为模块加载到父项目中,只在父项目中配置distributionManagement即可。如果各位看客有更好的方案,还望能在留言中告知下。^-^

  五、异常情况处理

*UNKNOWN com.sonatype.nexus.plugins.outreach.internal.outreach.SonatypeOutreach - Could not download page bundle
org.apache.http.conn.HttpHostConnectException: Connect to sonatype-download.global.ssl.fastly.net:443 [sonatype-download.global.ssl.fastly.net/69.171.245.49] failed: 连接超时
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:151) [httpcore:0.0.0]
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184) [httpcore:0.0.0]
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88) [httpcore:0.0.0]

  处理方法:登录账号,打开【System】--》【Capabilities】,将【Outreach:Management】禁用即可。

posted on 2017-06-19 14:16  呵呵鱼  阅读(28045)  评论(3编辑  收藏  举报