Maven the Version Number Nazi
Join the DZone community and get the full member experience.Join For Free
maven doesn’t like it when you use different verison numbers to the maven standard format. of course it doesn’t. it wouldn’t would it? it’s maven, and maven only likes it when you do what it tells you to do. i’m still a bit annoyed with maven, as you can probably tell.
don’t get me wrong, i’m not “maven bashing”, it’s just that this particular problem doesn’t have quite the elegant solution i was looking for. i do appreciate maven, honestly.
this was the problem:
i wanted to change our versioning system from something like 1.0.0-1234 to something like 1.0.0-1234-01
why the hell would i want to do that?? i’ll explain…
our verisoning is like this:
the only problem was, the build number was taken from the perforce check-in number, and this number didn’t always change whenever a build was made, especially if the build was kicked off by an upstream dependency, or a forced build was triggered. basically, if the build was kicked off by anything other than a commit to perforce, the build would create an artifact of identical version to the previous build. this, in theory, shouldn’t be a problem, because it is actually building exactly the same thing, but i just don’t like it. anything could happen, any environmental change could produce a slightly different build to the previous one.
the problem was that i wasn’t using an incremental counter anywhere in my version numbe. it’s essential to have an incrementing version number in order to ensure that every single build creates a unique identifier, so that no two different builds can appear to be the same build.
my first thought was to append a build counter on the end, like this:
and that would have worked fine, if it wasn’t for the fact that we use version ranges in our dependencies, and we already have plenty of builds which use the previous versioning system. maven kept picking up the builds with the previous version system, even though, in every possible sense, the new ones had higher version numbers. it made no sense. that’s when i looked into how maven works out versions. basically it says “if you’re using version ranges, and not using the maven standard versioning format, you might as well forget it”. if it sees dependencies using the standard format, and ones using the non-standard maven format, it’ll pick up the standard format ones and basically ignore the new ones. to get around this you can delete all the old builds using the standard maven format, and then it’ll work, because it’ll treat each build version like a string and just get you the latest in whatever your range is.
sadly, this isn’t an option for me, as i want to kep the old builds
using the old format. so i tried a few things. i tried putting a string
in as a separator, so it would look like this:
this effectively produces something looking like:
i’m fine with that. maven, on the other hand, isn’t. i made a build with this version 1.0.0-9999rc01 and used it as a dependency in another build, but the other build still went and got 1.0.0-1234, the old build using the standard maven versioning. i mean, you’d think 1.0.0-9999rc01 > 1.0.0-1234 but apparently not.
i was a bit pushed for time so i couldn’t spend forever looking into this, so i’ve basically just appended the build counter directly onto the end of the perforce number. this works ok, but just looks a little ugly.
there’s more information on the maven versioning rules here . it seems that you can break the rules no problems, but you’re in trouble if you use version ranges in your dependencies, and your dependencies need to live alongside binaries which use the standard maven versioning system
if anyone has any better solutions i’d like to hear them. and please don’t say “stop using maven”.
Published at DZone with permission of James Betteley, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.