使用 Maven 部署 artifact 到 Nexus 教程


本文側重講解如何將已經按照好的 Maven 和 Nexus 連接,即如何通過 Maven 部署 artifact 到 Nexus。

本文前提:

1. 安裝好 Maven。可以使用 Maven 創建、打包項目。關於安裝

2. 安裝好 Nexus。可以訪問本地的 Nexus 倉庫 http://localhost:8081/nexus/#welcome 。

 

正文:

1. 配置 Maven 的配置文件 settings.xml 和 項目的說明文件 pom.xml ,使得 Maven 知道往哪里部署 ,已經有權限部署 。

在 setting.xml 中 : 

     <servers>
         <server>
           <id>xxx-nexus</id>
           <username>deployment</username>
           <password>{deployment_pwd}</password>
        </server>
     </servers>

建議使用 deployment 賬號,這個賬號在安裝好 Nexus 后就有了,默認密碼是 deployment123 , 可以修改此密碼。


在 pom.xml 中 : 

  <distributionManagement>  
   <!-- Publish the versioned releases here -->  
   <repository>  
    <id>xxx-nexus</id>  
    <name>vineetmanohar nexus</name>  
    <url>http://localhost:8081/nexus/content/repositories/releases/</url>  
   </repository>  
    
   <!-- Publish the versioned releases here -->  
   <snapshotRepository>  
    <id>xxx-nexus</id>  
    <name>vineetmanohar nexus</name>  
    <url>http://localhost:8081/nexus/content/repositories/snapshots/</url>  
   </snapshotRepository>  
  </distributionManagement>  

注意一,這里 <repository> 元素 和 <snapshotRepository> 元素的 id 應該和 settings.xml 中的 id 保持一致,使得庫位置 和 庫賬號密碼能對的上。

注意二, <repository> 元素的 url 和 <snapshotRepository> 元素的 url 應該應該是不一樣的。我第一次配置的時候把上面的 url 值直接 copy 到下面的 url ,導致報錯 : 

Could not transfer artifact com.tony.aaa:webTT:war:1.0-20160124.105254-1 from/to vineetmanohar-nexus (http://localhost:8081/nexus/content/repositories/releases/) ...... Return code is: 400, ReasonPhrase: Bad Request. 

 

2. 添加部署插件

使用 nexus-staging-maven-plugin 插件,使得部署更順利。

在 pom.xml 中:

      <plugins>        
          <plugin>
             <groupId>org.sonatype.plugins</groupId>
             <artifactId>nexus-staging-maven-plugin</artifactId>
             <version>1.5.1</version>
             <executions>
                <execution>
                   <id>default-deploy</id>
                   <phase>deploy</phase>
                   <goals>
                      <goal>deploy</goal>
                   </goals>
                </execution>
             </executions>
             <configuration>
                <serverId>nexus</serverId>
                <nexusUrl>http://localhost:8081/nexus/</nexusUrl>
                <skipStaging>true</skipStaging>
             </configuration>
          </plugin>
      </plugins>

3. 執行部署命令

mvn clean deploy -Dmaven.test.skip=true

 

執行完后,應該看到類似的 : 

....
Uploading: http://localhost:8081/nexus/content/repositories/snapshots/com/tony/aaa/webTT/maven-metadata.xml
Uploaded: http://localhost:8081/nexus/content/repositories/snapshots/com/tony/aaa/webTT/maven-metadata.xml (275 B at 13.4 KB/sec)
[INFO]  * Bulk deploy of locally gathered snapshot artifacts finished.
[INFO] Remote deploy finished with success.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.689 s
[INFO] Finished at: 2016-01-24T21:12:28+08:00
[INFO] Final Memory: 16M/240M
[INFO] ------------------------------------------------------------------------

表示部署成功。然后可以到 Nexus 看到類似如下的結果 :

 

至此,部署完成。

 

參考資料 :

Maven Deploy to Nexus, Baelduang ,Maven 部署教程

"Bad Request" Error when deploying an artifact in Nexus

 

Getting started with Nexus Maven Repo Manager, Vineet Manohar's blog ,很好的教程,其中包含如何配置 Nexus 安全

Repository Management with Nexus 看了前三章,了解了關於 Nexus 的基本概念

Maven Tutorial - TutorialsPoint 很好的教程,看完能大致了解和使用 Maven.

Welcome to Apache Maven, Apache Maven 官網,關於 Maven 的細節可以在里面查找。

 


免責聲明!

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



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