如何解决Maven: Failed to read artifact descriptor

简介

本文总结了在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

  • 10
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 13
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值