之前看到過一些Nexus的介紹,由于剛開始接觸maven時使用的私服是artifactory,因此沒有太在意。今天想著既然Nexus能有膽量出來混,應(yīng)該有點(diǎn)真本事才是,看了一下nexus的安裝介紹,挺簡單的,試試無妨。因此裝上小試了一下,結(jié)果喜出望外,nexus的表現(xiàn)非常不錯,尤其是在開啟遠(yuǎn)程索引之后,簡直太方便了。
于是決定放棄artifactory改而使用nexus作為自己的maven私服。恩,慚愧,頗有點(diǎn)喜新厭舊的味道,artifactory才裝上來沒有幾天,就慘遭拋棄......
整理了一下,全過程記錄如下:
1. 首先下載Nexus
從官網(wǎng)http://nexus.sonatype.org/download.html下載下載最新版本,因?yàn)槭窃趙indows上安裝,因此下載的是zip版本,大小大概是16m。
2. 安裝
簡單解壓縮下載的zip包到安裝目錄就可以了。
可執(zhí)行文件在%nexus安裝目錄%\nexus-webapp-1.0.0\binjsw\windows-x86-32下:
InstallNexus.bat/UninstallNexus.bat是安裝/卸載nexus為windows service,如果需要設(shè)置nexus為開機(jī)自動啟動就可以安裝為windows service然后設(shè)置啟動方式為自動。
Nexus.bat是直接在命令行中啟動Nexus,如果不想安裝Nexus為windows service,可以用這個文件來手工控制Nexus的啟動退出。
3. 配置nexus
首先登錄,默認(rèn)地址http://localhost:8081/nexus/,默認(rèn)用戶名密碼為admin/admin123.
最重要的一件事情就是開啟遠(yuǎn)程索引下載,索引這個功能實(shí)在是太好用了。
nexus默認(rèn)是關(guān)閉遠(yuǎn)程索引下載功能的,主要是擔(dān)心會造成對服務(wù)器的巨大負(fù)擔(dān),需要我們手工開啟。
開啟的方式:
點(diǎn)擊Administration菜單下面的Repositories,將這三個倉庫Apache Snapshots,Codehaus Snapshots,Maven Central的Download Remote Indexes修改為true。然后在這三個倉庫上分別右鍵,選擇Re-index,這樣Nexus就會去下載遠(yuǎn)程的索引文件。
4. 配置maven
要讓maven使用nexus作為私服,需要做一些設(shè)置,使用和原來設(shè)置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官網(wǎng)倉庫中下載,比如sun的一些包,只好自己自行下載后添加到私服中。
在nexus中我選擇將這些Artifact上傳到默認(rèn)安裝就存在的倉庫3rd Party中,右擊倉庫名,選擇Upload Artifact。
6. 在eclipse中使用索引功能
原來試過,使用artifactory私服也可以使用Artifact的索引功能,但是由于不知道怎么設(shè)置遠(yuǎn)程倉庫的index,我只會設(shè)置
當(dāng)前artifactory私服已有的Artifact的索引,對于還沒有導(dǎo)入到artifactory私服的Artifact就沒有辦法索引了,很不方便,
畢竟剛開始使用maven時,所有用到的Artifact都是本地和私服沒有而需要到遠(yuǎn)程倉庫取的。
nexus中可以很方便的得到遠(yuǎn)程倉庫的Artifact的索引,在上面“3. 配置nexus”就介紹過。下面介紹如何在eclispe里面
設(shè)置和使用索引功能:
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下可以拉出極大數(shù)量的Artifact信息。
3) 測試一下使用
找個pom.xml文件,右鍵 -> Add Dependency, 然后填入一個關(guān)鍵詞,比如我填入mina,馬上填出和mina相關(guān)的一些
選擇,我找到apache mina,雙擊最新一個版本。會自動在pom.xml文件中增加以下內(nèi)容:
<dependency>
<groupId>org.apache.directory.mina</groupId>
<artifactId>mina-core</artifactId>
<version>0.9.5</version>
</dependency>
然后Maven自動下載jar包,再將jar包加入項(xiàng)目的build path,全程自動化處理,真是爽啊。
7. 為nexus增加新的proxy repository
方法很簡單,administration -> Repositories -> add -> proxy,填寫后保存即可。但是要注意,nexus不會自動將新加入的repository添加到group中,而我們一般喜歡直接使用默認(rèn)的"public repository" group, 比如前面我在maven的profile中就只設(shè)置了這一個URL:
http://localhost:8081/nexus/content/groups/public。因此需要手工修改"public repository" group的設(shè)置,將剛才添加的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/
加入這個純粹是因?yàn)樗衧un的jmxri/jmxtools這些Artifact,強(qiáng)烈鄙視sun,自己的官方repository居然沒有。
4)sonatype http://repository.sonatype.org/content/groups/public/
8. 總結(jié)
很明顯,nexus無論是在界面,功能,操作上,都比artifactory強(qiáng)大的多。
因此推薦大家使用nexus替代artifactory作為maven私服。
updates:
1. 2008.11.28
由于公司升級操作系統(tǒng),告別老舊不堪的windows2000升級到vista,因此重新安裝了nexus. 新版本的nexus似乎增加了不少小的功能比如對remote index的支持,具體沒有深究,不過能不斷更新實(shí)在是很令人欣慰。以后就打算用nexus了。
2. 2009.10.15
一年來陸續(xù)發(fā)現(xiàn)了一些比較不錯的maven倉庫
1) fedora.is
http://fedora.is/maven/
2) ibiblio
http://mirrors.ibiblio.org/pub/mirrors/maven2/
這個是ivy的官網(wǎng),東西比較多
3) rothamsted
http://ondex.rothamsted.bbsrc.ac.uk/nexus/content/repositories/releases/
http://ondex.rothamsted.bbsrc.ac.uk/nexus/content/repositories/wso2/
有一些比較偏門的東西