使用nexus替代artifactory作爲maven私服

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:

    <profile>
      <id>nexus</id>
      <repositories>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </repository>
        <repository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public</url>
            <releases><enabled>true</enabled></releases>
            <snapshots><enabled>false</enabled></snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>nexus</id>
            <name>local private nexus</name>
            <url>http://localhost:8081/nexus/content/groups/public-snapshots</url>
            <releases><enabled>false</enabled></releases>
            <snapshots><enabled>true</enabled></snapshots>
        </pluginRepository>
       </pluginRepositories>
    </profile> 
    修改activeProfiles爲:
      <activeProfiles>
        <activeProfile>nexus</activeProfile>
      </activeProfiles>     
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 -> show view -> other ... -> Maven -> 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文件,右鍵 -> Add Dependency, 然後填入一個關鍵詞,比如我填入mina,馬上填出和mina相關的一些
        選擇,我找到apache mina,雙擊最新一個版本。會自動在pom.xml文件中增加以下內容:

        <dependency>
            <groupId>org.apache.directory.mina</groupId>
            <artifactId>mina-core</artifactId>
            <version>0.9.5</version>
        </dependency>
        然後Maven自動下載jar包,再將jar包加入項目的build path,全程自動化處理,真是爽啊。

7. 爲nexus增加新的proxy  repository
        方法很簡單,administration -> Repositories -> add -> 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居然沒有。
8. 總結
    很明顯,nexus無論是在界面,功能,操作上,都比artifactory強大的多。
    因此推薦大家使用nexus替代artifactory作爲maven私服。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章