Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说maven私有仓库搭建_私有仓库储存包括,希望能够帮助你!!!。
近期在调研一个开源仓库,于是将 代码 从github下载后,当IDEA sync依赖时出现Cannot resolve org.fourthline.cling:cling-support:2.1.1 的问题,详情如下:
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仓库分为私有仓库、公有仓库、第三方仓库。其中:
熟悉仓库的分类对于解决上文中遇到的问题有这个重要的作用。
遇到依赖缺失的问题后,首先需要确认私有仓库是否存在依赖,查看nexus发现并没有需要的依赖,可以断定私有仓库没有此依赖。
Maven官方的仓库提供了Web搜索页面,地址:repo1.maven.org/maven2/,尝试搜索后发现也没有需要的依赖。如下:
经过以上搜索后,可以断定 代码 需要的依赖,一定是由第三方仓库提供的。如何找到是在哪个第三方仓库呢?
此时,就需要mvnrepository来提供帮助了,地址: mvnrepository.com/ 。mvnrepository提供了公共仓库和第三方仓库中jar包的索引、查询、下载等实用功能。
我们尝试搜索需要的依赖, mvnrepository.com/artifact/or…,结果如下图:
可以看到在mvnrepository中找到了需要的依赖。那么问题来,如何知道第三方仓库的地址呢?可以详细看上图中箭头指向的区域,这里展示了第三方仓库的maven url。
在实践中,完全可以跳过搜索
公共仓库,因为mvnrepository已经包含了公共仓库的依赖。
maven仓库的配置是在setting.xml配置的。如果要混合配置私有仓库和公共仓库,需要在setting.xml增加新的mirror和profile,并激活新的activeProfile。
setting.xml有mirrors节点,是用来配置镜像URL的。mirrors可以配置多个mirror,每个mirror有id,name,url,mirrorOf属性,详细解释如下:
我们这次需要添加的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中配置的顺序执行,而是根据字母排序来指定第一个,然后会遍历所有的仓库,直至找到需要的依赖。
作用:根据环境参数来调整构建配置的列表,settings.xml中的profile元素是pom.xml中profile元素的裁剪版本。
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>
作用:手动激活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侧边栏中,可以看到多了4thline的profile。(可以看到IDEA的profiles是按照首字母排序的,和上文中mirror的定义是一致的。)
重新执行Reload All Maven Projects,所有的依赖都正常import。
完整版的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程序员都再熟悉不过的工具,但是我们真的了解它吗?
任何一个优秀的框架、组件、工具,除了特殊的另外的特殊优化外,很多时候设计思路是大体相同的,我们在实际工作中不仅仅要扩展自己的涉猎领域,更需要在某些领域深入进入,对个人的提升是更为有益处的。
欢迎大家一起交流讨论。