在Maven项目中,大部分jar包可从阿里云仓库(Maven默认它的中央仓库,但是国内慢一般选择阿里云)获取,但是公司里有许多自用的jar包,放在自己公司搭建私服上,所以这时候需要主仓库是阿里云,阿里云找不到的再到我设置的备用仓库里找。这就需要使用多个仓库配合完成对jar的依赖。
其实公司配置的私有仓库,都是可以链接阿里云的,所以其实你的项目只需要配置公司的私有仓库就可以了,如果你需要的jar在公司私有仓库没有,它会自己去阿里云仓库下载到私有仓库,然后你在下载到本地仓库,但是这就有个弊端,如果私有仓库链接阿里云仓库出了问题,那就会影响了,所以多数人都会在自己本地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>
网友评论