Nexus是一个强大的Maven仓库管理器,它极大地简化了自己内部仓库的维护和外部仓库的访问。利用Nexus你可以只在一个地方就能够完全控制访问和部署在你所维护仓库中的每个Artifact。Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据。Nexus 使用ExtJS来开发界面,利用Restlet来提供完整的REST APIs,通过m2eclipse与Eclipse集成使用。Nexus支持WebDAV与LDAP安全身份认证。

来自<http://www.oschina.net/p/nexus>

安装Nexus

下载

Nexus有两种安装包,一种是包含Jetty容器的Bundle包,另一种是不包含Web容器的war包。我们可以从http://www.sonatype.org/downloads/nexus-latest-bundle.zip下载最新版本的Nexus的Bundle包。

解压nexus-latest-bundle.zip,会发现有两个文件夹。如图1:

图1

nexus-2.7.2-03目录包含了Nexus运行所需要的文件。是Nexus运行必须的。

sonatype-work目录包含Nexus生成的配置文件、日志文件、仓库文件等。当我们需要备份Nexus的时候默认备份此目录即可。

安装

进入目录nexus-2.7.2-03\bin\jsw,你会发现有很多的安装环境。我的操作系统是64位的Windows 8,那么进入windows-x86-64目录。有以下文件,如图2:

图2

install-nexus.bat:将Nexus服务安装到Windows系统。

start-nexus.bat:启动Nexus服务。

stop-nexus.bat:停止Nexus服务。

uninstall-nexus.bat:卸载Nexus服务。

右击install-nexus.bat“以管理员身份运行”运行文件,右击start-nexus.bat“以管理员身份运行”运行文件。在“服务”中,我们可以看到Nexus已经安装并且运行起来了,图3:

图3

安装之前要先安装JDK,并在环境变量中配置JAVA_HOME和PATH。

登录

在浏览器中输入:http://localhost:8081/nexus/得到以下界面,见图4:

图4

单击右上角的“Log In”按钮进行登录,Nexus的默认管理员账户密码为admin/admin123。

配置Nexus

Nexus仓库分类

Nexus包含了各种类型的仓库类型。在登录后的Nexus界面,单击左边的“Repositories”链接,可以看到图5所示:

图5

我们可以从上图中看到四种仓库类型:group(仓库组)、hosted(宿主)、proxy(代理)和virtual(虚拟)。其中每种类型的Format有Maven1或者Maven2,这里我们不看Maven1的。仓库的Policy(策略)表示该仓库为发布(Release)版本还是快照(Snapshot)版本仓库。

在图5中,Nexus列出了默认的几个仓库:

Public Repositories:仓库组,将所有策略为Release的仓库聚合并通过一致的地址提供服务。

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

Apache Snapshots:策略为Snapshots的代理仓库,用来代理Apache Maven仓库的快照版本构件。

Central:该仓库代理Maven的中央仓库,策略为Release,只会下载和缓存中央仓库中的发布版本构件。

Central M1 shadow:maven1格式的虚拟类型仓库。

Codehaus Snapshots:代理Codehaus Maven仓库快照版本的代理仓库。

Release:策略为Release的宿主类型仓库,用来部署组织内部的发布版本构件。

Snapshots:策略为Snapshots的宿主类型仓库,用来部署组织内部的快照版本构件。

用《Maven实战》中一张图来解释下仓库的分类:

图6

私有仓库配置

Nexus的索引与构件搜索

点击图5中列表上的“Public Repositories”行,在下方的“Configuration”中我们可以看到,在“Ordered Group Repositories”中包含了Release、Snapshots、3rd party、Central等仓库。

为了构建Nexus的Maven中央库索引,首先需要设置Nexus中Maven Cencal代理仓库下载远程索引,将“Download Remote Indexes”的值从默认值false改为true。如图7:

图7

点击“Save”后,点击左边的“Scheduled Tasks”链接,会出现图8界面,说明Nexus后台在下载Maven中央仓库的索引。

图8

如果没有出现图8界面,那么要在“Public Repositories”行右击,点击“Update Index”。

图9

在“Repositories”界面中,选择Browse Index选项卡,可以看到Maven中央仓库内容的树形结构,如图10。

图10

如果还是构建不成功,参考:http://blog.csdn.net/mtkong/article/details/9377605

http://397013586.iteye.com/blog/1558492

在左边的搜索框中输入“spring”关键字,会出现一大堆与spring相关的结果。

如果要对宿主仓库和代理仓库建立索引。只需要在对应的仓库上右击,从弹出的快捷菜单中选择“ReIndex”即可。

配置Maven从Nexus下载构件

在上面的基础上,我们可以用Nexus私服了。有2种方式:

1、在项目的POM中配置

<repositories>
<repository>
<id>local_nexus</id>
<name>local_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>local_nexus</id>
<name>local_nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>

这样的配置只对当前的Maven项目有效。但是,实际的项目开发中,我们都想着一次配置就能让本机的所有Maven项目都使用Maven私服,那么我们就要用到下面的方法。

2、在settings.xml中配置远程仓库。

Maven提供的profile是一组可选的配置,可以用来设置或者覆盖配置默认值。有了profile,你就可以为不同的环境定制构建。那么在settings.xml中加入以下代码:

<settings>
...
<profiles>
<profile>
<id>local_nexus</id>
<repositories>
<repository>
<id>local_nexus</id>
<name>local_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>local_nexus</id>
<name>local_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>local_nexus</activeProfile>
</activeProfiles>
...
</settings>

上面的配置中,使用了一个id为local_nexus的profile,这个profile包含了相关的仓库配置,同时配置中又使用了activeProfiles元素将nexus这个profile激活,这样当执行Maven构建的时候,激活的profile会将仓库配置应用到项目中去。

通过上面的配置,我们会发现Maven除了从Nexus下载构件外还会从中央仓库下载构件构件。既然是私服,那么我们就只希望Maven下载请求都仅仅通过Nexus。我们可以通过镜像实现这一需求。可以创建一个匹配任何仓库的镜像,镜像的地址是私服,这样Maven对任何仓库的构件下载请求都会转到私服中。把上面的配置修改为如下配置:

<settings>
...
<mirrors>
<mirror>
<id>local_mirror</id>
<mirrorOf>*</mirrorOf>
<name>local_mirror</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
</mirror>
</mirrors> <profiles>
<profile>
<id>local_nexus</id>
<repositories>
<repository>
<id>local_nexus</id>
<name>local_nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>central</id>
<!--由于配置过镜像,这个url不起作用-->
<url>http://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local_nexus</id>
<name>local_nexus</name>
<url>http://localhost:8081/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>central</id>
<url>http://repo.maven.apache.org/maven2</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>local_nexus</activeProfile>
</activeProfiles>
...
</settings>

部署构件到私服

我们在实际开发过程是多个人的,那么总有一些公共模块或者说第三方构件是无法从Maven中央库下载的。我们需要将这些构件部署到私服上,供其他开发人员下载。用户可以配置Maven自动部署构件至Nexus的宿主仓库,也可以通过界面手动上传构件。

使用Maven部署构件到Nexus私服上

日常开发的快照版本部署到Nexus中策略为Snapshot的宿主仓库中,正式项目部署到策略为Release的宿主仓库中,POM的配置方式如下:

...
<distributionManagement>
<repository>
<id>local_nexus_releases</id>
<name>core Release Repository</name>
<url>http://localhost:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>local_nexus_snapshots</id>
<name>core Snapshots Repository</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
...

Nexus的仓库对于匿名用户只是只读的。为了能够部署构件,我们还需要再settings.xml中配置验证信息:

...
<servers>
<server>
<id>local_nexus_releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>local_nexus_snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
...

其中,验证信息中service的id应该与POM中repository的id一致。

在Nexus界面上手动部署第三方构件至私服

我们除了自己的构件要部署到Nexus私服上外,我们有可能还要将第三方构件(如:SQLService的JDBC)部署到Nexus上。这个时候,在Nexus界面上选择一个宿主仓库(如3rd party),再在页面下方选择Artifact Upload选项卡。填写对应的Maven坐标。然后点击“Select Artifact(s) for Upload”按钮从本机选择要上传的构件,然后点击“Add Artifact”按钮将其加入到上传列表中。最后,单击页面底部的“Upload Artifact(s)”按钮将构件上传到仓库中。

使用Nexus创建Maven私服的更多相关文章

  1. (十五)使用Nexus创建Maven私服

    通过建立自己的私服,就可以降低中央仓库负荷.节省外网宽带.加速Maven构建.自己部署构件等,从而高效的使用Maven.有三种专门的Maven仓库管理软件可以用来帮助大家建立私服:Apache基金会的 ...

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

    从去年至今,自己一直在学习自动化测试工具,想利用自动化工具尽可能的将重复的.关键的.耗时耗力的工作实现自动化,减轻日常测试工作,提升测试效率.在学习的过程中,将 maven 作为了项目开发管理工具,进 ...

  3. Maven使用笔记(五)Sonatype Nexus 搭建Maven 私服

    1. 为什么使用Nexus 如果没有私服,我们所需的所有构件都需要通过maven的中央仓库和第三方的Maven仓库下载到本地, 而一个团队中的所有人都重复的从maven仓库下载构件无疑加大了仓库的负载 ...

  4. ubuntu下使用Nexus搭建Maven私服

    ubuntu下使用Nexus搭建Maven私服 1.私服简介: 私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件.有了私服之后,当 Maven 需要下载构件时,直接请求私服, ...

  5. 使用Sonatype Nexus搭建Maven私服后,如何添加第三方JAR包

    使用Sonatype Nexus搭建Maven私服后如何添加第三方JAR包 步骤如下 1.打开nexus地址,进行登录 2.登录Nexus后,点击右侧的“Repositories”,显示当前Nexus ...

  6. Maven 使用Nexus搭建Maven私服

    Maven学习 (四) 使用Nexus搭建Maven私服 为什么要搭建nexus私服,原因很简单,有些公司都不提供外网给项目组人员,因此就不能使用maven访问远程的仓库地址,所以很有必要在局域网里找 ...

  7. Windows使用Nexus搭建Maven私服

    简介 Maven私服是架设在局域网的一种特殊的远程仓库,目的是代理远程仓库及部署第三方构件,有了私服之后,当 Maven 需要下载构件时,直接请求私服,私服上存在则下载到本地仓库,否则,私服请求外部的 ...

  8. Maven——使用Nexus搭建Maven私服

    原文:http://www.cnblogs.com/xdp-gacl/p/4068967.html Maven学习总结(九)--使用Nexus搭建Maven私服 一.搭建nexus私服的目的 为什么要 ...

  9. 配置maven从自己的私服下载jar包nexus、maven私服仓库(二)

    配置maven项目从私服下载jar包 pom文件配置从maven私服下载jar包 settings文件配置从maven私服下载jar包 (方便自己关键字搜索,所以多写了几行o(* ̄︶ ̄*)o) 今天自 ...

随机推荐

  1. PSR : php编码规范

    诸王混战 关于开发标准这块,可以说一直都是风格迥异,各家都有各家的玩法,民间更是个人玩个人的.目前我们国内比较出名的几个框架(Yii,Laravel) 都已经支持Composer并且加入了PHP-FI ...

  2. [Virtualization][qemu][kvm][virtio] 使用 QEMU/KVM 模拟网卡多队列

    序: 做DPDK例子的时候,发现一些例子需要多队列,而我当前所使用的虚拟机并不是多队列的.关于我当前虚拟机的状态,可以见前文. 所以,我的需求就是,让虚拟机里的网卡,有多队列! 参考: http:// ...

  3. $(document).ready(function(){});

    $(document).ready 里的代码是在页面内容都加载完才执行的,你直接写到script标签里,当页面加载完这个script标签就会执行里边的代码了,如果你标签里执行的代码调用了当前还没加载过 ...

  4. PHP 多维数组 Key Value的使用

    <?php $user["60"] = array("id" => "60", "num" => &q ...

  5. 去bom头

    function rewrite ($filename, $data) { $filenum = fopen($filename, "w"); flock($filenum, LO ...

  6. lua2c

    lua2c lua2c is a Lua module and utility to convert Lua 5.1 source code to C API code. http://lua-use ...

  7. JQuery1.11版本对prop和attr接口的含义分离导致问题分析

    问题背景 实验中, 在jquery1.7版本, attr("value")  和 val() 接口获取 input 控件的值, 都是一致的, 都是当前控件值. 但是 jquery1 ...

  8. Leetcode: Largest BST Subtree

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  9. 50个常用的JQuery代码

    1. 如何创建嵌套的过滤器 //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class为“se ...

  10. Notepad++的列编辑功能

    转自:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/index.html http://www.crifan.c ...