Get version information from jar manifest

See https://github.com/remkop/picocli/issues/1663

Change-Id: I9c35e336dd06956967d3a07318c1f397b8518091
diff --git a/pom.xml b/pom.xml
index 256bef1..21fae85 100644
--- a/pom.xml
+++ b/pom.xml
@@ -173,6 +173,7 @@
                             <addClasspath>true</addClasspath>
                             <classpathPrefix>lib/</classpathPrefix>
                             <mainClass>de.ids_mannheim.korap.tokenizer.Main</mainClass>
+                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                         </manifest>
                     </archive>
                 </configuration>
@@ -204,6 +205,7 @@
                     <archive>
                         <manifest>
                             <mainClass>de.ids_mannheim.korap.tokenizer.Main</mainClass>
+                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                         </manifest>
                     </archive>
                 </configuration>
diff --git a/src/main/java/de/ids_mannheim/korap/tokenizer/Main.java b/src/main/java/de/ids_mannheim/korap/tokenizer/Main.java
index 7115928..ca298ab 100644
--- a/src/main/java/de/ids_mannheim/korap/tokenizer/Main.java
+++ b/src/main/java/de/ids_mannheim/korap/tokenizer/Main.java
@@ -4,13 +4,17 @@
 import picocli.CommandLine;
 
 import java.io.*;
+import java.net.URL;
 import java.nio.charset.Charset;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Enumeration;
 import java.util.List;
 import java.util.concurrent.Callable;
+import java.util.jar.Attributes;
+import java.util.jar.Manifest;
 import java.util.stream.Collectors;
 
 /**
@@ -20,7 +24,7 @@
  * @version $Id: $Id
  */
 @CommandLine.Command(mixinStandardHelpOptions = true,
-        name = "koraptokenizer", version = "2.2.3", description = "Tokenizes (and sentence splits) text input.")
+        name = "koraptokenizer", versionProvider = Main.ManifestVersionProvider.class, description = "Tokenizes (and sentence splits) text input.")
 public class Main implements Callable<Integer> {
 
     /**
@@ -271,4 +275,32 @@
         }
         return 0;
     }
+
+    static class ManifestVersionProvider implements CommandLine.IVersionProvider {
+        public String[] getVersion() throws Exception {
+            Enumeration<URL> resources = CommandLine.class.getClassLoader().getResources("META-INF/MANIFEST.MF");
+            while (resources.hasMoreElements()) {
+                URL url = resources.nextElement();
+                try {
+                    Manifest manifest = new Manifest(url.openStream());
+                    if (isApplicableManifest(manifest)) {
+                        Attributes attr = manifest.getMainAttributes();
+                        return new String[] {(String) get(attr, "Implementation-Version")};
+                    }
+                } catch (IOException ex) {
+                    return new String[] { "Unable to read from " + url + ": " + ex };
+                }
+            }
+            return new String[0];
+        }
+
+        private boolean isApplicableManifest(Manifest manifest) {
+            Attributes attributes = manifest.getMainAttributes();
+            return "KorAP-Tokenizer".equals(get(attributes, "Implementation-Title"));
+        }
+
+        private static Object get(Attributes attributes, String key) {
+            return attributes.get(new Attributes.Name(key));
+        }
+    }
 }