maven私服nexus的配置說明

一、添加自己的倉庫,從而不適用默認的中央倉庫

在實際的開發中我們很好用默認的中央倉庫,由於網絡限制等種種原因,導致默認的重要倉庫用起來很慢,所以下面在maven中的配置用於指定項目的使用的倉庫。

     <profiles>  
		<profile>  
			<id>nexusprofile</id>  
			<repositories>  
				<repository>  
					<id>nexus</id>
					<name>Team Nexus Repository</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>  					
					<releases>  
						<enabled>true</enabled>  
					</releases>  
					<snapshots>  
						<enabled>true</enabled>  
					</snapshots>  
				</repository>  
			</repositories>  
			<pluginRepositories>
				<pluginRepository>
					<id>nexus</id>
					<name>Team Nexus Repository</name>
					<url>http://localhost:8081/nexus/content/groups/public/</url>
					<releases>  
						<enabled>true</enabled>  
					</releases>  
					<snapshots>  
						<enabled>true</enabled>  
					</snapshots>
				</pluginRepository>
			</pluginRepositories>
		</profile>  
	</profiles>  
	<activeProfiles>  
		<activeProfile>nexusprofile</activeProfile>  
	</activeProfiles>  

repository:在repositories元素下,可以使用repository子元素聲明一個或者多個遠程倉庫。

id:倉庫聲明的唯一id,尤其需要注意的是,Maven自帶的中央倉庫使用的id爲central,如果其他倉庫聲明也使用該id,就會覆蓋中央倉庫的配置。

name:倉庫的名稱,讓我們直觀方便的知道倉庫是哪個,暫時沒發現其他太大的含義。

url:指向了倉庫的地址,一般來說,該地址都基於http協議,Maven用戶都可以在瀏覽器中打開倉庫地址瀏覽構件。

releases和snapshots:用來控制Maven對於發佈版構件和快照版構件的下載權限。需要注意的是enabled子元素,該例中releases的enabled值爲true,表示開啓JBoss倉庫的發佈版本下載支持,而snapshots的enabled值爲false,表示關閉JBoss倉庫的快照版本的下載支持。根據該配置,Maven只會從JBoss倉庫下載發佈版的構件,而不會下載快照版的構件。

layout:元素值default表示倉庫的佈局是Maven2及Maven3的默認佈局,而不是Maven1的佈局。基本不會用到Maven1的佈局。

其他:對於releases和snapshots來說,除了enabled,它們還包含另外兩個子元素updatePolicy和checksumPolicy。

元素updatePolicy用來配置Maven從遠處倉庫檢查更新的頻率,默認值是daily,表示Maven每天檢查一次。其他可用的值包括:never-從不檢查更新;always-每次構建都檢查更新;interval:X-每隔X分鐘檢查一次更新(X爲任意整數)。

元素checksumPolicy用來配置Maven檢查校驗和文件的策略。當構建被部署到Maven倉庫中時,會同時部署對應的檢驗和文件。在下載構件的時候,Maven會驗證校驗和文件,如果校驗和驗證失敗,當checksumPolicy的值爲默認的warn時,Maven會在執行構建時輸出警告信息,其他可用的值包括:fail-Maven遇到校驗和錯誤就讓構建失敗;ignore-使Maven完全忽略校驗和錯誤。

也可以不改maven的setting文件,可以把 下面部分的內容可以在各個項目中的pom文件中配置,但是不建議中這種辦法,否則你每建立一個項目就得配置一遍。

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

二、將自己的組件發佈到私服上供其他項目組人員來使用

想將自己的組件上傳的話,需要添加一下的賬戶密碼信息供驗證
<servers>  
	  <server>  
		<id>nexus-releases</id>  
		<username>admin</username>  
		<password>admin123</password>  
	  </server>  
	  <server>  
		<id>nexus-snapshots</id>  
		<username>admin</username>  
		<password>admin123</password>  
	  </server>    
	</servers>  
並且在pom文件中配置如下

<distributionManagement>
		<repository>
			<id>nexus-releases</id>
			<name>Team Nexus Repository</name>
			<url>http://localhost:8081/nexus/content/repositories/releases</url>
		</repository>
		<snapshotRepository>
			<id>nexus-snapshots</id>
			<name>Team Nexus Repository</name>
			<url>http://localhost:8081/nexus/content/repositories/snapshots</url>
		</snapshotRepository>
	</distributionManagement>
然後用eclipse的mvn deploy命令或者是在控制檯上用此命令進行發佈

三、鏡像倉庫

是用來替換的,就是可以全權代理某個倉庫,例如:
<mirrors>
     <mirror>
      <id>maven.oschina.net</id>
      <name>maven mirror in China</name>
      <url>http://maven.oschina.net/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>
    </mirror>
</mirrors>
這就表示,以後只要用到central中央倉庫的地方,全部都會去http://maven.oschina.net/content/groups/public/這裏面去下載,以後http://maven.oschina.net/content/groups/public/ 就是中央倉庫了。同時還有一種配置方式是:
<mirrors>    
     <mirror>
        <id>nexus-aliyun</id>
        <mirrorOf>*</mirrorOf>
        <name>Nexus aliyun</name>
        <url>http://maven.aliyun.com/nexus/content/groups/public</url>
    </mirror>
  </mirrors>
其中的*表示這個倉庫是所有倉庫的鏡像,以後無論下載上面東西都會去從這個倉庫中下載東西,如果這個倉庫中沒有的話就沒辦法了。

四、mirrors鏡像倉庫與上面的repository倉庫的方式的區別

repository倉庫的方式,是指我這裏面默認使用我裏面定義的倉庫,但是如果這個倉庫沒有的話,他會去maven默認的central中央倉庫中找。但是如果用mirrors是把倉庫直接覆蓋掉,如果這個倉庫中沒有你要找的包,那麼它將沒有辦法了。一般mirrors替換是替換爲國內的中央倉庫,或者是把倉庫全部換成nexus私服,因爲nexus私服已經有個各個倉庫的配置,它會按照nexus倉庫的配置去找包。







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