IDEA运行maven项目报错解决方法梳理

IDEA运行maven项目报错解决方法梳理

1、问题现象

IDEA2022.3.3,新建maven-archetype项目后,出现图标异常问题。并且执行报错:

image

image

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

问题原因:

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

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

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

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

image

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

image

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

image

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

2、解决方法

2-1、修改启动配置

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

image

image

3、创建maven项目

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

image

image

加载本地maven骨架:

-DarchetypeCatalog=local

image

5、创建maven archetype

5-1、配置maven

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

image

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

5-2、修改配置文件

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

1)maven镜像源修改

此处用的是阿里源:

  <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)配置本地仓库路径:

<!-- 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>

5-3、创建maven archetype项目

新建项目:

image

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

image

6、maven及maven archetype对比

maven archetype目录结构:

image

maven 标准目录结构:

image

maven-quick-start目录结构:

image

posted @ 2023-08-03 00:01  mini小新  阅读(1267)  评论(0编辑  收藏  举报