IntelliJ community edition compiles a maven project despite compilation errors, why?
Hi,
I spend someitme today trying to figure out what is wrong with my code and come across the fact that the maven project was compiling despite have compilation issues (red in editor). I am not sure which settings I changed to cause this but I am sure that I dont have the eclipse compiler installed in my computer (I know that compiler can do this).
Any idea what can be wrong?
Thanks.
Please sign in to leave a comment.
Is it a multi-module project? If so, is the error in the module you are compiling? If you compile a single module, maven (and therefore IDEA) uses the version of the module in your local maven repository and not the code in the editor. When you compile multi modules, from a parent pom for example, maven creates all the artifacts, and then uses the built artifact to satisfy any multi-module inter-dependencies.
If that is not it, what happens from the command line when you run the maven build?
It is a single module and the errors are in the module's source code. So when I right click and select the run button on the maven project view it compiles fine (while it has 10 red lines in the code) but the same project fails to compile from the command line when I use "mvn clean install" for it.
Note that I use the same maven for intellij that I have installed in my system (changed the maven path and settins int he IntelliJ settings so that it use my global settings). The errors that it fails to catch and ignores when compiling are constructor parameters related, e.g e.g I refactored the constructor to have 6 parameters and in one of the classes that I was using that class's constructor I forgot to add the other parameter when creating the objects. This is a compilation error when I compile in command line and is ignored when I compile inside intelliJ.
Just as an aside FYI, IntelliJ IDEA comes with the eclipse compiler bundled in it. It can be set as the compiler to use in Settings > Compiler > Java Compiler > Use Compiler, But I do not think that is your issue since that is the compiler used when IntelliJ IDEA compiles the project directly. If you use maven within IntelliJ IDEA, it uses the JDK defined in the Maven > Runner settings. This usually defaults to the Project JDK.
So if I understand you correctly, a maven build succeeds when the maven build is launched from within IDEA, but fails (as it should) when run from the command line. Hmm... Nothing jumps to mind that would cause this behavior.
If I understand how a maven build runs withing IntelliJ IDEA, it does not directly call the mvn script. It calls it's own Java application that launches maven in a similar way that the mvn script does. But there is the possibility for an inconsistency to occur. If you look at the command line output when you run a maven build within IntelliJ IDEA, you will see it calls 'java', not 'mvn'. Java runs 'com.intellij.rt.execution.application.AppMain' to which are passed various arguments. It would seem that something in that arrangement is going wrong for you.
Here are some things I can suggest to try and troubleshoot and maybe resolve the issue:
I hope this helps. I wish I could give a more definitive answer. Perhaps someone else can.