Can't compile/run a java project in IntelliJ

妖精的绣舞 提交于 2021-02-07 20:36:16

问题


The issue:

While it was working fine yesterday. Today, I wasn't able to compile my project(spring-boot). I created a demo project to simulate the same issue that happened in my current project.

When I try to run the project using the run button It doesn't want to compile. Note all the errors from the failed compilation. Also, you can see that all dependencies are imported (see External Libraries):

I know I can run the app by typing mvn spring-boot:run in the terminal. But for some reason, I don't want to do that.

As you can see, this demo is a simple spring-boot project:

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>

What I 've tried:

I've tried countless solutions among them:

  • Select Build->Rebuild Project
  • Clicking File>Invalidate caches/ restart
  • mvn clean -> Build -> Make Project
  • Maven -> Reimport
  • Deleted .iml file and re-imported the project
  • Checked that there is no excludes in Preferences | Build, Execution, Deployment | Compiler | Excludes

About 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

I've spent lots of time on this issue. How can I fix it?

From comments:

Running 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] =======================================================================

回答1:


Thanks to Andry's comment my issue was solved. Here are the steps I've done:

  1. Export IDE settings(keymaps, plugins). You can export it by going to File | Manage IDE settings | Export Settings... It will export a zip file that you can later import it when you restore default IDE configuration
  2. Go to File | Manage IDE settings | Restore Default Settings... the IDE will restart and will be restored to the default configuration. this command re-generated path.macros.xml file that was missing with the right configuration. Note that you can file in <IDE config>/options directory
  3. Import the exported zip file File | Manage IDE settings | Import Settings...



回答2:


Steps you can take:

  1. Delete .m2 folder
  2. Try to run mvn clean install to install all of your dependencies.

For IntelliJ, configure for Maven

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

For running Spring Boot:

Run/Debug Configurations -> Spring Boot

Code Coverage:

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



回答3:


If Maven succeeds and IntelliJ fails, it would seem to me that you have to tell IntelliJ to look at the same folder as Maven for creating and running artifacts.

Go to your project settings and make sure that IntelliJ's output folder is /target, right under the root of your project.

If you have an artifact, make sure that its build path is the same as the one created by Maven.

Check your SDK while you're at it. Make sure you have the correct one.




回答4:


Maven is probably not configured properly in your Settings. Go to:

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

And check the values Maven home directory, User settings file and especially Local repository (make sure it's the same that is used when you directly do mvn install).




回答5:


Run into the same issue, though with a Gradle build. The problem in that case was, the IDE checks for newer versions of the artifacts / dependencies in the build.gradle file and records them, therefore your build.gradle is updated with the new version codes for the artifacts. The IDE though does not download the latest versions when doing that.

SOLUTION: Rebuild the project when online. Or check the build.gradle file for modifications and revert to the previous versions.
NOTE: This is a Gradle solution, substitute appropriately for a maven build.




回答6:


I pulled my hair for an hour on this. At the end, it turned out Intellij was using a different maven home than what maven is actually using. The library was not actually present in that intellij specific .m2 folder. This discrepancy is exposed only after you do "Invalidate Caches and Restart" and then the IDE starts showing those dependencies in red.



来源:https://stackoverflow.com/questions/60990976/cant-compile-run-a-java-project-in-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!