IDEA build is delegated to Maven build -- solve the problem that the code runs without compiling errors but reports an error: "The package xxx does not exist"

First of all, let’s talk about a pit of Intellij IDEA——Under the maven project in IDEA, this situation may occur: there is no compilation error in the code editor, and it is no problem to perform various operations through maven’s clean, compile, and package. Click the green arrow to run (by default, the Build operation of the IDE itself will be executed first), but it is reported that the package xxx does not exist. The screenshot of the problem is as follows:

 

 

This problem is very headache. After groping, I found that the Build (compilation) operation of IDEA is separate from the Build of Maven, and they are not the same thing. In the case of such a problem, I even suspect that the real-time syntax compilation in the editor is not consistent with the classpath at Build time, otherwise this weird situation will not occur. The reason for this problem cannot be confirmed, whether it is a bug in IDEA itself, if so, then blame me for using the latest 2020.1 version of IDEA. In any case, the problem has to be solved, and Ben Shuai has already figured out everything.

The solution is to entrust the IDE's Build/Run operation to Maven to unify the two, which means that the IDE and Maven always use the same classpath for compilation. Specifically, in the File -> Settings -> Build, Execution, Deployment -> Build Tools -> Maven -> Runner dialog box, check "Delegate IDE build/run actions to Maven". In this way, when you click the green arrow to run, the Build that is executed incidentally is the Maven Build, so as long as Maven can succeed, the operation will be fine.

 

Guess you like

Origin blog.csdn.net/liudun_cool/article/details/106106982