IDEA新建maven项目出现x图标,运行报错解决方法

本文最后更新于:2024年4月21日 晚上

1、问题现象

IDEA2022.3.3,新建maven-archetype项目后,出现图标异常问题,左上角会显示x图标。并且无法执行程序:

image-20230524094939415

image-20230524094913810

问题原因分析:判断为文件路径结构异常,未识别到以下路径的文件,导致执行异常。尝试删除package $org.example;,依旧提示找不到对应类:

image-20230524094757419

问题原因:

选择的项目为maven archetype项目构建,此项目生成的目录结构不同于maven项目。如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
archetype
|-- pom.xml
`-- src
`-- main
`-- resources
|-- META-INF
| `-- maven
| `--archetype-metadata.xml
`-- archetype-resources
|-- pom.xml
`-- src
|-- main
| `-- java
| `-- App.java
`-- test
`-- java
`-- AppTest.java

而此项目生成对应的Module结构体为maven项目结构,如下:

image-20230524150755533

因而,导致除resources路径能识别之外,其他路径均无法识别,导致找不到对应的packages和class。因而执行之后程序报错。

解决方法:使用maven-quickstart快速创建项目。

image-20230524155451915

依据电脑性能生成时间长短不一,出现BUILD SUCCESS即生成成功:

image-20230524160602362

创建完成的路径结构如下:

image-20230524160707938

缺少的resources路径可自行创建。

2、解决方法

修改启动配置:

点击窗口右上角运行配置图标,选择Edit Configurations...

image-20230524095259624

image-20230524110142397

3、创建maven项目

选择创建一个快速maven项目:

image-20230524154307157

image-20230524114708392

加载本地maven骨架:

1
-DarchetypeCatalog=local

image-20230524114845490

4、创建maven archetype

4.1、配置maven

IDEA中,使用Ctrl+Alt+S快捷键,打开设置页面,搜索maven进入maven配置页面,具体设置如下:

image-20230524141943170

修改完成后apply应用,点击OK即可。

4.2、修改配置文件

编辑器打开setting配置文件修改:

1)maven镜像源修改

此处用的是阿里源:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<mirrors>
<!-- mirror
| Specifies a repository mirror site to use instead of a given repository. The repository that
| this mirror serves has an ID that matches the mirrorOf element of this mirror. IDs are used
| for inheritance and direct lookup purposes, and must be unique across the set of mirrors.
|
<mirror>
<id>mirrorId</id>
<mirrorOf>repositoryId</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://my.repository.com/repo/path</url>
</mirror>
-->
<mirror>
<id>alimaven</id>
<mirrorOf>*</mirrorOf>
<name>aliyun maven</name>
<url>https://maven.aliyun.com/repository/public</url>
<blocked>false</blocked>
</mirror>
</mirrors>

2)配置本地仓库路径:

1
2
3
4
5
6
7
<!-- localRepository
| The path to the local repository maven will use to store artifacts.
|
| Default: ${user.home}/.m2/repository
<localRepository>/path/to/local/repo</localRepository>
-->
<localRepository>D:\codefiles\MavenRepo</localRepository>

4.3、创建maven archetype项目

新建项目:

image-20230524122435042

使用Maven archetype模板新建(相较于maven,提高了加载速度),参考以下配置:

image-20230524143314409

5、maven及maven archetype对比

maven archetype目录结构:

image-20230524150553771

maven 标准目录结构:

image-20230524150755533

maven-quick-start目录结构:

image-20230524152123543

参考文档:

Apache官方文档

https://blog.csdn.net/zzvar/article/details/114879815

https://juejin.cn/s/maven%20archetype%E5%92%8Cmaven%E7%9A%84%E5%8C%BA%E5%88%AB

https://juejin.cn/post/7052210790748651551

https://blog.csdn.net/anddyhua/article/details/88548791

https://www.jianshu.com/p/03b41d172e5e

https://blog.csdn.net/qq_42490075/article/details/106957579

https://blog.csdn.net/hgnuxc_1993/article/details/125427590


IDEA新建maven项目出现x图标,运行报错解决方法
http://www.codestar.top/2023/11/04/JAVA/IDEA新建maven项目出现x图标,运行报错解决方法/
作者
mini小新
发布于
2023年11月4日
更新于
2024年4月21日
许可协议