Skip to content

Commit

Permalink
Get version information from jar manifest
Browse files Browse the repository at this point in the history
See remkop/picocli#1663

Change-Id: I9c35e336dd06956967d3a07318c1f397b8518091
  • Loading branch information
kupietz committed Sep 8, 2023
1 parent d1dc848 commit f8b7d95
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>de.ids_mannheim.korap.tokenizer.Main</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
Expand Down Expand Up @@ -204,6 +205,7 @@
<archive>
<manifest>
<mainClass>de.ids_mannheim.korap.tokenizer.Main</mainClass>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
</manifest>
</archive>
</configuration>
Expand Down
34 changes: 33 additions & 1 deletion src/main/java/de/ids_mannheim/korap/tokenizer/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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> {

/**
Expand Down Expand Up @@ -271,4 +275,32 @@ public Integer call() throws FileNotFoundException {
}
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));
}
}
}

0 comments on commit f8b7d95

Please sign in to comment.