How do I remove unused dependencies from the project?
I have an Idea project which is based on Maven Project. Maven project has some dependencies which in turn have versions. The verions may change.
I.e. yesterday my project has dependency named "common.artefacts" with version 10.2, and now it has version 10.3.
The questions is: how can I removed the unused now libraries from my project? By project I mean the Idea project and specially the External Libraries list on the left side of my screen. Its not about Maven dependencies because they are updated by Maven.
This problem emerges when I run debug - the old classes are used by debugger: i.e. I have 10.3 version now, but the debugger finds 10.2 version's class because that class is stil in External Libraries list of my Idea Project. I tried to force debugger searching in some folder, but it still runs through the class with wrong version.
Looks like I need to remove some old dependencies from my Idea project. How can I do it? Can Idea make it automatically after Maven POM changes?
Please sign in to leave a comment.
To make it more clear:
I want to find out Idea's behaviour in 2 use-cases:
1) Situation: One of the modules of the underlying Maven project is removed. Action: The module artifact and its dependencies should be removed from Idea project.
2) Situation: The version of one of Maven modules changes. Action: The STALE module artifact and its dependencies (if become stale) should be removed from Idea project.
As far as I understand Idea should automatically perform the listed actions in case on of the listed situations occur.
Am I right?
If this has not happened for some reason (error or user's action), how can I force running corresponding process of optimizing External Libraries in Idea?
Press "Reimport All Maven projects" on Maven Tool Window. See screenshot.
Attachment(s):
a.png
I assume you want to do this. For eg, If you want to delete the downloaded files of "Maven: com.google.code.gson:gson:2.1", because you want to use the 2.6 version, then navigate to ~/.m2/repository/com/google/code/ and then delete the gson folder and then as @Sergey has mentioned, Press the Reimport All Maven Projects.
@mypeople, what if my project contains a tens modules and hundreds dependencies in total?
Workaround what I have is to follow to ${PROJECT_DIR}/.idea/libraries directory and manually remove all files (just Ctrl+A/Command+A). After that when I open project, IDEA re-resolve all dependencies with appropriate version. But I think that it is unlegal approach :)
Please help who knows.
@Alexander O. do you use Maven/Gradle? See the answer above.
@Andrey Dernov, yes I using Maven and Reimport All Maven projects doesn't help, unfortunately.
This is strange. Re-import should update libraries according to your pom.xml files. What expected/actual result do you see? Can you describe the exact example of what libraries are not updated(removed)?
@Andrey Dernov, I tried to reproduce my problem on some small project (with less than 10 numbers of modules) but unsuccessfully. I reproduced the issue on a big project where number of modules more than 50.
So, what I did?
I updated my working directory in mercurial local repository to the latest revision and that open project in IDEA. When IDEA finish opening I reviewed all dependencies and all was good. For example I see that I have only one version of commons-codec artifact - 1.10 in my case. I see one version in:
After that I updated to some of old version. IDEA reload project, during reloading I confirmed that I'm ok to remove some maven projects.
When IDEA completed reloading I perform Reimport All Maven projects manually one again and than I found that a few libraries was included with wrong versions. It was
Maybe I do something wrong?
>When IDEA completed reloading I perform Reimport All Maven projects manually one again and than I found that a few libraries was included with wrong versions.
Could you please provide the module's .iml file where "httpcomponents" library is used and several last idea.log files containing the period when issue happened? Also could you attach screenshot of this dependency in Maven projects tool window? Thank you.
Please also check if it helps to call File | "Synchronize" and "Save All" actions before re-importing from Maven. Thanks.
Reimporting doesn't work. It appears as if you use the wrong version or want to change the version, you're also out of luck. Those dependencies are immutable. You can add dependencies, but can't delete, nor alter existing dependencies.
Edited: My error. Re-importing does work, but from IntelliJ, one has to expand the External Library entries in question, then delete the associated jar file.
Upon re-build, Maven will pull in the version and repopulate the jar file in the entry.
Example in IntelliJ external library:
Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.30. Expand that entry to get: tomcat-embed-core-9.0.30.jar. Delete the jar file (which also deletes the Mavem: org... entry too.
Upon re-compile, but for some reason, not Maven -> reimport, the build process will pull in Maven: org.apache.tomcat.embed:tomcat-embed-core:9.0.30 and its associated jar.
@craig linton can you add more information about how you are actually seeing the problem?
You can see the current dependencies for corresponding module in the module's Dependencies tab. What do you see there? Are those dependencies from Maven central? What are their versions? It would also be useful to check the idea.log file. Thank you.
For me the reason of such problem was that some other libraries still had dependencies to old version of deleted from pom library. It is sad, but I had to search by artifactId in all used libraries to figure out, what dependencies have to be removed/replace as well. In Ubuntu gnome-search-tool helped me a lot. I wonder whether Intelij idea can analyze inner dependencies of project dependencies?
@Tetiana Kysla can you clarify what problem did you observe?
IDE for example have dependencies diagram representation of dependencies in Maven - where you can use Ctrl(Cmd)+F - find action to search for a specific dependency.
You can also explore the transitive dependencies (that direct dependencies provide to project), please see Work with Maven transitive dependencies.
I've faced the same issue as Alexander O.
All was fine with a project on Idea 2020.1.4. Idea correctly picked any changes in pom.xml. Once I've updated to the latest version 2021.3.3, I've started to experience this behavior. Any changes to the dependency version inside pom.xml started to produce duplicate extra dependency (they where always added, and the previous one never replaced). One thing that I'm noticed that pom.xml files on new Idea started to fail validation (Reporting configuration should be done in <reporting> section, not in maven-site-plugin <configuration>), and projects in maven toolbox were underlined with a red line. I've manually resolved all validation but error marks in the tollbox section stayed the same. I also tried to disable any pom.xml validation, this dint resolved the issue. So I decided to try a different version 2020.2.4, experienced the same behavior.
Once I've downgraded back to 2020.1.4, all was fine. I hope this will help to investigate the reason
@... Seems that Maven project is not reimported after changing the pom.xml. Does it help to reimport it manually?
If it helps, you can configure IDE to re-import the project automatically on build script changes: https://i.imgur.com/8urcNUX.png
I'm facing the same problem as above.
I created for this another post:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010618919-IntelliJ-Idea-not-removing-external-libraries