'How to properly remove a dependency in a Maven project

I have a Java Maven project where I have some dependencies defined in the pom.xml file. Recently I decided to move from Junit to TestNG so I deleted the Junit dependency from my pom.xml and added the TestNG one.

I was expecting to see the Junit jar library disappear from the Maven Dependencies folder as a part of the process but that didn't happen. I can still see the jar file in the dependencies folder and it is still being used by my test cases.

I can see TestNG jar is there as well but it's not being used. I can change it manually of course but that wasn't my intention.

Am I doing something wrong are there any additional steps that I missed that will allow me to remove the old dependency?



Solution 1:[1]

The following applies for the global .m2 folder where maven dependencies can be downloaded and is common to your maven projects.

the .m2 folder will not clean by itself as those dependencies could be used in another project so its wrong assumption that replacing a dependency in a a pom file will automatically remove from your repo.

you can look at the depedency plugin and run the following

mvn dependency:purge-local-repository

or

mvn dependency:purge-local-repository -DreResolve=false

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1