in tooling/common/src/main/java/org/apache/karaf/minho/tooling/common/Runtime.java [77:168]
public void createJar() throws Exception {
log.info("Creating Minho runtime uber jar");
// exploded all jar
Path exploded = workingFolder.resolve("exploded");
Files.createDirectories(exploded);
String minhoLib = (properties != null && properties.get("minho.lib") != null) ? properties.get("minho.lib").toString() : "";
Path libFolder = baseFolder.resolve(minhoLib);
Files.list(libFolder).forEach(artifact -> {
if (artifact.toString().endsWith(".jar")) {
// test
try (JarFile jarFile = new JarFile(artifact.toFile())) {
for (Enumeration<JarEntry> j = jarFile.entries(); j.hasMoreElements(); ) {
JarEntry entry = j.nextElement();
String name = entry.getName();
if ("META-INF/INDEX.LIST".equals(name)) {
continue;
}
if ("module-info.class".equals(name)) {
continue;
}
if (entry.isDirectory()) {
Files.createDirectories(exploded.resolve(name));
} else {
if (name.contains("META-INF/services")) {
log.info("services " + name);
Path service = exploded.resolve(name);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(service.toFile(), true))) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(jarFile.getInputStream(entry)))) {
String line = null;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("#")) {
writer.write(line + "\n");
}
}
}
}
} else {
InputStream is = null;
try {
is = jarFile.getInputStream(entry);
try (FileOutputStream os = new FileOutputStream(new File(exploded.toFile(), name))) {
byte[] buffer = new byte[1024];
int readCount = 0;
while ((readCount = is.read(buffer)) >= 0) {
os.write(buffer, 0, readCount);
}
os.flush();
}
} catch (Exception e) {
log.warning("Can't extract " + entry + ": " + e.getMessage());
}
}
}
}
} catch (Exception e) {
log.warning("Can't extract jar " + artifact);
}
} else {
try {
Files.copy(artifact, exploded.resolve(artifact.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
log.warning("Can't copy " + artifact + ": " + e.getMessage());
}
}
});
// package all as a uber jar
Path uberJarPath = baseFolder.resolve(name + ".jar");
// add Main-Class in the manifest
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
if (properties == null || properties.get("MainClass.disabled") == null || !properties.get("MainClass.disabled").toString().equalsIgnoreCase("true")) {
String mainClass = (properties != null && properties.get("MainClass") != null) ? properties.get("MainClass").toString() : "org.apache.karaf.minho.boot.Main";
manifest.getMainAttributes().put(Attributes.Name.MAIN_CLASS, mainClass);
}
try (JarOutputStream uberJar = new JarOutputStream(new FileOutputStream(uberJarPath.toFile()), manifest)) {
Files.list(exploded).forEach(file -> {
log.info("Adding jar entry " + file);
try {
addJarContent(file, exploded.toString(), uberJar);
} catch (Exception e) {
log.warning("Can't add jar content: " + e.getMessage());
}
});
}
// cleanup
if (properties != null && properties.get("minho.cleanup") != null && properties.get("minho.cleanup").toString().equalsIgnoreCase("true")) {
Files.walk(libFolder).map(Path::toFile).forEach(File::delete);
Files.delete(libFolder);
}
}