How do I remove unused dependencies from the project?

Answered

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?

9 comments

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?

0

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



Attachment(s):
a.png
2

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.

1

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

0

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

0

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

0

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

0

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

0

Please sign in to leave a comment.