Maven系列二:利用Nexus搭建私服

一、簡介

Nexus是一個應用廣泛的Maven倉庫管理軟件。

二、安裝

Nexus是典型的Java Web應用。它有兩種部署方式:一種是部署bundle包,內置jetty服務器;一種是部署war包到java web 服務器(比如tomcat等),通過tomcat對外提供服務。

1、bundle方式安裝

下載bundle包:http://download.sonatype.com/nexus/oss/nexus-2.6.0-05-bundle.zip,然後解壓到某一目錄(如/usr/local/)下面,解壓之後包括兩個文件夾:

(1)nexus-2.6.0-05:該目錄包含了運行需要的文件:lib包、jetty服務器的配置、服務器啓動\停止腳本等等。

(2)sonatype-work:該目錄包含了服務生成的配置文件、倉庫文件和日誌文件等。

目錄 /usr/local/nexus-2.6.0-05/bin下面包含了控制服務器的腳本,用法如下:

./nexus console:啓動服務,所有的輸出打印到控制檯,可以通過Ctrl-C停止服務。

./nexus start:在後臺以daemon方式啓動服務

./nexus stop:停止後臺啓動的服務

./nexus status:顯示後臺服務的狀態

./nexus restart:重啓後臺服務

Nexus服務默認監聽8081端口,可以通過/usr/local/nexus-2.6.0-05/conf/nexus.properties中配置項目application-port進行修改。

2、war包方式安裝

以war包方式其實就是一個普通的jave web部署。

三、倉庫

倉庫是nexus中最重要的概念之一。nexus中的倉庫包括四種類型:宿主倉庫(hosted)、代理倉庫(proxy)、虛擬倉庫(virtual)和倉庫組(group)。倉庫具有一定個格式(format):maven2/maven1。倉庫具有一定的策略(policy):release和snapshot。

Nexus服務器初始時下列倉庫:

(1)Releases倉庫:這是一個宿主(hosted)倉庫。該倉庫一般用來保存我們自己的項目的發佈版。

(2)Snapshots倉庫:這是一個宿主(hosted)倉庫。該倉庫一般用來保存我們自己的項目的快照版本。

(3)3rd Party倉庫:這是一個宿主(hosted)倉庫。該倉庫一般用來保存第三方的包。

(4)Apache Snapshots:這是一個代理(proxy)倉庫。該倉庫保存從apache快照庫下載的包(http://repository.apache.org/snapshots/)。

(5)Codehaus Snapshots:這是一個代理(proxy)倉庫。該倉庫保存從codehaus快照庫下載的包(http://nexus.codehaus.org/snapshots/)。

(6)Central:這是一個代理(proxy)倉庫。該倉庫保存從中央倉庫下載的包(http://repo1.maven.org/maven2/)。

(7)Central M1 shadow:這是一個虛擬(virtual)倉庫。該倉庫以M1格式代理中央倉庫。

(8)Public Repositories:這是一個組(group)倉庫。該倉庫可以代理以上倉庫,通過統一的接口供外下載。

四、配置Maven項目從本地私服下載構件

1、項目文件配置方式

在project下面增加repositories和pluginRepositories

	<repositories>
		<repository>
			<id>nexus</id>
			<name>Nexus</name>
			<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
			<releases><enabled>true</enabled></releases>
			<snapshots><enabled>true</enabled></snapshots>
		</repository>
	</repositories>
	<pluginRepositories>
		<pluginRepository>
			<id>nexus</id>
			<name>Nexus</name>
			<url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
			<releases><enabled>true</enabled></releases>
			<snapshots><enabled>true</enabled></snapshots>
		</pluginRepository>
	</pluginRepositories>

2、setting.xml配置方式

在profiles下面增加一個profile

    <profile>
    	<id>nexus</id>
    	<repositories>
		    <repository>
			    <id>nexus</id>
		    	<name>Nexus</name>
			    <url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
			    <releases><enabled>true</enabled></releases>
			    <snapshots><enabled>true</enabled></snapshots>
		    </repository>
	    </repositories>
	    <pluginRepositories>
		    <pluginRepository>
			    <id>nexus</id>
			    <name>Nexus</name>
			    <url>http://10.241.227.69:8081/nexus/content/groups/public/</url>
			    <releases><enabled>true</enabled></releases>
			    <snapshots><enabled>true</enabled></snapshots>
		    </pluginRepository>
	     </pluginRepositories>
    </profile> 	
在activeProfiles下面增加一個activeProfile

  <activeProfiles>
    <activeProfile>nexus</activeProfile>
  </activeProfiles>  

五、部署構件至本地私服

在project下面配置distributionManagement

	<distributionManagement>
		<repository>
		  <id>nexus-releases</id>
		  <name>Nexus Releases Repository</name>
		  <url>http://10.241.227.69:8081/nexus/content/repositories/releases/</url>
		</repository>
		<snapshotRepository>
		  <id>nexus-releases</id>
		  <name>Nexus Releases Repository</name>
		  <url>http://10.241.227.69:8081/nexus/content/repositories/snapshots/</url>
		</snapshotRepository>
	</distributionManagement>
這裏配置了兩個repository用來存儲本地編譯的構件。

匿名用戶只有讀權限沒有部署權限。部署之前在setting.xml中配置認證信息。在servers下面增加配置admin/admin123是nexus服務的默認賬號密碼。

    <server>
      <id>nexus-releases</id>
      <username>admin</username>
      <password>admin123</password>
    </server>
    <server>
      <id>nexus-snapshots</id>
      <username>admin</username>
      <password>admin123</password>
    </server>




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