Hey I have created a Groovy script that will extract the version numbers of some folder. I would then like to compare the version numbers and select the highest.
I got my script to run through the dir folder and I then get the versions in this format: 02.2.02.01
So I could get something like this:
- 02.2.02.01
- 02.2.02.02
- 02.2.03.01
I don't have them as a list but like this:
baseDir.listFiles().each { file ->
def string = file.getName().substring(5, 15)
// do stuff
}
Also I have tested that Groovy could compare them with the >
operator and it can! But now I need to select the one with the highest version
I use gradle 4.1 in Android Studio 3.0 Beta 7. There is VersionNumber.java (under C:\Users\ssfang.gradle\wrapper\dists\gradle-4.1-all\bzyivzo6n839fup2jbap0tjew\gradle-4.1\src\core\org\gradle\util)
For example:
--
Here is a modification of Tim's answer which takes two version strings and returns a boolean (true if the first is newer than the second)
The code I am using with Jenkins ExtendedChoiceParameter (tolerant to non-integer fragments in the version string)
Mine is the shortest! lol )
If you just need to implement
Comparable
orComparator
interface here's the shortest solution I came up with based on the other answers: