Skip to content

Instantly share code, notes, and snippets.

@kylestev
Last active May 3, 2016 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylestev/17e9e58513ef53bb2c59923e61a17f62 to your computer and use it in GitHub Desktop.
Save kylestev/17e9e58513ef53bb2c59923e61a17f62 to your computer and use it in GitHub Desktop.
Used for grabbing a value associated with a key from a manifest in your classpath.
import java.io.IOException;
import java.net.URL;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.jar.Manifest;
import java.util.stream.Stream;
/**
* Created by kylestev
*/
public class Manifests {
private static final String MANIFEST_FILE = "META-INF/MANIFEST.MF";
private static List<URL> manifestUrls() {
try {
final ClassLoader current = Thread.currentThread().getContextClassLoader();
return Collections.list(current.getResources(MANIFEST_FILE));
} catch (IOException e) {
return Collections.emptyList();
}
}
private static Manifest loadManifest(final URL url) {
try {
return new Manifest(url.openStream());
} catch (IOException e) {
return null;
}
}
private static Stream<Manifest> load() {
return manifestUrls()
.stream()
.map(Manifests::loadManifest)
.filter(m -> m != null);
}
public static Optional<String> get(final String key) {
return load()
.map(m -> m.getMainAttributes().getValue(key))
.filter(v -> v != null)
.findFirst();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment