zoukankan      html  css  js  c++  java
  • 使用 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 的细节可以在里面查找。

  • 相关阅读:
    iBatis,第二次亲密接触
    微斯人,吾谁与归
    一个月了
    生命在于运动
    眼皮跳了好几天
    往返
    中病毒,学习批处理

    爱如潮水
    今天夏至
  • 原文地址:https://www.cnblogs.com/TonyYPZhang/p/5156149.html
Copyright © 2011-2022 走看看