目录
  • 背景
  • 私有和公共仓库混合配置
    • Maven仓库
    • 解决步骤
      • 一、验证私有仓库
      • 二、搜索共有仓库
      • 三、搜索第三方仓库
      • 四、maven配置
    • mirror
      • profile
        • activeProfile
        • 配置结果
          • setting.xml完整配置
          • 结论

            背景

            近期在调研一个开源仓库,于是将 代码 从github下载后,当IDEA sync依赖时出现Cannot resolve org.fourthline.cling:cling-support:2.1.1 的问题,详情如下:

            关于Maven混合配置私有仓库和公共仓库的问题

            Cannot resolve org.fourthline.cling:cling-support:2.1.1 
            Clean up the broken artifacts data (.lastUpdated files) and reload the project.
            Cannot resolve org.fourthline.cling:cling-core:2.1.1 
            Clean up the broken artifacts data (.lastUpdated files) and reload the project.

            很明显这个问题是Maven仓库找不到需要的jar包。那么如何解决这个问题呢?

            私有和公共仓库混合配置

            Maven仓库

            首先,需要知道Maven仓库分为私有仓库、公有仓库、第三方仓库。其中:

            • 私有仓库:一般是公司或个人自己搭建的仓库。
            • 公共仓库

              首选是Maven官方的仓库,地址:repo1.maven.org/maven2/

              官方仓库在国内访问极慢,因此国内一般会使用阿里云仓库替代,地址:maven.aliyun.com/nexus/conte…

            • 第三方仓库:一些个人、团体、公司开放的仓库,仓库中的jar包并没有发布到公共仓库中。

            熟悉仓库的分类对于解决上文中遇到的问题有这个重要的作用。

            解决步骤

            一、验证私有仓库

            遇到依赖缺失的问题后,首先需要确认私有仓库是否存在依赖,查看nexus发现并没有需要的依赖,可以断定私有仓库没有此依赖。

            关于Maven混合配置私有仓库和公共仓库的问题

            二、搜索共有仓库

            Maven官方的仓库提供了Web搜索页面,地址:repo1.maven.org/maven2/,尝试搜索后发现也没有需要的依赖。如下:

            关于Maven混合配置私有仓库和公共仓库的问题

            三、搜索第三方仓库

            经过以上搜索后,可以断定 代码 需要的依赖,一定是由第三方仓库提供的。如何找到是在哪个第三方仓库呢?

            此时,就需要mvnrepository来提供帮助了,地址: mvnrepository.com/ 。mvnrepository提供了公共仓库和第三方仓库中jar包的索引、查询、下载等实用功能。

            我们尝试搜索需要的依赖, mvnrepository.com/artifact/or…,结果如下图:

            关于Maven混合配置私有仓库和公共仓库的问题

            可以看到在mvnrepository中找到了需要的依赖。那么问题来,如何知道第三方仓库的地址呢?可以详细看上图中箭头指向的区域,这里展示了第三方仓库的maven url。

            在实践中,完全可以跳过搜索公共仓库,因为mvnrepository已经包含了公共仓库的依赖。

            四、maven配置

            maven仓库的配置是在setting.xml配置的。如果要混合配置私有仓库和公共仓库,需要在setting.xml增加新的mirrorprofile,并激活新的activeProfile

            mirror

            setting.xmlmirrors节点,是用来配置镜像URL的。mirrors可以配置多个mirror,每个mirrorid,name,url,mirrorOf属性,详细解释如下:

            • id是唯一标识一个mirror
            • name貌似没多大用,相当于描述
            • url是官方的库地址
            • mirrorOf代表了一个镜像的替代位置,其中,
              • *: 匹配所有,所有内容都从镜像拉取;
              • external:*: 除了本地缓存的所有从镜像仓库拉取;
              • repo,repo1: repo或者repo1,这里的repo指的仓库ID;
              • *,!repo1: 除了repo1的所有仓库;

            我们这次需要添加的mirror如下:

            <mirror>
            	<id>nexus-4thline</id>
            	<mirrorOf>4thline</mirrorOf>
            	<name>4thline nexus</name>
            	<url>http://4thline.org/m2/</url>
            </mirror>

            这里又产生了一个问题,配置了多个mirror,Maven如何排序?答案是,Maven并不是按照setting.xml中配置的顺序执行,而是根据字母排序来指定第一个,然后会遍历所有的仓库,直至找到需要的依赖。

            profile

            作用:根据环境参数来调整构建配置的列表,settings.xml中的profile元素是pom.xmlprofile元素的裁剪版本

            • id:profile的唯一标识
            • activation:自动触发profile的条件逻辑
            • repositories:远程仓库列表
            • pluginRepositories:插件仓库列表
            • properties:扩展属性列表

            这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile

            我们这次需要添加的profile如下:

            <profile>
              <id>4thline</id>
              <repositories>
            	<repository>
            	  <id>4thline</id>
            	  <url>http://4thline.org/m2/</url>
            	  <releases>
            		<enabled>true</enabled>
            	  </releases> 
            	  <snapshots>
            		<enabled>true</enabled> 
            		<updatePolicy>always</updatePolicy>
            	  </snapshots>
            	</repository>
              </repositories>
            </profile>

            activeProfile

            作用:手动激活profiles的列表,按照profile被应用的顺序定义activeProfile

            该元素包含了一组activeProfile元素,每个activeProfile都含有一个profile id。任何在activeProfile中定义的profile id,不论环境设置如何,其对应的 profile都会被激活。如果没有匹配的profile,则什么都不会发生。

            我们这次需要添加的activeProfile如下:

            <activeProfiles>
            	<activeProfile>corp</activeProfile>
            	<activeProfile>aliyun</activeProfile>
            	<activeProfile>4thline</activeProfile>
            </activeProfiles>

            配置结果

            经过上述配置后,首先我们可以在IDEA的Maven侧边栏中,可以看到多了4thlineprofile。(可以看到IDEA的profiles是按照首字母排序的,和上文中mirror的定义是一致的。)

            关于Maven混合配置私有仓库和公共仓库的问题

            重新执行Reload All Maven Projects,所有的依赖都正常import。

            关于Maven混合配置私有仓库和公共仓库的问题

            setting.xml完整配置

            完整版的setting.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>D:\Users\xxx\.m2\repository\&;/localRepository>
            	<pluginGroups>
            	</pluginGroups>
            	<mirrors>
            		<mirror>
            		  <id>nexus</id>
            		  <mirrorOf>corp</mirrorOf>
            		  <name>corp nexus</name>
            		  <url>http://maven.corp.com/nexus/content/groups/public</url>
            		</mirror>
            		<mirror>
            			<id>nexus-aliyun</id>
            			<mirrorOf>*</mirrorOf>
            			<name>aliyun nexus</name>
            			<url>http://maven.aliyun.com/nexus/content/groups/public</url>
            		</mirror>
            		<mirror>
            			<id>nexus-4thline</id>
            			<mirrorOf>4thline</mirrorOf>
            			<name>4thline nexus</name>
            			<url>http://4thline.org/m2/</url>
            		</mirror>
            	</mirrors>
            	<servers>
            		<server>
            			<id>releases</id>
            			<username>xxx</username>
            			<password>yyy</password>
            		</server>
            		<server>
            			<id>snapshots</id>
            			<username>xxx</username>
            			<password>yyy</password>
            		</server>
            	</servers>
            	<profiles xmlns="">
            		<profile>
            			<id>corp</id>
            			<repositories>
            				<repository>
            				  <id>public</id>
            				  <name>all repoes</name>
            				  <url>http://maven.corp.com/nexus/content/groups/public</url>
            				  <releases>
            					<enabled>true</enabled>
            					<checksumPolicy>warn</checksumPolicy>
            				  </releases>
            				  <snapshots>
            					 <enabled>true</enabled>
            					 <updatePolicy>always</updatePolicy>
            					 <checksumPolicy>fail</checksumPolicy>
            				  </snapshots>
            				</repository>
            			</repositories>
            			<pluginRepositories>
            				<pluginRepository>
            					<id>corp</id>
            					<url>http://maven.corp.com/nexus/content/groups/public</url>
            					<releases>
            						<enabled>true</enabled>
            						<checksumPolicy>warn</checksumPolicy>
            					</releases>
            					<snapshots>
            						<enabled>true</enabled>
            						<updatePolicy>always</updatePolicy>
            						<checksumPolicy>fail</checksumPolicy>
            					</snapshots>
            				</pluginRepository>
            			</pluginRepositories>
            		</profile>
            		<profile>
            		  <id>aliyun</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> 
            				<updatePolicy>always</updatePolicy>
            			  </snapshots>
            			</repository>
            		  </repositories>
            		</profile>
            		<profile>
            		  <id>4thline</id>
            		  <repositories>
            			<repository>
            			  <id>4thline</id>
            			  <url>http://4thline.org/m2/</url>
            			  <releases>
            				<enabled>true</enabled>
            			  </releases> 
            			  <snapshots>
            				<enabled>true</enabled> 
            				<updatePolicy>always</updatePolicy>
            			  </snapshots>
            			</repository>
            		  </repositories>
            		</profile>
            	</profiles>
              	<activeProfiles>
            		<activeProfile>corp</activeProfile>
            		<activeProfile>aliyun</activeProfile>
            		<activeProfile>4thline</activeProfile>
            	</activeProfiles>
            </settings>

            结论

            Maven是每一个Java程序员都再熟悉不过的工具,但是我们真的了解它吗?

            任何一个优秀的框架、组件、工具,除了特殊的另外的特殊优化外,很多时候设计思路是大体相同的,我们在实际工作中不仅仅要扩展自己的涉猎领域,更需要在某些领域深入进入,对个人的提升是更为有益处的。

            声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。