maven deploy distributionManagement


分發構件至遠程倉庫
mvn install 會將項目生成的構件安裝到本地Maven倉庫, mvn deploy 用來將項目生成的構件分發到遠程Maven倉庫。本地Maven倉庫的構件只能供當前用戶使用,在分發到遠程Maven倉庫之后,所有能訪問該倉庫的用戶都能使用你的構件。
我們需要配置POM的distributionManagement來指定Maven分發構件的位置,如下:
Xml代碼 
  1. <project>    
  2.   ...    
  3.   <distributionManagement>    
  4.     <repository>    
  5.       <id>nexus-releases</id>    
  6.       <name>Nexus Release Repository</name>    
  7.       <url>http://127.0.0.1:8080/nexus/content/repositories/releases/</url>    
  8.     </repository>    
  9.     <snapshotRepository>    
  10.       <id>nexus-snapshots</id>    
  11.       <name>Nexus Snapshot Repository</name>    
  12.       <url>http://127.0.0.1:8080/nexus/content/repositories/snapshots/</url>    
  13.     </snapshotRepository>    
  14.   </distributionManagement>    
  15.   ...    
  16. </project>    
Maven區別對待release版本的構件和snapshot版本的構件,snapshot為開發過程中的版本,實時,但不穩定,release版本則比較穩定。Maven會根據你項目的版本來判斷將構件分發到哪個倉庫。
一般來說,分發構件到遠程倉庫需要認證,如果你沒有配置任何認證信息,你往往會得到401錯誤。這個時候,如下在settings.xml中配置認證信息:
Xml代碼 
  1. <settings>    
  2.   ...    
  3.   <servers>    
  4.     <server>    
  5.       <id>nexus-releases</id>    
  6.       <username>admin</username>    
  7.       <password>admin123</password>    
  8.     </server>    
  9.     <server>    
  10.       <id>nexus-snapshots</id>    
  11.       <username>admin</username>    
  12.       <password>admin123</password>    
  13.     </server>      
  14.   </servers>    
  15.   ...    
  16. </settings>  
需要注意的是,settings.xml中server元素下id的值必須與POM中repository或snapshotRepository下id的值完全一致。將認證信息放到settings下而非POM中,是因為POM往往是它人可見的,而settings.xml是本地的。
 

小結

本文介紹了Maven倉庫,它是什么?本地倉庫,遠程倉庫,中央倉庫具體是指什么?並介紹了如何在POM中配置項目層次的倉庫,在settings中配置用戶層次的倉庫,以及mirror。本文還介紹了如何安裝構件到本地倉庫,如何分發構件至倉庫。

如果這里不配置,會報錯: 報 錯:Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.5:deploy (default-deploy) on project git-demo: Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM