简体   繁体   中英

Command line mvn compile, but not IntelliJ

I am having trouble compiling a Maven project in IntelliJ.

  • If I run mvn clean install from commandline = SUCCESS
  • If I click on the "Reimport All Maven Projects" icon in IntellJ = SUCCESS
  • If I click on the "Generate Sources and Update Folders For All Projects" icon in IntelliJ = SUCCESS
  • If I click on the "install" Lifecyclein the Maven Projects panel = SUCCESS
  • After the Maven "install" I can even run the application without problems and I can see my latest source code changes.

Therefore I don't believe there is anything wrong with the project itself. But

  • IntelliJ shows there are a lots of methods and fields missing (red highlights)
  • Make Module ... fails
  • Compile Module ... fails
  • All Unit Tests running from IntelliJ fail with "java: cannot find symbol" errors, but PASS from the command line

I have

  • restarted the IDE several times
  • Click File -> Invalidate Caches/Restart...
  • Click File -> Project Structure... -> Problems = Empty panel (I assume it means no problems)
  • Click File -> Settings... -> Maven = Maven settings is correct

What else could I try to make IntelliJ intelligent enough to compile the project?

This can happen if command line uses a different version of mvn than IntelliJ. Make sure that Intellij is using the same maven executable that command line is using. First type: "which mvn" to find out the mvn executable that command line is using. Then make IntelliJ use the same version by going to: Intellij => Preferences => Build, Execution and Deployment => Build Tools => Maven => Maven Home Directory. Mvn Settings in IntelliJ 2017.1

Just ran into this problem. What worked for me was to close the project and delete the .idea folder.

Reopened the project then set it up as a maven project again...

All good after that.

Check the JDK setting for the project. In my experience, that is the most common cause of bizzare Intellij-cannot-compile problems.

Under "Project Settings" (CMD-,) - see the "Project SDK" setting.

Make sure it's a valid SDK, and the version matches the required version for the sources (ie check the maven compiler plugin source/target settings, and make sure the selected SDK is at least that version, or higher).

After a few years, I came across the same issue again but with another project. In this case IntelliJ was missing the Lombok plugin which generates the missing sources.

To install:

  1. Go to: File -> Settings -> Plugins
  2. Type "Lombok" in the search bar
  3. Click "Install" next to Lombok
  4. Restart IntelliJ

I've had this issue when I've had generated sources. Maven wasn't setup to mark the compiled-test-sources folder as a test sources root, so every time I did something with Maven I had to mark the generated folders.

I encountered a similar issues with compiling to Java 11 from IntelliJ Maven compile indicating that com.sun.management.HotSpotDiagnosticMXBean#dumpHeap() did not exist.

After trying the above steps, I still could not get it to resolve. I had recently migrated to a new instance of Ubuntu and decided to fire up my old VM, where the code compiled without an issue. So it occurred to me that when I installed IntelliJ on the new VM, I chose to download AdoptOpenJDK 11 ( adopt-openj9-11.0.7 ) via IntelliJ SDK download feature.

So I manually downloaded and installed a fresh copy of OpenJDK 11 ( jdk-11.0.6+10 ), then changed the project's default SDK to the new one I installed, and everything compiled just fine.

I hope this answer may help others.

I faced similar problem. Solution is, set the IntelliJ maven home directory to the installed maven location. Goto File->Settings And then set the maven home directory as shown in the image:

在此处输入图片说明

Have been facing this issue for long now, and usually the root cause for it is different maven version running on IntelliJ as mentioned above, but also sometimes it can be caused by different version of Java selected in the project structure.

As for me even after aligning the maven version the issue would not go, but once I checked the java version, Intellij had by defaulted selected Java 11 for me, and once I changed it back to Java 8, it started working again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM