Maven配置私有库

一、仓库

仓库类型:
本地仓库、远程中央仓库、公司自己搭建的私有仓库

寻找jar的基本优先级顺序:
本地仓库 > settings.xml的profile的仓库 > pom.xml的profile的仓库 >pom.xml的仓库 > 中央仓库

设置仓库的方式有两种,一种是在项目最顶级POM.xml中设置,另一种是在settings.xml中设置。

在POM.xml中设置:

<repositories>
<repository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://192.168.100.100:8181/nexus/content/groups/public</url>
</repository>
<repository>
<id>thirdparty</id>
<name>Nexus thirdparty</name>
<url>http://192.168.100.100:8181/nexus/content/repositories/thirdparty/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<name>Team Nexus Repository</name>
<url>http://192.168.100.100:8181/nexus/content/groups/public</url>
</pluginRepository>
</pluginRepositories>

settings.xml中设定:

<?xml version="1.0" encoding="UTF-8"?>
<settings
xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/Users/本地仓库地址/Documents/repo</localRepository>
<pluginGroups></pluginGroups>
<proxies></proxies>
<servers>
<!--
发布到仓库中的配置,id要和distributionrepository保持一致
服务器要打包上传到私服时,设置私服的鉴权信息,否和报错 Return code is: 401, ReasonPhrase: Unauthorized
-->
<server>
<id>release</id>
<username>deployment</username>
<password>123456</password>
</server>
<server>
<id>snapshot</id>
<username>deployment</username>
<password>123456</password>
</server>
</servers>
<mirrors>
<!-- 设置多个mirrors镜像,镜像只会执行第一个位置mirror。-->
<!-- 配置的多个mirror可以都放着不影响,选取一个镜像下载比较快的放在第一个就行。比如你设置使用自己公司的私有仓库-->
<!--只有当前一个mirror无法连接的时候,才会去找后一个,类似于备份和容灾。所以当第一个mirror中不存在a.jar的时候,并不会去第二个mirror中查找,甚至于,maven根本不会去其他的mirror地址查询-->
<mirror>
<!-- 当有id为B,A,C的顺序的mirror在mirrors节点中,maven会根据字母排序来指定第一个,所以不管怎么排列,一定会找到A这个mirror来进行查找,当A无法连接,出现意外的情况下,才会去B查询-->
<id>aliyun</id>
<name>阿里云仓库地址</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
<!--覆盖了Maven自带的central-->
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<!-- 全局JDK1.8配置 -->
<profile>
<id>jdk1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
<!-- 阿里云配置: 提高国内的jar包下载速度 -->
<profile>
<id>aliyun-Repository</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>suwell-Repository</id>
<repositories>
<repository>
<id>first</id>
<name>Repository first</name>
<url>http://192.168.100.100:8181/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>gomain-Repository</id>
<repositories>
<repository>
<id>second</id>
<name>Repository second</name>
<url>http://192.168.100.100:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
</profiles>
激活仓库配置,拉取依赖会在这些仓库中逐个去找
<activeProfiles>
<activeProfile>jdk1.8</activeProfile>
<activeProfile>first-Repository</activeProfile>
<activeProfile>aliyun-Repository</activeProfile>
<activeProfile>second-Repository</activeProfile>
</activeProfiles>
</settings>

二、pom.xml

pom.xml 配置文件主要分为两类

  • 用于配置自己的实际依赖
  • 用于声明一些版本和仓库便于版本管理和发布。

pom配置是可以被继承的,父级依赖一般是做版本控制以及指定私有仓库的。

 

 

三、settings.xml文件

settings 主要由mirrors、servers 和profiles 三部分组成。

1、mirrors
mirrors 主要作用是一个镜像代理,便于内外网厂库切换,或者单独配置内网使用。

如果pom中的repository的id能和mirrorOf的值关联上,那么url以mirror的为准,否则以repository中自己的url为准。

<mirror>
<id>test-nexus</id>
<mirrorOf>*</mirrorOf>
<name>sugon local repository</name>
<url>http://172.22.5.34:9996/repository/sugoncloud-public/</url>
</mirror>

mirrorof 有三种值:

  • *代表 所有仓库请求都走这个配置的镜像代理
  • central 默认是maven 的仓库

2、servers

<server>
<id>nexus-releases</id>
<username>deployment</username>
<password>deployment123</password>
</server>

它关联pom中配置的私有仓库id, 在推送依赖包的时候根据id进行认证。

3、profiles
配置全局私用仓库。

注意:
如果只配置mirrors 是不能拉取父pom文件的,如果需要拉取父pom文件那么需要配置repository。

参考文章:
[1] Maven私有仓库nexus配置
[2] Java Maven settings.xml中私有仓库配置详解

posted @ 2023-08-11 13:12  逐梦北京  阅读(1404)  评论(0编辑  收藏  举报