Maven version ordering gotchas

2017-03-04 21:36:18.061

Version ordering is problematic (at least if you want some artifact's latest version). Usually you would think that versions are released in this order: 1.0 -> 1.1 -> 2.0 -> 2.1 and that's true for many artifacts.

But there are also those artifacts where the author supports multiple major versions simultaneously and he can release versions for example in this order: 1.0 -> 2.0 -> 2.1 -> 1.1

This means that there's a difference between "latest" version and the last released version.

In that case you may say that ordering should be done using more intelligent algorithm which sorts it first by major version, then minor version etc. Well, this would work for our current example but version is just a String. There can be ANYTHING. For example commons-io's first version is 20030203.000550. And few days after that they started with more sane versioning 0.1 -> 1.0 -> 1.1 etc. Unfortunatelly in version 20030203.000550 the major version is: 20030203 which beats the current latest 2 and thus using this versioning the 20030203.000550 version is latest.

Also I was very unhappy when Spring changed versioning for their io.spring.platform:platform-bom where they used format "Major.Minor.Inc.RELEASE" and changed it to text like Athens-RELEASE.

How does various search engines deal with this situation? tries to group versions using their own algorithm and mostly it's just fine, but sometimes it gives weird results like for example io.spring.platform:platform-bom where ordering is currently: Athens-SR3 -> 2.0.8 -> ... -> 2.0.0 -> Athens-SR2 -> 1.1.5 ... 1.0.0 -> Athens-SR1 -> 1.0.0.RC1 -> Brussels-RC1 (in fact this one is currently latest) -> ... but instead it should have been: Brussels-RC1 -> Athens-SR3 -> ... -> Athens-SR1 -> 2.0.8 ... doesn't care at all, latest version is 2.0.8 and when you select the artifact, you will notice that ordering is by time. Which for this artifact is just fine, but for others releasing bugfix versions for older major versions it's just a mess.

Eclipse IDE doesn't care at all, they have ordering only based on major-minor-etc. versions. And IntelliJ Idea has most weird ordering I have ever seen. Both examples are for commons-io:commons-io:

Maven ordering Eclipse
Maven ordering IntelliJ Idea

I hope that you have learned that ordering isn't trivial, so you must think before selecting some particular version. Also sometimes somebody wants to get the "LATEST" version. Currently it's possible even though it's a deprecated feature. You really should never ever use it, not only because LATEST doesn't really have to be "latest", but also because it leads to unrepeatable builds. And version ranges are bad as well for the same reasons. As I always say - just because something exists doesn't mean that it's a good idea to use it.

© Jiri Pinkas 2015 - 2022. All rights reserved! Admin login To submit bugs / feature requests please use this github page
related: JavaVids | Top Java Blogs | Java školení
Apache and Apache Maven are trademarks of the Apache Software Foundation. The Central Repository is a service mark of Sonatype, Inc.