执行 maven 命令 报错Unable to add module to the current project as it is not of packaging type 'pom'

本文记录了在本地搭建Maven工程时遇到的错误及其解决办法。问题源于工作空间根目录存在pom.xml文件导致M2eclipse插件误判。通过删除该文件成功解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天学习在本地搭建Maven工程时,执行了mvn archetype:generate 命令,报错。

Unable to create project from archetype [org.apache.maven.archetypes:maven-archetype-quickstart:1.1] 
Unable to add module to the current project as it is not of packaging type 'pom'

上网搜资料发现一个帖子,问题解决:

原文如下:

一开始以为是IDE出错了,很是着急,后来回想出错之前的操作。就在这之前,我备份了一个pom文件,然后直接移到了workspace的根目录下,心想是不是这个问题呢?带着疑问,我把这个pom.xml删掉了,然后再创建maven工程,果然不出所料,不再报错了。

为了确保是这个原因,后来google了一下,在国外一个论坛中碰到类似的问题,有个网友的回答摘抄如下:

Are you running the command from a directory that has an existing pom.xml file in it? I think that may be confusing Maven, as it thinks you're trying to add your new project as a sub-module of the project in the working directory.

原来是因为我在Workspace下放了个POM文件,造成了M2eclipse插件的误解,删掉就OK了。

使用命令行创建多模块项目 创建父项目: 使用以下命令创建父项目: Bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=my-multi-module \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false 删除父项目的 src 目录: Bash rm -rf my-multi-module/src 创建子模块: 进入父项目目录,并创建子模块: Bash cd my-multi-module mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=module1 \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false Bash mvn archetype:generate \ -DgroupId=com.example \ -DartifactId=module2 \ -DarchetypeArtifactId=maven-archetype-quickstart \ -DinteractiveMode=false 修改父项目的 pom.xml 文件,添加 <modules> 配置: XML <modules> <module>module1</module> <module>module2</module> </modules> 配置父 POM 和子模块 POM: 父项目的 pom.xml 文件中定义 <dependencyManagement> 和 <pluginManagement>。 子模块的 pom.xml 文件中添加 <parent> 配置,继承父项目的公共依赖和插件。 验证模块是否成功继承 检查子模块的 pom.xml 文件: 确保子模块中正确声明了 <parent> 配置。 子模块的 pom.xml 文件应包含父 POM 中定义的依赖和插件。 使用 Maven 命令验证构建: 在父项目目录下运行以下命令,验证整个项目的构建是否成功: Bash mvn clean install 检查依赖关系: 使用 mvn dependency:tree 命令查看子模块的依赖树,确保所有依赖都已正确解析。 在该步骤中的创建子模块我报错了:[INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 0.855 s [INFO] Finished at: 2025-07-31T14:13:34+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-archetype-plugin:3.4.0:generate (default-cli) on project my-multi-module: Unable to add module to the current project as it is not of packaging type 'pom' -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
最新发布
08-01
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值