Maven-004-使用 Nexus 搭建 maven 私服

从去年至今,自己一直在学习自动化测试工具,想利用自动化工具尽可能的将重复的、关键的、耗时耗力的工作实现自动化,减轻日常测试工作,提升测试效率。在学习的过程中,将 maven 作为了项目开发管理工具,进行分模块开发。初始的时候,自己一个人想怎么搞就怎么搞。。。之前给同时做了分享,并引入公司,大家一起努力去完善系统。悲剧的是,最近公司限网,从 maven 中央仓库下载所需的 jar 包,需要外网的支持,而使用相同的 jar 时,每人均重复下载浪费网络资源,并且也影响项目构建的速度,因而想构建一个公司的 maven 私服解决这一问题,咨询了一下度娘、谷大爷决定选择 nexus 搭建 maven 私服。

 

一、下载安装

点击 Nexus 下载,下载 Nexus,此处下载版本为:nexus-2.12.0-01-bundle.zip,文件内容如下所示:

  

nexus-2.12.0-01:为nexus应用目录。

sonatype-work:为 nexus 默认的存储目录。

 

二、启动

将下载的 zip 文件解压到本地目录,并进入目录 nexus-2.12.0-01\bin,打开命令行窗口执行 nexus.bat install 命令,安装系统服务 nexus。

无需进入 nexus-2.12.0-01\bin\jsw 目录选择对应的系统版本安装启动,因为在启动脚本 nexus.bat/nexus 中已经做出了相应的处理,会依系统启动对应的 nexus 版本。

安装成功后,系统服务中会存在 nexus 的服务,如下所示:

  

  可在服务中启动 nexus,也可在命令行中执行 nexus.bat start 命令,启动 nexus。若出现如下信息,则说明 nexus 启动成功。

 

  打开浏览器,输入:http://localhost:8081/nexus 即可访问 nexus,如下所示:

   

  

  nexus 配置文件为:nexus-2.12.0-01\conf\nexus.properties,可通过修改配置文件,修改其对应的端口、工作目录等信息,修改后重启服务接口。

# Sonatype Nexus
# ==============
# This is the most basic configuration of Nexus.

# Jetty section
application-port=8081
application-host=0.0.0.0
nexus-webapp=${bundleBasedir}/nexus
nexus-webapp-context-path=/nexus

# Nexus section
nexus-work=${bundleBasedir}/../sonatype-work/nexus
runtime=${bundleBasedir}/nexus/WEB-INF

 

   点击 nexus 欢迎页面右上角的 【Log In】按钮,输入用户名、密码登录系统(系统默认的管理员账号/密码为:admin/admin123),登录后点击导航栏的【Repositories】链接,查看 nexus 内置仓库,如下图示所示:

  

内置仓库:

【Public Repositories】:仓库组,默认内置。

【3rd party】:宿主仓库,用来部署无法从公共仓库获得的第三方发布版本构件,策略Release。

【Apache Snapshots】:代理仓库,用来代理Apache Maven仓库的快照版本,策略为Snapshot,不可发布。

【Central】:代理仓库,默认代理maven中央仓库,策略为Release,只会下载和缓存中央仓库的发布版本构件。不可发布。

【Releases】:宿主仓库,用来部署组织内部的发布版本构建,策略为Release。

【Snapshots】:宿主仓库,用来部署组织内部的快照版本构建,策略为Snapshot。

 

  三、配置

  Central 代理仓库开启远程索引下载:

  点击左侧导航栏【Scheduled Tasks】连接,可查看进度,如下所示:

  

  更新完成后,即可在左侧导航栏【Artifact Search】搜索所需要的构件,如下所示:

  

  

  设置 Release 可重复发布部署构件:

  

  

  配置 maven 从 Nexus 下载构件:

  在 maven 的 setting.xml 文件的 profiles 节点下添加如下配置:

<settings>
...
<profiles>
    <!-- 配置 maven 从私服下载构件 -->
    <profile>
        <id>nexus</id>
        
        <repositories>
            <repository>
                <id>nexus</id>
                <name>Nexus</name>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </repository>
        </repositories>
        
        <pluginRepositories>
            <pluginRepository>
                <id>nexus</id>
                <name>Nexus</name>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                
                <releases>
                    <enabled>true</enabled>
                </releases>
                <snapshots>
                    <enabled>true</enabled>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
</profile>
</profiles>
...
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

  配置中使用了一个 id 为 nexus 的profile,这个 profile 包含了相关的仓库配置(配置了 nexus 的 groups 仓库组),同时配置中使用了 activeProfile 元素将 nexus 这个 profile 激活。当执行 maven 构建的时候,激活的 profile 会将仓库配置应用到项目中。

  maven 除了从 Nexus 中下载构件外,还会不时的访问中央仓库 central,如何使 maven 所有下载请求都仅仅通过 nexus,以全面发挥 nexus 私服的作用?可通过配置 maven 的镜像实现。通过创建一个匹配任何仓库的镜像地址为私服,即可将 maven 对任何仓库的构件下载请求都会转到 nexus 私服中,具体配置如下所示:

<settings>
...
<mirrors>
    <!-- 配置 maven 所有下载请求仅仅通过 nexus -->
    <mirror>
      <id>nexus</id>
      <mirrorOf>*</mirrorOf>
      <name>Nexus - Maven</name>
      <url>http://localhost:8081/nexus/content/groups/public/</url>
    </mirror>
</mirrors>

<profiles>
    <!-- 配置 maven 从私服下载构件 -->
    <profile>
        <id>nexus</id>
        
        <repositories>
            <repository>
                <id>central</id>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                
                <releases>
                    <enable>true</enable>
                </releases>
                <snapshots>
                    <enable>true</enable>
                </snapshots>
            </repository>
        </repositories>
        
        <pluginRepositories>
            <pluginRepository>
                <id>central</id>
                <url>http://localhost:8081/nexus/content/groups/public/</url>
                
                <releases>
                    <enable>true</enable>
                </releases>
                <snapshots>
                    <enable>true</enable>
                </snapshots>
            </pluginRepository>
        </pluginRepositories>
</profile>
</profiles>
...
<activeProfiles>
    <activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>

    上述配置中,仓库及插件仓库的 id 均为 central,也就是说覆盖了超级 POM 中央仓库的配置,此时 url 已经无关紧要,因为此时所有请求都会通过镜像访问 nexus 私服地址。配置仓库及插件仓库的主要目的是开启对快照版本下载的支持,当 maven 需要下载发布版或快照版构件的时候,首先会检查 central,查看该类型的构件是否支持,然后根据镜像匹配规则转而访问 nexus 私服仓库地址。

 

至此, Maven-004-使用 Nexus 搭建 maven 私服 顺利完结,希望此文能够给初学 Maven 的您一份参考。

最后,非常感谢亲的驻足,希望此文能对亲有所帮助。热烈欢迎亲一起探讨,共同进步。非常感谢! ^_^

 

posted @ 2016-02-16 20:01  范丰平  Views(356)  Comments(0Edit  收藏  举报