Issue
I'm using Apache Maven to release our product to Production, and as of right now, the resultant jar file that is created does not have a version number appended to it. I would like to append the current pom.xml version number (which I know how to do), but I need our installer script (basically a java -jar command with extra parameters) to access the newest version of the installer by default.
Say my deployment file is called foo.jar
. My script is basically java -jar foo.jar
Instead, I'd like my deployment directory to contain foo-4.0.0.jar
, foo-4.0.1.jar
, foo-4.0.2.jar
, etc.
I would like to create a bash script that runs the highest version of foo.
I've looked at sorting by creation date, but I can immediately think of scenarios that might not work out very well. I've considered doing it by date modified, and then manually touch
ing the file that I want to install, but that idea kind of sucks too.
Clearly I need to split the name (probably with a regex) and isolate the version numbers (maybe substring from the last dash and the last period in the name?) and somehow sort them. The sorting is probably my biggest concern...that and reassociating the file.
If this is too complicated, I could theoretically make a Java file that does this (has it become clear that my primary language is Java?), but ideally it would just be a bash script.
Thanks
Solution
You can use sort, specifying the separator and set of keys to sort on e.g.
$ ls *.jar | sort -t- -k2 -V -r
will sort your jars in reverse (-r) using a version number (-V) sort, separating the version number from the jar name using -t-, and sorting on the second field using -k2 (your version number)
In my test directory I get:
a-2.2
a-2.1
a-2.0
a-1.50
a-1.10
a-1.3.1
c-1.3.0.1
a-1.3
a-1.2.9
b-1.2.8
a-1.2
which looks good.
Pipe the above through head -1
to give you the top entry.
Answered By - Brian Agnew
Answer Checked By - Timothy Miller (JavaFixing Admin)