2

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?

Gangnus
  • 24,044
  • 16
  • 90
  • 149

2 Answers2

0

Every time you manually change the pom.xml file, including the dependencies you need to load these changes into IDE. IDE does it on Reload from Maven action. See also Import Maven dependencies.

Andrey
  • 15,144
  • 25
  • 91
  • 187
0

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.

Peter
  • 4,752
  • 2
  • 20
  • 32
  • Thank you, I know about that feature "delegate IDE build to Maven". And I keep it turned off. But I am NOT talking about the different building SW. 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. – Gangnus Nov 12 '20 at 21:22
  • Sorry for not being clear. I wanted to motivate the cause for the build/classpath drift between maven pom descriptior and the IDEA classpath/build process. But yes, I agree. This is one of the things I don't like about IDEA. No tool is perfect. – Peter Nov 13 '20 at 06:48
  • So, you think, that separate external libraries' repo of the IntelliJ is not a wise and necessary for some good, unknown for me, reason, but it simply was organized in some far from being the best way and exists as it is? – Gangnus Nov 13 '20 at 10:26