訪問(wèn)http://nexus.sonatype.org/downloads/下載Nexus。
啟動(dòng)Nexus,就是啟動(dòng)一個(gè)web服務(wù)器,它的默認(rèn)地址是localhost:8081。Nexus在一個(gè)名為Jetty的servlet容器中運(yùn)行,它使用一個(gè)名為Tanuki Java Service Wrapper的本地服務(wù)包裹器啟動(dòng)。這個(gè)服務(wù)包裹器可以被配置成以Windows服務(wù)或Unix守護(hù)線程的形式運(yùn)行Nexus。要啟動(dòng)Nexus,你需要為你的平臺(tái)找到合適的啟動(dòng)腳本。要查看可用平臺(tái)的列表,查看${NEXUS_HOME}/bin/jsw目錄的內(nèi)容。可執(zhí)行文件在%nexus安裝目錄%\nexus-webapp-1.0.0\binjsw\windows-x86-32下:InstallNexus.bat/UninstallNexus.bat是安裝/卸載nexus為windows service。Nexus.bat是直接在命令行中啟動(dòng)Nexus,如果不想安裝Nexus為windows service,可以用這個(gè)文件來(lái)手工控制Nexus的啟動(dòng)退出。
1.配置nexus
首先登錄,默認(rèn)地址http://localhost:8081/nexus/,默認(rèn)用戶名密碼為admin/admin123.
nexus默認(rèn)是關(guān)閉遠(yuǎn)程索引下載功能的。開(kāi)啟的方式:
點(diǎn)擊Administration菜單下面的Repositories,將這三個(gè)倉(cāng)庫(kù)Apache Snapshots,Codehaus Snapshots,Maven Central的
Download Remote Indexes修改為true。然后在這三個(gè)倉(cāng)庫(kù)上分別右鍵,選擇Re-index,這樣Nexus就會(huì)去下載遠(yuǎn)程的索引文件。
2.管理倉(cāng)庫(kù)
以管理員用戶登陸然后點(diǎn)擊左邊導(dǎo)航菜單Administration下面的Repositories。Nexus提供了三種不同的倉(cāng)庫(kù)。
(1)代理倉(cāng)庫(kù)
一個(gè)代理倉(cāng)庫(kù)是對(duì)遠(yuǎn)程倉(cāng)庫(kù)的一個(gè)代理。默認(rèn)情況下,Nexus自帶了如下配置的代理倉(cāng)庫(kù):
Apache Snapshots
這個(gè)倉(cāng)庫(kù)包含了來(lái)自于Apache軟件基金會(huì)的快照版本。http://people.apache.org/repo/m2-snapshot-repository
Codehaus Snapshots
這個(gè)倉(cāng)庫(kù)包含了來(lái)自于Codehaus的快照版本。 http://snapshots.repository.codehaus.org/
Central Maven Repository
這是中央Maven倉(cāng)庫(kù)(發(fā)布版本)。 http://repo1.maven.org/maven2/
(2)宿主倉(cāng)庫(kù)
一個(gè)宿主倉(cāng)庫(kù)是由Nexus托管的倉(cāng)庫(kù)。Maven自帶了如下配置的宿主倉(cāng)庫(kù)。
3rd Party
這個(gè)宿主倉(cāng)庫(kù)應(yīng)該用來(lái)存儲(chǔ)在公共Maven倉(cāng)庫(kù)中找不到的第三方依賴。這種依賴的樣例有:你組織使用的,商業(yè)的,私有的類庫(kù)如Oracle JDBC驅(qū)動(dòng)。
Releases
這個(gè)宿主倉(cāng)庫(kù)是你組織公布內(nèi)部發(fā)布版本的地方。
Snapshots
這個(gè)宿主倉(cāng)庫(kù)是你組織發(fā)布內(nèi)部快照版本的地方。
(3)虛擬倉(cāng)庫(kù)
一個(gè)虛擬倉(cāng)庫(kù)作為Maven 1的適配器存在。Nexus自帶了一個(gè)central-m1虛擬倉(cāng)庫(kù)
3. 管理組
組是Nexus一個(gè)強(qiáng)大的特性,它允許你在一個(gè)單獨(dú)的URL中組合多個(gè)倉(cāng)庫(kù)。Nexus自帶了兩個(gè)組:public和public-snapshots。public組中組合了三個(gè)宿主倉(cāng)庫(kù):3rd Party, Releases, 和Snapshots,還有中央Maven倉(cāng)庫(kù)。而public-snapshots組中組合了Apache Snapshots和Codehaus Snapshots倉(cāng)庫(kù)。
4. 配置maven
要讓maven使用Nexus作為倉(cāng)庫(kù),要修改~/.m2/settings.xml.
Xml代碼
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>local private nexus</name>
<url>http://localhost:8081/nexus/content/groups/public</url>
</repository>
</repositories>
</profile>
<profile>
<id>nexus-snapshots</id>
<repositories>
<repository>
<id>nexus-snapshots</id>
<name>local private nexus snapshots</name>
<url>http://localhost:8081/nexus/content/groups/public-snapshots</url>
</repository>
</repositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
<activeProfile>nexus-snapshots</activeProfile>
</activeProfiles>
5.部署構(gòu)件至Nexus
要部署構(gòu)件至Nexus,在distributionManagement中提供倉(cāng)庫(kù)URL,然后運(yùn)行mvn deploy。Maven會(huì)通過(guò)一個(gè)簡(jiǎn)單的HTTP PUT將項(xiàng)目POM和構(gòu)件推入至你的Nexus安裝。需要配置你項(xiàng)目POM中distributionManagement部分的repository。
Xml代碼
<distributionManagement>
<repository>
<id>releases</id>
<name>Internal Releases</name>
<url>http://localhost:8081/nexus/content/repositories/releases</url>
</repository>
<snapshotRepository>
<id>Snapshots</id>
<name>Internal Snapshots</name>
<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
</snapshotRepository>
</distributionManagement>
這樣還沒(méi)完,這時(shí)如果部署會(huì)報(bào)錯(cuò),還要在~/.m2/settings.xml中添加如下的服務(wù)器登錄信息:
Xml代碼
<servers>
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>Snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
部署第三方構(gòu)件:
構(gòu)件可能是私有數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng)如Oracle,或者你依賴于另一個(gè)JAR,它既不開(kāi)源也無(wú)法免費(fèi)獲得。在這樣的情況下,你就需要手動(dòng)拿來(lái)這些構(gòu)件然后發(fā)布到你自己的倉(cāng)庫(kù)中。Nexus提供宿主的"third-party"倉(cāng)庫(kù),就是為了這個(gè)目的。
使用以下命令發(fā)布該文件至Nexus:
Java代碼
mvn deploy:deploy-file -DgroupId=com.oracle -DartifactId=ojdbc14 -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc.jar -Durl=http://localhost:8081/nexus/content/repositories/thirdparty -DrepositoryId=thirdparty
6.Nexus監(jiān)聽(tīng)端口
默認(rèn)情況下,Nexus監(jiān)聽(tīng)端口8081。你可以更改這個(gè)端口,通過(guò)更改${NEXUS_HOME}/conf/plexus.properties的值,為此,停止Nexus,更改文件中applicationPort的值,然后重啟Nexus。
7.Maven Profiles
Maven中的profile是一組可選的配置,可以用來(lái)設(shè)置或者覆蓋配置默認(rèn)值。有了profile,你就可以為不同的環(huán)境定制構(gòu)建。profile可以在pom.xml中配置,并給定一個(gè)id。然后你就可以在運(yùn)行Maven的時(shí)候使用的命令行標(biāo)記告訴Maven運(yùn)行特定profile中的目標(biāo)。以下pom.xml使用production profile覆蓋了默認(rèn)的Compiler插件設(shè)置。
Xml代碼
<profiles>
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>false</debug>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
要使用production profile來(lái)運(yùn)行mvn install,你需要在命令行傳入-Pproduction參數(shù)。要驗(yàn)證production profile覆蓋了默認(rèn)的Compiler插件配置,可以像這樣以開(kāi)啟調(diào)試輸出(-X) 的方式運(yùn)行Maven。
如果你開(kāi)始大量使用Maven profile,你會(huì)希望將profile從POM中分離,使用一個(gè)單獨(dú)的文件如profiles.xml。你可以混合使用定義在pom.xml中和外部profiles.xml文件中的profile。只需要將profiles元素放到${basedir}目錄下的profiles.xml文件中,然后照常運(yùn)行Maven就可以。profiles.xml文件的大概內(nèi)容如下:
Xml代碼
<profiles>
<profile>
<id>development</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>true</debug>
<optimize>false</optimize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>production</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<debug>false</debug>
<optimize>true</optimize>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
settings profile可以應(yīng)用到所有你使用Maven構(gòu)建的項(xiàng)目。你可以在兩個(gè)地方定義settings profile:定義在~/.m2/settings.xml中的用戶特定settings profile,或者定義在${M2_HOME}/conf/settings.xml中的全局settings profile。
本文來(lái)自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/summertse/archive/2010/11/23/6030039.aspx