80

Just wanted to know if it is possible in Eclipse to sort Maven dependencies by alphabetical order?

It's bothering me to have a list of 200 jars not ordered... :(

Sebastien Lorber
  • 89,644
  • 67
  • 288
  • 419

7 Answers7

46

Finally it is possible with Eclipse Photon now.

Just tick the checkbox Sort library entries alphabetically in Package Explorer under PreferencesJavaAppearance

See also here under 'Sort library entries alphabetically in Package Explorer'.

Unfortunately theres a bug, at least for me, that the source folders are also reordered, see here.

preferences

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
42

I guess you refer to the m2eclipse plugin. You have these options:

  1. The order in the navigator is the classpath order. You can sort the dependencies in the POM to get a better list. Of course, there is a problem with transient dependencies.

  2. You can open the POM file in the POM editor and examine the dependencies in the tab "Dependency Hierarchy"

  3. You can type part of the name of a dependency to locate it when the tree item "Maven Dependencies" is open.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • 14
    Depressing. This is a good answer (+1), but the options are very unsatisfactory. Perhaps someone should file a feature request for an alphabetic order option, but I wouldn't know if that should be targeted at eclipse or at m2eclipse. – Sean Patrick Floyd Sep 28 '10 at 14:15
  • 2
    Unfortunately, none of these options work: (1) does not fit with transient dependencies as you note, (2) you can see it there, but can't expand to see the packages/classes inside, (3) okay as long as you can EXACTLY type the artifact name without any prompt/feedback and without making any mistakes. For example, I was just looking for "selenium-remote-driver" in a list with half a dozen selenium JARs sprinkled throughout (all of which are prefixed with "selenium-") - to type "selenium-re" without making any mistakes and without seeing any feedback on-screen, is quite difficult. – Adam Burley Apr 30 '15 at 16:06
  • @Kidburla: Open "Maven Dependencies" in the package explorer. Type "sel" and then use Cursor-Up/Down to step through all instances which begin with these three letters. – Aaron Digulla Sep 10 '15 at 15:38
18

Maven's Pom viewer will do the trick:

Double-click the pom.xml you care about. Eclipse opens it in a special viewer with five tabs on the bottom: Overview, Dependencies, Dependency Hierarchy, Effective POM, and pom.xml.

Click Dependency Hierarchy. You should see the following on the right side of the view:

enter image description here

It's alphabetized.

Jake Toronto
  • 3,524
  • 2
  • 23
  • 27
  • Not alphabetized by default for me, had to click on the button "Sort" (on the top right corner in your screenshot) – George Jul 06 '19 at 09:08
15

I just ran into the Sortpom Maven Plugin. This enables you to sort the dependencies in your pom using a Maven command. So you can use it in Eclipse, but also in any other IDE (I'm using NetBeans where you can configure it as custom goal definition).

Simply run:

mvn com.github.ekryd.sortpom:sortpom-maven-plugin:sort -Dsort.sortDependencies=groupId,artifactId

At the GitHub wiki you can find all parameters.

But as pointed out in the comments, sorting like this isn't a good idea. Let your IDE take care of it. NetBeans has a "Dependencies" folder in a project where the dependencies are shown sorted by default (first by type, then by name).

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
  • 5
    IMO this is generally a bad idea, as Maven (as of version 2.0.9) [uses deterministic classpath ordering based on order specified in POM](http://stackoverflow.com/questions/793054/maven-classpath-order-issues). Sorting them around can easily mess up your build. – eis Aug 19 '14 at 07:00
  • @eis - FYi, the sortpom plugin does not sort modules or dependencies by default (because of that reason). It can be done if configured, and the documentation is clear about the warnings. – luis.espinal Mar 25 '19 at 14:36
2

It worked for me when I used the Java perspective instead of the Java EE perspective. On my computer, the dependencies were not sorted in the Java EE perspective, also not when I checked the checkbox in Java | Appearance. I had to choose the Java perspective instead. Then the checkbox did work and I could have sorted dependencies.

Martijn Dirkse
  • 543
  • 7
  • 16
1

have a look at the project properties in the 'java build path' section under the 'libraries' tab.

i don't know for sure if this works for the m2eclipse plugin.

Salandur
  • 6,409
  • 2
  • 22
  • 23
  • That actually IS sorted, and is a better solution than trying to manually scan the list to see whether your item is in there. – Jake Toronto Oct 30 '15 at 19:20
1

I had an issue where Sort library entries alphabetically in Package Explorer under PreferencesJavaAppearance was checked, but the dependencies were still unsorted.

Right clicking the library path and clicking Show in -> System Explorer fixed the issue for me.

enter image description here

Gamebuster19901
  • 132
  • 1
  • 8