Search code examples
javamavenintellij-idea

IntelliJ does not recompile/build maven project


I am running into problems when using IntelliJ for Spring development. At first, the IntelliJ build command works fine and picks up changes I make to the source code. At some point, this stops working and I can only compile my sources through maven, as the build or rebuild project commands seem to do nothing anymore.

I am confused, as it works as desired at the start but then stops working at some point.

Steps to recreate:

  • Create a new Spring project using Spring Initializr:

    curl https://start.spring.io/starter.zip -d type=maven-project -d javaVersion=11 -o demo.zip && unzip demo.zip

  • Start IntelliJ from the command line in that folder, this will auto-import the project: idea64 .

  • Write a log message in the main class and run the project using Run->Run 'DemoApplication'. This compiles the sources to the target folder and starts up the project. You should see the log message

  • Alter the log message, run again and the changes should be visible (so far so good)

  • Now re-compile the sources via maven command mvn compile and restart IntelliJ

  • From now on, running build or rebuild project within IntelliJ will not pick up any changes I make to any of my source files. In fact both commands don't seem to do anything at all. I have to compile via maven in order to properly compile my sources

I tried a few things to overcome this problem, but without success:

  • Invalidated caches/restart
  • Checked project settings for Project Compiler Output and set it to <project-folder>/target
  • Checked project settings/modules for output path and test output path and put in the path as above
  • Used Reimport All Maven Projects
  • Deleted the .idea folder within the project folder. This seems to work, but is very inconvenient and surely no intended behavior. Also it breaks again if I recreate the steps detailed above.

Is anyone experiencing similar problems and/or has a solution?


Solution

  • So for anyone running into the problems I described above:

    Apparently deleting the .idea folder and restarting IntelliJ does indeed do the trick. The folder is recreated on restart and afterwards I'm able to switch between compiling within IntelliJ using run, build or rebuild project and compiling through maven goals (mvn compile, mvn test, mvn surefire:test, ...). This persists through restarts of IntelliJ such that I don't have to repeat this everytime I open a project.

    I still don't know why this behavior happens, but it's a solution I'm happy to work with as long as I only have to do it every now and then for a project.