maven连接nexus私服配置以及maven settings.xml文件详解

settings.xml有什么用?

如果在Eclipse中使用过Maven插件,想必会有这个经验:配置settings.xml文件的路径。

settings.xml文件是干什么的,为什么要配置它呢?
从settings.xml的文件名就可以看出,它是用来设置maven参数的配置文件。并且,settings.xml是maven的全局配置文件。而pom.xml文件是所在项目的局部配置。
Settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

settings.xml文件位置

settings.xml文件一般存在于两个位置:
全局配置: ${M2_HOME}/conf/settings.xml
用户配置: {user.home} 和和所有其他系统属性只能在3.0+版本上使用。请注意windows和Linux使用变量的区别。

配置优先级

需要注意的是:局部配置优先于全局配置。
配置优先级从高到低:pom.xml> user settings > global settings
如果这些文件同时存在,在应用配置时,会合并它们的内容,如果有重复的配置,优先级高的配置会覆盖优先级低的。

maven怎么从远程仓库下载jar包,setting中配置:

 <!--  我们使用maven下载需要的jar包,但是很多的时候由于中央仓库没有,所以此处可以在maven的设置中心添加多个下载仓库,当中央仓库没有的话,继续到下一个仓库去下载。这样丰富了中央仓库的下载地址。  -->
     <mirror>
      <id>nexus</id>
      <name>nexus maven</name>
      <url>http://localhost:8081/repository/maven-public/</url>
      <mirrorOf>*</mirrorOf>        
     </mirror> 

     <mirror>
      <id>alimaven</id>
      <name>aliyun maven</name>
      <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
      <mirrorOf>central</mirrorOf>        
     </mirror> 

     <mirror>  
      <id>repo2</id>  
      <mirrorOf>central</mirrorOf>  
      <name>Human Readable Name for this Mirror.</name>  
      <url>http://repo2.maven.org/maven2/</url>  
    </mirror>
  </mirrors>

Nexus配置
项目使用nexus私服的jar包,在项目的pom.xml文件中指定私服仓库

<repositories>
      <repository>
          <id>nexus</id>
          <name>nexus</name>
          <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
             <enabled>true</enabled>
         </snapshots>
     </repository>
 </repositories>

此时下载了jar在本地仓库   如果想删除此私服配置  重新从本地仓库引入  idea出现红色错误不能引入  可以进入本地仓库  删掉 _remote.repositories文件

项目使用nexus私服的插件,在项目的pom.xml文件中指定插件仓库

 <pluginRepositories>
      <pluginRepository>
          <id>nexus</id>
          <name>nexus</name>
          <url>http://192.168.1.103:8081/nexus/content/groups/public/</url>
          <releases>
              <enabled>true</enabled>
          </releases>
          <snapshots>
             <enabled>true</enabled>
         </snapshots>
     </pluginRepository>
 </pluginRepositories>

如果想本机所有的maven项目都使用私服的组件,可以在maven的设置文件settings.xml中添加属性,并激活

  <profile>
       <id>Nexus</id>
       <repositories>
         <repository>
          <id>nexus</id>
          <url>http://localhost:8081/repository/maven-public/</url>
          <releases><enabled>true</enabled></releases>
          <snapshots><enabled>true</enabled></snapshots>
         </repository>
       </repositories>
       <pluginRepositories>
         <pluginRepository>
           <id>nexus</id>
           <url>http://localhost:8081/repository/maven-public/</url>
           <releases>
            <enabled>true</enabled>
           </releases>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
         </pluginRepository>
        </pluginRepositories>
     </profile>
  </profiles>
  <!-- 激活 -->
  <activeProfiles>
    <activeProfile>Nexus</activeProfile>
  </activeProfiles>

如何将自己的项目发布到nexus私服

我们知道用mvn install命令可以将项目装载的本地的仓库,但是项目发布到私服,maven项目就要使用命令:mvn clean deploy;
要想发布项目到nexus里,必须通过标签来进行配置。在之前的文章中有介绍nexus的工厂类别,其中提到两个:hosted里的Releases、Snapshots.

当我们发布项目到nexus里时,如果项目版本是x.x.x-Releases,则会发布到Releases工厂中;而项目版本是x.x.x-SNAPSHOTS则发布到Snapshots工厂中。
需要在pom文件中配置一下代码;


<distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Release Repository</name>
            <url>http://localhost:8081/repository/maven-releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshot Repository</name>
            <url>http://localhost:8081/repository/maven-snapshots//</url>
        </snapshotRepository>
    </distributionManagement>

注意还需要配置mvn发布的权限,否则会报401错误,在settings.xml中配置权限,其中id要与pom文件中的id一致

  <!--授权信息  -->
      <server> 
        <id>nexus-releases</id> 
        <username>admin</username> 
        <password>admin123</password> 
      </server> 
      <server> 
        <id>nexus-snapshots</id> 
        <username>admin</username> 
        <password>admin123</password> 
      </server>

这里面的username和password对应的是nexus私服中具有发布权限的用户名和密码

参考文件:
http://blog.csdn.net/u012225679/article/details/73740785
https://www.cnblogs.com/jingmoxukong/p/6050172.html?utm_source=gold_browser_extension
http://blog.csdn.net/moshenglv/article/details/52027125
http://blog.csdn.net/haohaizijhz/article/details/72841489
https://www.cnblogs.com/h–d/p/5719040.html
http://blog.csdn.net/cwh056056/article/details/49667971

作者:摩尔__摩尔
来源:CSDN
原文:https://blog.csdn.net/u011217058/article/details/79418317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值