Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why refresh of Maven repository is not enough for IntelliJ?

I had a NoClassDefFoundError problem with some test, launched from IntelliJ. In order to repair the situation, I had to make several changes in many poms of the project - adding new packages and excluding some old ones for to escape the overlapping of them. Also, I reapired the situation with different versions. But the situation did not improve. Again, some package, declared in pom, was not found where it should be.

I refreshed the maven repository by

mvn -e clean install -U

, as is advised in https://stackoverflow.com/a/9697970/715269 - so old and upvoted answer, that it surely looks as Santa.

The problem remained unchanged.

I output the maven map. It was correct and it contained all needed.

I looked at the list of the External Libraries of the project. It was the old uncorrected list of overlapping jars with same names and different versions, and without good packages I added just now, and well seen in maven tree output!

Already hapless,

I reimported packages in IntelliJ

by:
Ctrl+Shift+A, Reimport All Maven Projects.

Ho! The list of libraries got repaired. And the problem, mentioned in subj, disappeared.

The question is: How it could happen, that the same project has that very pom for everything, but gets packages differently being launched in maven and in IntelliJ?

I know about that feature "delegate IDE build to Maven". And I keep it turned off. But I am NOT talking about the different SW for building. Whether they are different or not, they should be up to the actual pom's. And whereas maven, if turned off from the automatic building won't know about changes in poms, IntelliJ KNOWS about them. It could have jars up to pom, or up to maven - it has sense, but it simply has some old rubbish. Was there some deep thought under that construction?

like image 376
Gangnus Avatar asked Dec 10 '25 11:12

Gangnus


1 Answers

Intellij doesn't use maven to bulid and run a project except you are delegating build and run action to maven: enter image description here

Since, IDEA doen't really use maven to run and build, it uses the pom.xml to import the project structure and "tries" to build the project the same way was maven does.

Actually, there are quite a few differences between these to build processes. Generating sources or filtering resources (don't know if this is still an issue) aren't done during building the project with Intellij IDEA.

In case you are using code generation you have to build the project via maven first and then - when all the resouces are filtered and additional sources are generated - you are able to run, debug aso. the project with Inellij IDEA.

That's an important thing to be aware of and that's the reason why maven and IntelliJ IDEA project structures might get out of sync.

You can enable the "Reload project after changes in build scripts" feature and select the Any changes checkbox to keep your project structure updated: enter image description here

Why should you disable this feature anyway

If you are working on a build file (gradle or maven is not important) reloading the structure on any change can be very anoying. It's cpu intense, dependcies are fetched aso.

Therefore, I prefer to reload project structure only in case of an external change. This happens when pulling an updated version of the build file for example.

like image 174
Peter Avatar answered Dec 12 '25 01:12

Peter