Maven连接Nexus私服配置

settings.xml是maven的全局配置文件,而pom.xml文件是所在项目的局部配置。settings.xml中包含类似本地仓储位置、修改远程仓储服务器、认证信息等配置。

settings.xml文件位置

settings.xml文件一般存在于两个位置:

  • 全局配置: ${M2_HOME}/conf/settings.xml
  • 用户配置: {user.home}和所有其他系统属性只能在3.0+版本上使用

配置优先级

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

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

  <mirrors>
    <!-- 我们使用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>

项目使用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中添加属性,并激活

  </profiles>
    <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仓库中;而项目版本是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私服中具有发布权限的用户名和密码

上一篇 Maven私服Nexus3.x环境构建操作记录
下一篇 CentOS下安装Confluence 7.1.1 bin包
目录
文章列表
1 Android DataBinding 采坑记录
Android DataBinding 采坑记录
2
RxJava2学习之一:观察者模式
RxJava2学习之一:观察者模式
3
Fidder简介
Fidder简介
4
React Native学习之RN与iOS原生通信
React Native学习之RN与iOS原生通信
5
Python搭建HTTP服务器:Windows下使用virtualenv安装Flask
Python搭建HTTP服务器:Windows下使用virtualenv安装Flask
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。