Summary: I have multiple versions of several different libraries in my .m2 repository. My maven pom specifies to use a particular version of each dependency. My project builds properly from the command line (using mvn clean install). However, when I attempt to build or edit code within IntelliJ IDEA, my IDE defaults to use the oldest version of each dependency, instead of the versions for the dependencies which have been specified in the pom.
For example: in my .m2 directory I might have the following versions for dependency X: 2.1, 2.2, 2.3, 2.4. My pom specifies dependency version 2.3. However, the IntelliJ IDE defaults to using version 2.1. If I delete version 2.1 from my .m2 directory, the IntelliJ IDE will default to using version 2.2.
Deleting versions from my .m2 directory is not a solution in practice. For example, there is one project I work on daily which uses 2 different versions of the same dependency in different modules. Module A uses version 2.2, while module B uses version 2.3. In this case, I would expect IntelliJ to use version 2.2 while I edit/compile files in module A, and version 2.3 while I edit/compile files in module B. The actual behavior is that IntelliJ uses version 2.1 for both modules, which results in errors.
Temporary Manual Solution: The way that one can solve this problem is by going into File -> Project Structure -> Dependencies and manually removing the unwanted versions of the dependencies (by highlighting them and clicking the "-" button, and then clicking "apply").
However, this is only a temporary solution. Every time I import a new project, open an existing project, or even just switch git branches, all of the manual work I have done to get the correct versions of each dependency (by following the method above) is undone. Manually fixing this every time is untenable as I work with multiple git branches and also work on multiple different services, each of which use different versions of the library dependencies. Some dependencies which are regularly changed and updated produce 30+ jars. Manually removing all of these jars every time should not be the solution to this problem.
Re-importing the project and invalidating the cache are fixes that I have tried but which do not work.
This is a problem which has also been documented before (although not brought to a resolution) here
Expectation: IntelliJ should always resolve its dependencies deterministically based on what has been exactly specified in the pom file. If I change the pom file, then I would expect IntelliJ to use the newly specified version by default. As a user of IntelliJ, I expect this kind of dependency resolution to work seamlessly with little to no additional manual work, just as it does for build tools such as maven and gradle.
Build versions: I am using IntelliJ IDEA CE 2020.2 on MacOS Catalina 10.15.7