---
title: "初学maven(3)-使用nexus替代artifactory作为maven私服"
author: "skydream"
date: "2008-09-06T23:16:00Z"
tags: ["JBoss", "Mina", "SUN", "Windows", "maven"]
slug: "maven-3-nexus-artifactory-maven"
draft: false
lastmod: "2009-08-02T11:14:26Z"
id: 437942
url: "https://skydream.iteye.com/blog/437942"
---

之前看到过一些Nexus的介绍，由于刚开始接触maven时使用的私服是artifactory，因此没有太在意。今天想着既然Nexus能有胆量出来混，应该有点真本事才是，看了一下nexus的安装介绍，挺简单的，试试无妨。因此装上小试了一下，结果喜出望外，nexus的表现非常不错，尤其是在开启远程索引之后，简直太方便了。

    于是决定放弃artifactory改而使用nexus作为自己的maven私服。恩，惭愧，颇有点喜新厌旧的味道，artifactory才装上来没有几天，就惨遭抛弃......

    整理了一下，全过程记录如下：

1\. 首先下载Nexus  
    从官网http://nexus.sonatype.org/download.html下载下载最新版本，因为是在windows上安装，因此下载的是zip版本，大小大概是16m。  
     
2\. 安装  
    简单解压缩下载的zip包到安装目录就可以了。  
    可执行文件在%nexus安装目录%\\nexus-webapp-1.0.0\\binjsw\\windows-x86-32下：  
        InstallNexus.bat/UninstallNexus.bat是安装/卸载nexus为windows service，如果需要设置nexus为开机自动启动就可以安装为windows service然后设置启动方式为自动。  
        Nexus.bat是直接在命令行中启动Nexus，如果不想安装Nexus为windows service，可以用这个文件来手工控制Nexus的启动退出。  
         
         
3\. 配置nexus

    首先登录，默认地址http://localhost:8081/nexus/，默认用户名密码为admin/admin123.  
     
    最重要的一件事情就是开启远程索引下载，索引这个功能实在是太好用了。  
     
    nexus默认是关闭远程索引下载功能的，主要是担心会造成对服务器的巨大负担，需要我们手工开启。  
     
    开启的方式：  
        点击Administration菜单下面的Repositories，将这三个仓库Apache Snapshots，Codehaus Snapshots，Maven Central的Download Remote Indexes修改为true。然后在这三个仓库上分别右键，选择Re-index，这样Nexus就会去下载远程的索引文件。  
         
4\. 配置maven  
    要让maven使用nexus作为私服，需要做一些设置，使用和原来设置artifactory相似的方法。修改~/.m2/settings.xml.  
     
    增加nexus的profile：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;     &lt; profile &gt;  
       &lt; id &gt; nexus &lt;/ id &gt;  
       &lt; repositories &gt;  
         &lt; repository &gt;  
             &lt; id &gt; nexus &lt;/ id &gt;  
             &lt; name &gt; local private nexus &lt;/ name &gt;  
             &lt; url &gt; http://localhost:8081/nexus/content/groups/public &lt;/ url &gt;  
             &lt; releases &gt;&lt; enabled &gt; true &lt;/ enabled &gt;&lt;/ releases &gt;  
             &lt; snapshots &gt;&lt; enabled &gt; false &lt;/ enabled &gt;&lt;/ snapshots &gt;  
         &lt;/ repository &gt;  
         &lt; repository &gt;  
             &lt; id &gt; nexus &lt;/ id &gt;  
             &lt; name &gt; local private nexus &lt;/ name &gt;  
             &lt; url &gt; http://localhost:8081/nexus/content/groups/public-snapshots &lt;/ url &gt;  
             &lt; releases &gt;&lt; enabled &gt; false &lt;/ enabled &gt;&lt;/ releases &gt;  
             &lt; snapshots &gt;&lt; enabled &gt; true &lt;/ enabled &gt;&lt;/ snapshots &gt;  
         &lt;/ repository &gt;  
       &lt;/ repositories &gt;  
       &lt; pluginRepositories &gt;  
         &lt; pluginRepository &gt;  
             &lt; id &gt; nexus &lt;/ id &gt;  
             &lt; name &gt; local private nexus &lt;/ name &gt;  
             &lt; url &gt; http://localhost:8081/nexus/content/groups/public &lt;/ url &gt;  
             &lt; releases &gt;&lt; enabled &gt; true &lt;/ enabled &gt;&lt;/ releases &gt;  
             &lt; snapshots &gt;&lt; enabled &gt; false &lt;/ enabled &gt;&lt;/ snapshots &gt;  
         &lt;/ pluginRepository &gt;  
         &lt; pluginRepository &gt;  
             &lt; id &gt; nexus &lt;/ id &gt;  
             &lt; name &gt; local private nexus &lt;/ name &gt;  
             &lt; url &gt; http://localhost:8081/nexus/content/groups/public-snapshots &lt;/ url &gt;  
             &lt; releases &gt;&lt; enabled &gt; false &lt;/ enabled &gt;&lt;/ releases &gt;  
             &lt; snapshots &gt;&lt; enabled &gt; true &lt;/ enabled &gt;&lt;/ snapshots &gt;  
         &lt;/ pluginRepository &gt;  
        &lt;/ pluginRepositories &gt;  
     &lt;/ profile &gt;

   
    修改activeProfiles为：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;       &lt; activeProfiles &gt;  
         &lt; activeProfile &gt; nexus &lt;/ activeProfile &gt;  
       &lt;/ activeProfiles &gt;

       
5\. 为nexus增加Artifact  
    有些特殊的Artifact无法从maven官网仓库中下载，比如sun的一些包，只好自己自行下载后添加到私服中。  
    在nexus中我选择将这些Artifact上传到默认安装就存在的仓库3rd Party中，右击仓库名，选择Upload Artifact。  
     
6\. 在eclipse中使用索引功能  
    原来试过，使用artifactory私服也可以使用Artifact的索引功能，但是由于不知道怎么设置远程仓库的index，我只会设置  
    当前artifactory私服已有的Artifact的索引，对于还没有导入到artifactory私服的Artifact就没有办法索引了，很不方便，  
    毕竟刚开始使用maven时，所有用到的Artifact都是本地和私服没有而需要到远程仓库取的。  
     
    nexus中可以很方便的得到远程仓库的Artifact的索引，在上面“3. 配置nexus”就介绍过。下面介绍如何在eclispe里面  
    设置和使用索引功能：  
    1) 打开Maven Indexes 的eclispe view  
        在eclispe中选择window -&gt; show view -&gt; other ... -&gt; Maven -&gt; Maven Indexes  
    2) 添加nexus的index  
        右键菜单中选"add index", 在弹出的"Add Respository index"窗口中填入：  
            Repository URL: http://localhost:8081/nexus/content/groups/public  
            Repository Id: nexus  
            Index Update URL: 放空，暂时还不知道该怎么填  
        加入后eclispe会自动load一次index信息，然后就可以在新加入的index下可以拉出极大数量的Artifact信息。  
    3) 测试一下使用  
        找个pom.xml文件，右键 -&gt; Add Dependency， 然后填入一个关键词，比如我填入mina，马上填出和mina相关的一些  
        选择，我找到apache mina，双击最新一个版本。会自动在pom.xml文件中增加以下内容：

&lt;!--&lt;br /&gt; &lt;br /&gt; Code highlighting produced by Actipro CodeHighlighter (freeware)&lt;br /&gt; http://www.CodeHighlighter.com/&lt;br /&gt; &lt;br /&gt; --&gt;         &lt; dependency &gt;  
             &lt; groupId &gt; org.apache.directory.mina &lt;/ groupId &gt;  
             &lt; artifactId &gt; mina-core &lt;/ artifactId &gt;  
             &lt; version &gt; 0.9.5 &lt;/ version &gt;  
         &lt;/ dependency &gt;

        然后Maven自动下载jar包，再将jar包加入项目的build path，全程自动化处理，真是爽啊。

7\. 为nexus增加新的proxy  repository  
        方法很简单，administration -&gt; Repositories -&gt; add -&gt; proxy，填写后保存即可。但是要注意，nexus不会自动将新加入的repository添加到group中，而我们一般喜欢直接使用默认的"public repository" group， 比如前面我在maven的profile中就只设置了这一个URL： http://localhost:8081/nexus/content/groups/public 。因此需要手工修改"public repository" group的设置，将刚才添加的proxy  repository加到组中。  
        推荐的repository有：  
                1) jboss         http://repository.jboss.com/maven2/  
                2) sun            http://download.java.net/maven/2/  
                3)k-int           http://developer.k-int.com/maven2/  
                        加入这个纯粹是因为它有sun的jmxri/jmxtools这些Artifact，强烈鄙视sun，自己的官方repository居然没有。  
                4)sonatype        http://repository.sonatype.org/content/groups/public/  
8\. 总结  
    很明显，nexus无论是在界面，功能，操作上，都比artifactory强大的多。  
    因此推荐大家使用nexus替代artifactory作为maven私服。  
     
updates:  
1.  2008.11.28   
由于公司升级操作系统，告别老旧不堪的windows2000升级到vista，因此重新安装了nexus. 新版本的nexus似乎增加了不少小的功能比如对remote index的支持，具体没有深究，不过能不断更新实在是很令人欣慰。以后就打算用nexus了。