您的位置:首页 > 大数据 > 人工智能

如何解决Maven: Failed to read artifact descriptor

2017-09-18 20:53 423 查看

简介

本文总结了在Maven多模块项目下,一模块引用其它模块时,如何解决“Failed to read artifact descriptor”问题。

多子模块项目创建

这两天尝试创建多模块项目,参考了《Maven权威指南》第6章的内容,其中父模块的POM配置如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent</name>
<url>http://maven.apache.org</url>

<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</pluginManagement>
</build>
</project>
simple-weather子模块的POM配置:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>simple-weather</artifactId>
<packaging>jar</packaging>
<name>simple-weather</name>
<url>http://maven.apache.org</url>

<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>

<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.1</version>
</dependency>

<dependency>
<groupId>velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.5</version>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>simple-weather</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- <skip>true</skip -->
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>

<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>jar-with-dependencies</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
</project>
simple-webapp子模块:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-weather</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<finalName>simple-webapp</finalName>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>


编译、安装

首先针对simple-weather子模块项目进行编译和安装:进入simple-weather子项目目录

mvn compile
mvn install
进入simple-webapp子模块项目目录下,通过mvn compile编译时,出现下面的错误

[ERROR] Failed to execute goal on project simple-webapp: Could not resolve dependencies for project
org.sonatype.mavenbook.ch06:simple-webapp:war:1.0-SNAPSHOT: Failed to collect dependencies at
org.sonatype.mavenbook.ch06:simple-weather:jar:1.0-SNAPSHOT: Failed to read artifact descriptor for
org.sonatype.mavenbook.ch06:simple-weather:jar:1.0-SNAPSHOT: Could not find artifact
org.sonatype.mavenbook.ch06:simple-parent:pom:1.0-SNAPSHOT -> [Help 1]
针对这个问题,首先是检查了POM文件配置,没有什么异常信息。查看本地.m2/repository的安装情况,也都正常。再次通过mvn -U clean install为simple-weather子模块进行清空后安装,问题依然存在。最终在stackoverflow上发现了类似的问题,发现有人给出的下面的解决方案,最终解决了我的问题:

This problem can occur if you have some child projects that refer to a parent pom and you have not installed from the parent pom directory (run mvn install from the parent directory). One of the child projects may depend on a sibling project and when it
goes to read the pom of the sibling, it will fail with the error mentioned in the question unless you have installed from the parent pom directory at least once.

大致翻译一下:如果你有子项目引用了父项目的POM,但没有在父项目POM目录下执行安装操作,这个问题就会出现。针对子模块依赖兄弟子模块的情况,需要在父项目POM目录下至少执行一次安装。

参考资料

1.
Maven: Failed to read artifact descriptor
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐