IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

时间:2024-05-21 17:11:09

总结:

  1. 更改setting.xml的镜像源为alimaven
  2. archetype-catalog.xml下载到本地.m2目录下,同时设置-DarchetypeCatalog=internal
  3. mvn clean install -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true         (一行语句)

Q1.IDEA(eclipse)用maven自带的maven—archetype模板创建工程失败,创建过程如下:

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

错误症状为通过上述方式创建的maven工程目录不包含src目录,当然也不包括resources和webapp,具体结果为:IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

而且Pom.xml文件缺失内容比较多,基本的依赖和构建标签都木有,如下图所示:

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

pom中依赖不够,那我直接通过如下方式(import changes)添加依赖时,

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

Q2.控制台日志显示如下错误:(开始慌了)

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

具体内容为:

Could not transfer artifact com.alibaba:druid-spring-boot-starter:pom:1.1.10 from/to alimaven (http://maven.aliyun.com/nexus/content/groups/public/): PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

这样虽然可以向工程中添加相关目录(如src,resources和webapp)等,但是对于接触java web不久的我来说,还是有点不知所措,毕竟需要的jar包拿不到,接下来没法继续了,而且不清楚整个工程构建原理,开始慌了。

于是开始找问题;

对于问题Q2,在网上查了些文档,主要建议三个方法:

M1.在setting.xml中增加alimaven镜像,这个比较容易,就是把

<mirror>
          <id>alimaven</id>
          <mirrorOf>central</mirrorOf>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</mirror>

粘贴到<mirrors>标签下,粘贴后的结果如下,关注行号就OK。。。

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

M2.通过InstallCert.java这个工具生产证书来解决,具体参考https://www.iteye.com/blog/truth99-2160540,我照着做了,但貌似没生效。涉及的InstallCert.java文件下载要积分,随后我直接上传了。

M3. 执行maven打包命令时忽略https证书

mvn clean install  -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true,

目前我采用M3解决依赖搞不下的问题,但肯定不彻底,望分享。

 

再回到最初的问题Q1,工程目录不全

后来查看很多资料,发现问题出现在:maven-archetype插件下载失败。

大概原理为:

        maven的maven-archetype模板为maven一个插件,即maven-archetype-plugin-3.1.2.jar,创建工程不全主要是因为这个jar包下载不全,或不存在,当出现这个问题时,你可以到自己的本地仓库查看下:\repository\org\apache\maven\plugins\maven-archetype-plugin,是否存在对应的jar包;

        idea创建工程时需要加载archetype-catalog.xml (下载也需要积分,待会儿和InstallCert.java一起共享)来完成jar包的下载,但archetype-catalog.xml 比较大,约7Mb,网络较差的情况下,就无法正常下载,拿不到模板包,因此只能创建半截工程。

       于是直接去下载archetype-catalog.xml 到本地,让idea通过本地来加载该文件,此时需要指定

-DarchetypeCatalog=internal配置在如下图位置:

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

这也是好多人说的设置-DarchetypeCatalog=internal的原理吧,不设置internal,默认remote,又没有证书,只能报异常。

这种方式可以解决“半截工程”的问题,但问题Q2仍旧未有效解决。

那我下载的archetype-catalog.xml放置位置在哪?

建议放置在本地的.m2文件夹同时在.m2下增加settings.xml文件,这样新建工程,目录结构就正常了。同时也增加setting.xml到该目录,结构如下:

IDEA创建maven—archetype工程失败,更新pom.xml依赖,提示PKIX path building failed错误

总结:

  1. 更改setting.xml的镜像源为alimaven
  2. archetype-catalog.xml下载到本地.m2目录下,同时设置-DarchetypeCatalog=internal
  3. 用该命令来执行jar包的更新 :   mvn clean install -Dmaven.multiModuleProjectDirectory=$MAVEN_HOME -Dmaven.wagon.http.ssl.insecure=true -Dmaven.wagon.http.ssl.allowall=true -Dmaven.wagon.http.ssl.ignore.validity.dates=true         (一行语句)