无法在IntelliJ中编译/运行Java项目

3

问题:

昨天还能正常工作,但今天我无法编译我的项目(spring-boot)。我创建了一个演示项目,以模拟发生在当前项目中的相同问题。

当我尝试使用运行按钮运行该项目时,它不想编译。请注意所有编译失败的错误。此外,您可以看到已导入所有依赖项(参见外部库):

enter image description here

我知道我可以在终端中键入mvn spring-boot:run来运行应用程序。但出于某种原因,我不想这样做。

如您所见,这个演示是一个简单的spring-boot项目:

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demox</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demox</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

我尝试过:

我尝试了无数种解决方案,其中包括:

  • 选择Build->Rebuild Project
  • 点击File>Invalidate caches/ restart
  • mvn clean -> Build -> Make Project
  • Maven -> Reimport
  • 删除.iml文件并重新导入项目
  • 检查Preferences | Build,Execution,Deployment | Compiler | Excludes中是否有排除项

关于IntelliJ IDEA:

IntelliJ IDEA 2019.3.4 (Ultimate Edition)
Build #IU-193.6911.18, built on March 17, 2020
Licensed to Hamza Belmellouki
Subscription is active until October 10, 2020
For educational use only.
Runtime version: 11.0.6+8-b520.43 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.3
GC: ParNew, ConcurrentMarkSweep
Memory: 1502M
Cores: 4
Registry: debugger.click.disable.breakpoints=true, documentation.show.toolbar=true, debugger.watches.in.variables=false, compiler.automake.allow.when.app.running=true, ide.tree.collapse.recursively=false, ide.balloon.shadow.size=0
Non-Bundled Plugins: AceJump, BrowseWordAtCaret, DBN, GsonFormat, IdeaVIM, Lombook Plugin, MavenRunHelper, com.4lex4.intellij.solarized, com.alayouni.ansiHighlight, com.berzanmikaili.intellij.monokai_pro, com.codota.csp.intellij, com.daylerees.rainglow, com.github.holgerbrandl.pasteimages/, com.github.mturlo.intellij.theme.spacegray, com.github.shiraji.emoji, com.godwin.json.parser, com.intellij.ideolog, com.intellij.jira, com.intellij.plugins.html.instantEditing, com.jetbrains.CyanTheme, com.jetbrains.colorful.toolwindow.icons, com.jetbrains.darkPurpleTheme, com.jetbrains.grayTheme, com.ppolivka.gitlabprojects, com.robohorse.robopojogenerator, com.sburlyaev.terminal.plugin, com.viartemev.requestmapper, com.vincentp.gruvbox-theme, commit-template-idea-plugin, mobi.hsz.idea.gitignore, net.seesharpsoft.intellij.plugins.csv, tanvd.grazi, org.asciidoctor.intellij.asciidoc, org.intellij.scala, org.root.GoogleIt, org.sonarlint.idea, some.awesome

我在这个问题上花费了很多时间。我该如何解决它?

来自评论:

运行 mvn clean install -X

Apache Maven 3.6.0 (97c98ec64a1fdfee7767ce5ffb20918da4f719f3; 2018-10-24T19:41:47+01:00)
Maven home: /usr/local/Cellar/maven/3.6.0/libexec
Java version: 1.8.0_192, vendor: Oracle Corporation, runtime: /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre
Default locale: en_MA, platform encoding: UTF-8
OS name: "mac os x", version: "10.15.3", arch: "x86_64", family: "mac"
[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign packages into class realm maven.api
[DEBUG]   Imported: javax.annotation.* < plexus.core
[DEBUG]   Imported: javax.annotation.security.* < plexus.core
[DEBUG]   Imported: javax.enterprise.inject.* < plexus.core
[DEBUG]   Imported: javax.enterprise.util.* < plexus.core
[DEBUG]   Imported: javax.inject.* < plexus.core
[DEBUG]   Imported: org.apache.maven.* < plexus.core
[DEBUG]   Imported: org.apache.maven.artifact < plexus.core
[DEBUG]   Imported: org.apache.maven.classrealm < plexus.core
[DEBUG]   Imported: org.apache.maven.cli < plexus.core
[DEBUG]   Imported: org.apache.maven.configuration < plexus.core
[DEBUG]   Imported: org.apache.maven.exception < plexus.core
[DEBUG]   Imported: org.apache.maven.execution < plexus.core
[DEBUG]   Imported: org.apache.maven.execution.scope < plexus.core
[DEBUG]   Imported: org.apache.maven.lifecycle < plexus.core
[DEBUG]   Imported: org.apache.maven.model < plexus.core
[DEBUG]   Imported: org.apache.maven.monitor < plexus.core
[DEBUG]   Imported: org.apache.maven.plugin < plexus.core
[DEBUG]   Imported: org.apache.maven.profiles < plexus.core
[DEBUG]   Imported: org.apache.maven.project < plexus.core
[DEBUG]   Imported: org.apache.maven.reporting < plexus.core
[DEBUG]   Imported: org.apache.maven.repository < plexus.core
[DEBUG]   Imported: org.apache.maven.rtinfo < plexus.core
[DEBUG]   Imported: org.apache.maven.settings < plexus.core
[DEBUG]   Imported: org.apache.maven.toolchain < plexus.core
[DEBUG]   Imported: org.apache.maven.usability < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.* < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.authentication < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.authorization < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.events < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.observers < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.proxy < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.repository < plexus.core
[DEBUG]   Imported: org.apache.maven.wagon.resource < plexus.core
[DEBUG]   Imported: org.codehaus.classworlds < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.* < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.classworlds < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.component < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.configuration < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.container < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.context < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.lifecycle < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.logging < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.personality < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.util.xml.Xpp3Dom < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.util.xml.pull.XmlPullParser < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.util.xml.pull.XmlPullParserException < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.util.xml.pull.XmlSerializer < plexus.core
[DEBUG]   Imported: org.eclipse.aether.* < plexus.core
[DEBUG]   Imported: org.eclipse.aether.artifact < plexus.core
[DEBUG]   Imported: org.eclipse.aether.collection < plexus.core
[DEBUG]   Imported: org.eclipse.aether.deployment < plexus.core
[DEBUG]   Imported: org.eclipse.aether.graph < plexus.core
[DEBUG]   Imported: org.eclipse.aether.impl < plexus.core
[DEBUG]   Imported: org.eclipse.aether.installation < plexus.core
[DEBUG]   Imported: org.eclipse.aether.internal.impl < plexus.core
[DEBUG]   Imported: org.eclipse.aether.metadata < plexus.core
[DEBUG]   Imported: org.eclipse.aether.repository < plexus.core
[DEBUG]   Imported: org.eclipse.aether.resolution < plexus.core
[DEBUG]   Imported: org.eclipse.aether.spi < plexus.core
[DEBUG]   Imported: org.eclipse.aether.transfer < plexus.core
[DEBUG]   Imported: org.eclipse.aether.version < plexus.core
[DEBUG]   Imported: org.fusesource.jansi.* < plexus.core
[DEBUG]   Imported: org.slf4j.* < plexus.core
[DEBUG]   Imported: org.slf4j.helpers.* < plexus.core
[DEBUG]   Imported: org.slf4j.spi.* < plexus.core
[DEBUG] Populating class realm maven.api
[INFO] Error stacktraces are turned on.
[DEBUG] Message scheme: color
[DEBUG] Message styles: debug info warning error success failure strong mojo project
[DEBUG] Reading global settings from /usr/local/Cellar/maven/3.6.0/libexec/conf/settings.xml
[DEBUG] Reading user settings from /Users/hamzabelmellouki/.m2/settings.xml
[DEBUG] Reading global toolchains from /usr/local/Cellar/maven/3.6.0/libexec/conf/toolchains.xml
[DEBUG] Reading user toolchains from /Users/hamzabelmellouki/.m2/toolchains.xml
[DEBUG] Using local repository at /Users/hamzabelmellouki/.m2/repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10.0 for /Users/hamzabelmellouki/.m2/repository
[INFO] Scanning for projects...
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for maven-releases (https://nexus.sdf.x-hub.io/repository/maven-releases).
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for maven-snapshots (https://nexus.sdf.x-hub.io/repository/maven-snapshots).
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for central (http://central).
[DEBUG] Extension realms for project com.example:demox:jar:0.0.1-SNAPSHOT: (none)
[DEBUG] Looking up lifecycle mappings for packaging jar from ClassRealm[plexus.core, parent: null]
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for sonatype-nexus-snapshots (https://oss.sonatype.org/content/repositories/snapshots).
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for central (https://repo.maven.apache.org/maven2).
[DEBUG] Using mirror central (https://nexus.sdf.x-hub.io/repository/maven-public/) for apache.snapshots (https://repository.apache.org/snapshots).
[DEBUG] Extension realms for project org.springframework.boot:spring-boot-starter-parent:pom:2.2.6.RELEASE: (none)
[DEBUG] Looking up lifecycle mappings for packaging pom from ClassRealm[plexus.core, parent: null]
[DEBUG] Extension realms for project org.springframework.boot:spring-boot-dependencies:pom:2.2.6.RELEASE: (none)
[DEBUG] Looking up lifecycle mappings for packaging pom from ClassRealm[plexus.core, parent: null]
[DEBUG] === REACTOR BUILD PLAN ================================================
[DEBUG] Project: com.example:demox:jar:0.0.1-SNAPSHOT
[DEBUG] Tasks:   [clean, install]
[DEBUG] Style:   Regular
[DEBUG] =======================================================================

执行以下命令:mvn clean install -X。它会提供执行详细信息,如果你仍然解决不了问题,可以在此贴一张截图让我们来看看。 - IllegalSkillsException
你只收到这么多日志了吗? - IllegalSkillsException
请看此处:https://gist.github.com/Hamzablm/bcee101487c7ddb13bc0fc2d1706bf62 - Hamza Belmellouki
4
请尝试从https://www.jetbrains.com/idea/download/下载2020.1版本的IDE,并使用默认设置运行它:使用文件|管理设置|“还原默认设置”操作来在2020.1版本中重置它们。另外,请确保在<IDE config>/options目录中存在path.macros.xml文件,参见此评论 - Andrey
你救了我的一天。谢谢! - Hamza Belmellouki
显示剩余7条评论
8个回答

14

感谢Andry的评论,我的问题得以解决。

  1. 导出IDE设置(键盘映射、插件)。您可以通过进入文件| 管理IDE设置|导出设置...来导出它。它将导出一个zip文件,您可以在恢复默认IDE配置时稍后导入它
  2. 转到文件| 管理IDE设置|恢复默认设置... IDE将重新启动并恢复为默认配置。此命令使用正确的配置重新生成了缺失的path.macros.xml文件。请注意,您可以在<IDE config>/options目录中找到它
  3. 导入导出的zip文件 文件| 管理IDE设置| 导入设置...

4
这是新的“清除缓存并重启”=)) - Cosmin Lehene
3
很遗憾,它没有帮助我解决同样的问题。 - 27P
1
我也遇到了这种可怕的情况。但很遗憾,我们不得不采取这样破坏性的解决方案!希望Intellij的开发人员能够采取措施解决这个问题。 - Shades88

1
我在这个问题上纠结了一个小时。最后,结果是Intellij使用的Maven Home与实际使用的Maven不同。该库实际上并不存在于Intellij特定的.m2文件夹中。只有在执行“无效缓存并重启”之后,才会暴露出这种差异,并且IDE开始显示这些依赖项为红色。

0

您可以采取以下步骤:

  1. 删除 .m2 文件夹
  2. 尝试运行 mvn clean install 以安装所有依赖项。

对于 IntelliJ,配置 Maven

Run/Debug Configurations -> Maven -> Command Line: clean install

运行Spring Boot:

Run/Debug Configurations -> Spring Boot

代码覆盖率

Run/Debug Configurations -> Spring Boot -> Code Coverage

另外,我已经检查了所有的依赖项都在.m2文件夹中。 - Hamza Belmellouki
你在问题中写了 mvn clean - Ayman Arif
尝试删除.m2文件夹,然后再次运行mvn clean install - Ayman Arif
是的,我之前提到过。 - Hamza Belmellouki
我想将它作为Spring Boot编辑配置来运行,而不是Maven配置。因为这样可以使用IntelliJ代码覆盖工具。 - Hamza Belmellouki
显示剩余3条评论

0

Maven 可能在您的设置中未正确配置。请前往:

File > Settings... > Build, Execution, Deployment > Build Tools > Maven

检查以下值:Maven主目录用户设置文件,尤其是本地存储库(确保它与直接执行mvn install时使用的相同)。


我以为他/她说Maven成功了,但IntelliJ失败了。 - duffymo
事实上,当你在IntelliJ中将一个项目转换为Maven项目时,它会从设置中配置的本地仓库获取所需的依赖项,并使用这些设置下载缺失的依赖项。如果此配置不正确,则无法下载JAR文件,但是当你在终端中使用Maven时,它可能会使用不同的配置,这可能会起作用。 - Nyamiou The Galeanthrope

0
如果Maven成功而IntelliJ失败,那么你需要告诉IntelliJ去查看与Maven相同的文件夹以创建和运行构件。
进入项目设置并确保IntelliJ的输出文件夹是在项目根目录下的/target。
如果有一个构件,请确保它的构建路径与Maven创建的路径相同。
在此期间检查您的SDK,确保使用的是正确的SDK。

0
遇到了同样的问题,不过是在一个Gradle构建中。那种情况下的问题是,IDE会检查build.gradle文件中更高版本的艺术品/依赖项,并记录它们,因此你的build.gradle文件会更新为艺术品的新版本代码。但是,IDE在这样做时并不会下载最新版本。
解决方法:在线重新构建项目,或检查build.gradle文件的修改并恢复到以前的版本。
注意:这是一个Gradle解决方案,如果是maven构建,请相应地进行替换。

0

使用2022.2.1版本,同样的问题出现了。

折磨了两天,尝试了所有方法(包括缓存失效、重新导入、重启、重新加载、重建、IDE版本升级/降级等),但都没有用!

我是通过IntelliJ的新项目向导创建有问题的Spring Boot项目的。在尝试了这里那里提供的所有建议后,最后一招,我通过Spring initializr创建了一个新项目,并将其重新导入为一个新模块。成功了!

我不知道到底修复了什么,但那个奇怪的异常已经消失了!


0

类似的问题,结果是Intellij使用了/.m2/repository和Maven /.m2


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接