• 关于Maven找不到依赖或者下载慢的问题总结


    导语

    Maven和gradle是现在JAVA世界中最普遍的两个依赖管理工具。很多人最开始接触的便是maven,而即便是使用gradle的人,也不能保证你即将接触的项目不是基于maven的。
    相信作为一个JAVA开发者,一定会遇到不少Maven相关的错误。这里总结一下一些maven的使用经验,能解决几乎所有平时能遇到的棘手问题。

    2020年01月15日起 Maven中央仓库禁止了HTTP访问,需要修改HTTP为HTTPS才能访问。

    Effective January 15, 2020, The Central Repository no longer supports insecure communication over plain HTTP and requires that all requests to the repository are encrypted over HTTPS.

    If you're receiving this error, then you need to replace all URL references to Maven Central with their canonical HTTPS counterparts
    参考链接

    找不到jar,无法在某某仓库找到jar

    解决方案:
    以commons-collections为例,

    1. 检查是不是网络问题:
      mvn dependency:get -Dartifact=org.apache.commons:commons-collections4:4.4
    2. 如果不能下载,则可能是仓库问题,需要去https://mvnrepository.com/查找该jar所在的仓库,一般来说 central的jar,下载失败,肯定是因为网速问题。如果一些jar不在central,例如:
    <dependency>
        <groupId>org.apache.hadoop</groupId>
        <artifactId>hadoop-common</artifactId>
        <version>2.6.0-cdh5.13.2</version>
    </dependency>
    

    网站会提示:Note: this artifact it located at Cloudera Rel repository (//repository.cloudera.com/content/repositories/releases/)
    那么,要么你的项目pom.xml需要配置 repository要么 在 MAVEN_HOME/conf/settings.xml中的profile中配置全局repository

    IDEA中,某一个依赖的import报红,或者某一个方法报 cannot resolve symbol之类的,而其实是可以用mvn package打包的,就是不能在本地运行。

    解决方案:

    1. 可能的原因有,本地仓库的jar在网络传输中损坏,删除掉重新下载。
    2. 重新下载的方式有 点击右侧刷新reimport,以及手动下载,执行mvn dependency:get -Dartifact=$groupId:$artifactId:$version
    3. 最后一个终极解决办法,无需重新安装IDEA,无需invalid Cache And Restart,直接退出IDEA,删除%USERPROFILE%.IdeaIC2019.3systemMaven,重新打开IDEA,IDEA会update maven indices。
    4. 对于IDEA 2020.1.x的版本,位置变成了%APPDATA%LocalJetBrainsIdeaIC2020.1Maven
    5. 如果你曾经手动删除过local Repository的某一个文件夹,这个缓存就对不上了,特别容易出现这个问题。

    IDEA中,有一个已知的非中央仓库的,特别是私服的jar,如果IDEA点击刷新reimport报错:cannot resolve $groupId:$artifactId:$version

    解决方案:

    1. 可手动下载mvn dependency:get -Dartifact=$groupId:$artifactId:$version
    2. 如果1不行,则参考上述删除缓存的方案,如果是401 unauthorzied之类的,则检查settings.xml语法是否正常,Server的账户密码是否正确。

    maven下载慢

    maven镜像服务器是肯定需要配置的,有华为云,腾讯云,阿里云镜像服务器,又快又稳。注意,每个人所在的公司的网络环境可能有所不同,访问阿里云不一定是最快的,或者访问腾讯云不是最快的,甚至很慢。
    可以在构建项目的时候多做测试。

    下面给出最快的Maven三个镜像服务器

     <!-- 阿里云仓库 -->
        <mirror>
          <id>alimaven</id>
          <mirrorOf>central</mirrorOf>
          <name>aliyun-maven</name>
          <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
        </mirror>
    
        <!-- 中央仓库1 -->
        <mirror>
          <id>repo1</id>
          <mirrorOf>central</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>https://repo1.maven.org/maven2/</url>
        </mirror>
    
        <!-- 中央仓库2 -->
        <mirror>
          <id>repo2</id>
          <mirrorOf>central</mirrorOf>
          <name>Human Readable Name for this Mirror.</name>
          <url>https://repo2.maven.org/maven2/</url>
        </mirror>
    
        <!-- 腾讯云 -->
        <mirror>
          <id>nexus-tencentyun</id>
          <name>Nexus tencentyun</name>
          <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
        </mirror>
    
        <!--华为云 -->
        <mirror>
          <id>huaweicloud</id>
          <url>https://mirrors.huaweicloud.com/repository/maven/</url>
        </mirror>
    

    常见的误区

    注意,99%的JAR都是在中央仓库的,还有一些在公司的私服,也就是nexus repository,还有一些在第三方开源仓库,例如SpringPlugins:

    <dependency>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>jetty</artifactId>
        <version>6.1.26.cloudera.4</version>
    </dependency>
    

    Note: this artifact it located at Spring Plugins repository (//repo.spring.io/plugins-release/)
    因此镜像服务器不要上来就配mirrorOf *, 这个表示所有的jar都从这里下载,这肯定是不行的。

    <mirror>
       <id>alimaven</id>
       <mirrorOf>*</mirrorOf>
       <name>aliyun-maven</name>
       <url>http://maven.aliyun.com/nexus/content/repositories/central/</url>
    </mirror>
    

    正确做法是

    <mirrorOf>external:*</mirrorOf>
    

    或者是

    <mirrorOf>central</mirrorOf>
    

    external表示URL是127.0.0.1或者localhost以外的
    参见https://github.com/apache/maven/blob/maven-3.3.9/... external的源码

    另外,对于公司自己的私服,肯定是要在mirrorOf里面排除的,假设私服的id是 snaps,不论是在 pom.xml还是在 settings.xml的profiles里面的repository内配置了这个私服,
    在mirror中的写法:

     <mirrorOf>external:*,!snaps</mirrorOf>
    

    而且对于某一个找不到jar的依赖,应该养成习惯,第一时间检查这个artifact是不是在maven中央仓库,是不是在某一个第三方仓库。如果在某一个第三方仓库,则把它加入到自己的pom.xml中,或者全局配置。
    除此以外,还可以将这个仓库加入自己私服的PROXY代理中。

  • 相关阅读:
    标准库中的生成器函数
    Python 数据分析5
    Chrome 开发者工具(三)------ Sources
    Chrome 开发者工具(二)------ Console
    Chrome 开发者工具 F12(一)
    jquery 获取自定义属性的值 data-*
    PHP 常用函数备忘
    Winsows 服务器,PHP 开发环境搭建
    FuelPHP 查看 Query SQL
    Laravel —— could not find driver
  • 原文地址:https://www.cnblogs.com/slankka/p/12202640.html
Copyright © 2020-2023  润新知