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?

Comment actions Permalink

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?

Comment actions Permalink

Press "Reimport All Maven projects" on Maven Tool Window. See screenshot.

Comment actions Permalink

I assume you want to do this. For eg, If you want to delete the downloaded files of  "Maven:", 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.

Comment actions Permalink

@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.

Comment actions Permalink

@Alexander O. do you use Maven/Gradle? See the answer above.

Comment actions Permalink

@Andrey Dernov, yes I using Maven and Reimport All Maven projects doesn't help, unfortunately.

Comment actions Permalink

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)?

Comment actions Permalink

@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:

  • project tree
  • ${PROJECT_DIR}/.idea/libraries directory
  • effective pom
  • report created using dependency:tree

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

  • commons-codec
  • gson
  • httpcomponents
  • and sometimes our internal libraries (which are developed in company where I work)

So, I expected that list of libraries should be the same as defined in pom.xml files but in fact sometimes I see different situation.

Maybe I do something wrong?

Comment actions Permalink

>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.

Comment actions Permalink

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.

Comment actions Permalink

@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.


Please sign in to leave a comment.