Skip to content

Instantly share code, notes, and snippets.

@basinilya
Created February 8, 2020 10:48
Show Gist options
  • Save basinilya/15db9267ec753941d098cfd2f7ff844d to your computer and use it in GitHub Desktop.
Save basinilya/15db9267ec753941d098cfd2f7ff844d to your computer and use it in GitHub Desktop.
Generate jboss-cli "module add" command for a jar with recursive Class-Path dependencies
package org.foo.modulegen;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* Generate jboss-cli "module add" command for a jar with recursive Class-Path dependencies.<br/>
* <p>
* Command line arguments: main-jar-path...
* </p>
* <p>
* Output: --absolute-resources="C:\some\main-jar.jar;C:\some\jar2.jar;..."
* </p>
*/
public class Modulegen {
static HashSet<URL> urlsSet;
static MessageFormat fmt = new MessageFormat("\"{0}\" ");
static String sep;
public static void main(final String[] args) throws Exception {
final URL[] urls = new URL[args.length];
for (int i = 0; i < args.length; i++) {
final String s = args[i];
URL u;
try {
u = new URL(s);
} catch (final MalformedURLException e) {
u = new File(s).toURI().toURL();
}
urls[i] = u;
}
urlsSet = new HashSet<>();
sep = "";
System.out.print("--absolute-resources=\"");
for (final URL url : urls) {
ccc(url);
}
System.out.print("\"");
}
private static void ccc(final URL url) throws IOException, URISyntaxException {
if (urlsSet.add(url)) {
bbb(url);
}
}
private static void bbb(final URL url) throws IOException, URISyntaxException {
try (InputStream in = tryOpen(url)) {
if (in != null) {
final File f = new File(url.toURI());
System.out.print(sep);
sep = File.pathSeparator;
System.out.print(f);
aaa(url, in);
}
}
}
private static InputStream tryOpen(final URL url) {
try {
return url.openStream();
} catch (final IOException e) {
return null;
}
}
private static void aaa(final URL url, final InputStream in)
throws IOException,
URISyntaxException {
final ZipInputStream zin = new ZipInputStream(new BufferedInputStream(in));
ZipEntry ze;
L1: while (null != (ze = zin.getNextEntry())) {
if ("META-INF/MANIFEST.MF".equals(ze.getName())) {
final Manifest manifest = new Manifest(zin);
final Attributes attrs = manifest.getMainAttributes();
final String classPath = attrs.getValue("Class-Path");
if (classPath != null) {
final URI uri = url.toURI();
final String[] neighbours = classPath.split(" ");
for (final String neighbour : neighbours) {
final URI neighbourUri = uri.resolve(neighbour);
final URL neighbourUrl = neighbourUri.toURL();
ccc(neighbourUrl);
}
}
break L1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment