public void sign(Builder builder, String alias) throws Exception { File f = builder.getFile(keystore); if (!f.isFile()) { builder.error("Invalid keystore %s", f.getAbsolutePath()); return; } Jar jar = builder.getJar(); File tmp = File.createTempFile("signdjar", ".jar"); tmp.deleteOnExit(); jar.write(tmp); Command command = new Command(); command.add(path); if (keystore != null) { command.add("-keystore"); command.add(f.getAbsolutePath()); } if (storetype != null) { command.add("-storetype"); command.add(storetype); } if (keypass != null) { command.add("-keypass"); command.add(keypass); } if (storepass != null) { command.add("-storepass"); command.add(storepass); } if (sigFile != null) { command.add("-sigFile"); command.add(sigFile); } if (digestalg != null) { command.add("-digestalg"); command.add(digestalg); } command.add(tmp.getAbsolutePath()); command.add(alias); logger.debug("Jarsigner command: {}", command); command.setTimeout(20, TimeUnit.SECONDS); StringBuilder out = new StringBuilder(); StringBuilder err = new StringBuilder(); int exitValue = command.execute(out, err); if (exitValue != 0) { builder.error("Signing Jar out: %s%nerr: %s", out, err); } else { logger.debug("Signing Jar out: {}\nerr: {}", out, err); } Jar signed = new Jar(tmp); builder.addClose(signed); Map<String, Resource> dir = signed.getDirectories().get("META-INF"); for (Entry<String, Resource> entry : dir.entrySet()) { String path = entry.getKey(); if (path.matches(".*\\.(DSA|RSA|SF|MF)$")) { jar.putResource(path, entry.getValue()); } } jar.setDoNotTouchManifest(); }