diff --git a/executable-jar/build.gradle b/executable-jar/build.gradle new file mode 100644 index 0000000..f814a70 --- /dev/null +++ b/executable-jar/build.gradle @@ -0,0 +1,46 @@ +plugins { + id 'maven-publish' + id 'java-gradle-plugin' +} + +version = "0.1" + +configurations { + embedded + compileOnly.extendsFrom(embedded) +} + +dependencies { + embedded project(path: "executable-jar-common", configuration: "archives") +} + +Provider copyLauncher = tasks.register("copyLauncher", Copy) { + from(project("executable-jar-launcher").tasks.named("tar").map {it.outputs }) + into(new File(project.buildDir, "resources/main/META-INF")) +} + +tasks.named("processResources") { + inputs.files(copyLauncher) +} + +jar { + manifest { + attributes "version" : archiveVersion.get() + } + from { + configurations.named('embedded').map { + it.collect { + it.isDirectory() ? it : zipTree(it) + } + } + } +} + +gradlePlugin { + plugins { + create("JlinkPlugin") { + id = "net.woggioni.gradle.executable.jar" + implementationClass = "net.woggioni.gradle.executable.jar.ExecutableJarPlugin" + } + } +} diff --git a/executable-jar/executable-jar-common/build.gradle b/executable-jar/executable-jar-common/build.gradle new file mode 100644 index 0000000..620edb7 --- /dev/null +++ b/executable-jar/executable-jar-common/build.gradle @@ -0,0 +1,3 @@ +plugins { + id 'java-library' +} \ No newline at end of file diff --git a/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Common.java b/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Common.java new file mode 100644 index 0000000..e72ad8f --- /dev/null +++ b/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Common.java @@ -0,0 +1,117 @@ +package net.woggioni.executable.jar; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.InputStream; +import java.io.OutputStream; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.util.AbstractMap; +import java.util.Map; +import java.util.Optional; +import java.util.function.Supplier; +import java.util.zip.CRC32; +import java.util.zip.ZipEntry; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; +import java.io.IOException; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class Common { + final private static char[] hexArray = "0123456789ABCDEF".toCharArray(); + + public static String bytesToHex(byte[] bytes) { + char[] hexChars = new char[bytes.length * 2]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = hexArray[v >>> 4]; + hexChars[j * 2 + 1] = hexArray[v & 0x0F]; + } + return new String(hexChars); + } + + @SneakyThrows + public static byte[] computeSHA256Digest(Supplier streamSupplier) { + byte[] buffer = new byte[Constants.BUFFER_SIZE]; + MessageDigest md = MessageDigest.getInstance("SHA-256"); + return computeDigest(streamSupplier, md, buffer); + } + + @SneakyThrows + public static byte[] computeDigest(Supplier streamSupplier, MessageDigest md, byte[] buffer) { + try(InputStream stream = new DigestInputStream(streamSupplier.get(), md)) { + while(stream.read(buffer) >= 0) {} + } + return md.digest(); + } + + @SneakyThrows + public static void computeSizeAndCrc32( + ZipEntry zipEntry, + InputStream inputStream, + byte[] buffer) { + CRC32 crc32 = new CRC32(); + long sz = 0L; + while (true) { + int read = inputStream.read(buffer); + if (read < 0) break; + sz += read; + crc32.update(buffer, 0, read); + } + zipEntry.setSize(sz); + zipEntry.setCompressedSize(sz); + zipEntry.setCrc(crc32.getValue()); + } + + @SneakyThrows + public static void write2Stream(InputStream inputStream, OutputStream os, + byte[] buffer) { + while (true) { + int read = inputStream.read(buffer); + if (read < 0) break; + os.write(buffer, 0, read); + } + } + + public static void write2Stream(InputStream inputStream, OutputStream os) { + write2Stream(inputStream, os, new byte[Constants.BUFFER_SIZE]); + } + + public static Optional> splitExtension(String fileName) { + int index = fileName.lastIndexOf('.'); + if (index == -1) { + return Optional.empty(); + } else { + return Optional.of( + new AbstractMap.SimpleEntry<>(fileName.substring(0, index), fileName.substring(index))); + } + } + + /** + * Helper method to create an {@link InputStream} from a file without having to catch the possibly + * thrown {@link IOException}, use {@link FileInputStream#FileInputStream(File)} if you need to catch it. + * @param file the {@link File} to be opened + * @return an open {@link InputStream} instance reading from the file + */ + @SneakyThrows + public static InputStream read(File file, boolean buffered) { + InputStream result = new FileInputStream(file); + return buffered ? new BufferedInputStream(result) : result; + } + + /** + * Helper method to create an {@link OutputStream} from a file without having to catch the possibly + * thrown {@link IOException}, use {@link FileOutputStream#FileOutputStream(File)} if you need to catch it. + * @param file the {@link File} to be opened + * @return an open {@link OutputStream} instance writing to the file + */ + @SneakyThrows + public static OutputStream write(File file, boolean buffered) { + OutputStream result = new FileOutputStream(file); + return buffered ? new BufferedOutputStream(result) : result; + } +} diff --git a/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Constants.java b/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Constants.java new file mode 100644 index 0000000..069a622 --- /dev/null +++ b/executable-jar/executable-jar-common/src/main/java/net/woggioni/executable/jar/Constants.java @@ -0,0 +1,29 @@ +package net.woggioni.executable.jar; + +import java.util.Calendar; +import java.util.GregorianCalendar; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class Constants { + public static final String LIBRARIES_FOLDER = "LIB-INF"; + public static final String METADATA_FOLDER = "META-INF"; + public static final int BUFFER_SIZE = 0x10000; + public static final String DEFAULT_LAUNCHER = "net.woggioni.executable.jar.Launcher"; + + public static class ManifestAttributes { + public static final String MAIN_MODULE = "Executable-Jar-Main-Module"; + public static final String MAIN_CLASS = "Executable-Jar-Main-Class"; + public static final String ENTRY_HASH = "SHA-256-Digest"; + } + + /** + * This value is used as a default file timestamp for all the zip entries when + * AbstractArchiveTask.isPreserveFileTimestamps + * is true; its value is taken from Gradle's ZipCopyAction + * for the reasons outlined there. + */ + public static final long ZIP_ENTRIES_DEFAULT_TIMESTAMP = + new GregorianCalendar(1980, Calendar.FEBRUARY, 1, 0, 0, 0).getTimeInMillis(); +} diff --git a/executable-jar/executable-jar-launcher/build.gradle b/executable-jar/executable-jar-launcher/build.gradle new file mode 100644 index 0000000..1ba6d80 --- /dev/null +++ b/executable-jar/executable-jar-launcher/build.gradle @@ -0,0 +1,47 @@ +import java.util.jar.Attributes +plugins { + id 'java-library' +} + +configurations { + embedded + compileOnly.extendsFrom(embedded) +} + +dependencies { + embedded project(path: ":executable-jar:executable-jar-common", configuration: 'archives') +} + +java { + modularity.inferModulePath = true +} + +jar { + manifest { + attributes([ + (Attributes.Name.SPECIFICATION_TITLE) : "executable-jar-launcher", + (Attributes.Name.SEALED) : true + ].collectEntries { + [it.key.toString(), it.value.toString()] + }) + } +} + +tasks.register("tar", Tar) { + archiveFileName = "${project.name}.tar" + from(sourceSets.main.output) + from { + configurations.named('embedded').map { + it.collect { + it.isDirectory() ? it : zipTree(it) + } + } + } +} + +tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) { + doFirst { + String path = project(":executable-jar:executable-jar-common").extensions.getByType(JavaPluginExtension).sourceSets.named("main").get().output.asPath + options.compilerArgs.addAll(["--patch-module", "net.woggioni.executable.jar=$path"]) + } +} \ No newline at end of file diff --git a/executable-jar/executable-jar-launcher/src/main/java/module-info.java b/executable-jar/executable-jar-launcher/src/main/java/module-info.java new file mode 100644 index 0000000..f0e2139 --- /dev/null +++ b/executable-jar/executable-jar-launcher/src/main/java/module-info.java @@ -0,0 +1,4 @@ +module net.woggioni.executable.jar { + requires java.logging; + requires static lombok; +} \ No newline at end of file diff --git a/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/Launcher.java b/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/Launcher.java new file mode 100644 index 0000000..400269e --- /dev/null +++ b/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/Launcher.java @@ -0,0 +1,109 @@ +package net.woggioni.executable.jar; + +import java.io.InputStream; +import java.lang.module.Configuration; +import java.lang.module.ModuleFinder; +import java.lang.reflect.Method; +import java.net.URI; +import java.net.URL; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.function.Function; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import lombok.SneakyThrows; +import lombok.extern.java.Log; + +@Log +public class Launcher { + + @SneakyThrows + private static URI findCurrentJar() { + String launcherClassName = Launcher.class.getName(); + URL url = Launcher.class.getClassLoader().getResource(launcherClassName.replace('.', '/') + ".class"); + if (url == null || !"jar".equals(url.getProtocol())) + throw new IllegalStateException(String.format("The class %s must be used inside a JAR file", launcherClassName)); + String path = url.getPath(); + return new URI(path.substring(0, path.indexOf('!'))); + } + + @SneakyThrows + public static void main(String[] args) { + URI currentJar = findCurrentJar(); + URL manifestResource = Launcher.class.getResource("/" + JarFile.MANIFEST_NAME); + if(Objects.isNull(manifestResource)) { + throw new RuntimeException("Launcher manifest not found"); + } + Manifest mf = new Manifest(); + try(InputStream is = manifestResource.openStream()) { + mf.read(is); + } + try(FileSystem fs = FileSystems.newFileSystem(Path.of(currentJar.getPath()), null)) { + Attributes mainAttributes = mf.getMainAttributes(); + + List jarList = StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function>() { + @Override + @SneakyThrows + public Stream apply(Path path) { + return Files.list(path.resolve(Constants.LIBRARIES_FOLDER)) + .filter(Files::isRegularFile) + .filter(p -> p.getFileName().toString().endsWith(".jar")); + } + }).flatMap(new Function>() { + @Override + @SneakyThrows + public Stream apply(Path path) { + return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false); + } + }).collect(Collectors.toUnmodifiableList()); + +// for (Map.Entry entry : mf.getEntries().entrySet()) { +// String jarEntryName = entry.getKey(); +// Attributes attributes = entry.getValue(); +// if (jarEntryName.startsWith(Constants.LIBRARIES_FOLDER + '/') && attributes.getValue(Constants.ManifestAttributes.ENTRY_HASH) != null) { +// jarList.add(fs.getPath(jarEntryName)); +// } +// } + + Path[] jars = jarList.toArray(new Path[jarList.size()]); + ClassLoader pathClassLoader = new PathClassLoader(jars); + String mainClassName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_CLASS); + String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE); + Class mainClass; + if (mainModuleName == null) { + mainClass = pathClassLoader.loadClass(mainClassName); + } else { + ModuleLayer bootLayer = ModuleLayer.boot(); + Configuration bootConfiguration = ModuleLayer.boot().configuration(); + Configuration cfg = bootConfiguration.resolve(ModuleFinder.of(jars), ModuleFinder.of(), Arrays.asList(mainModuleName)); + ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cfg, pathClassLoader); + Module mainModule = layer.findModule(mainModuleName).orElseThrow( + () -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName))); + mainClass = Class.forName(mainModule, mainClassName); + } + try { + Method mainMethod = mainClass.getMethod("main", String[].class); + Class returnType = mainMethod.getReturnType(); + if (mainMethod.getReturnType() != Void.TYPE) { + throw new IllegalArgumentException(String.format("Main method in class '%s' " + + "has wrong return type, expected '%s', found '%s' instead", mainClass, Void.class.getName(), returnType)); + } + mainMethod.invoke(null, (Object) args); + } catch (NoSuchMethodException nsme) { + throw new IllegalArgumentException(String.format("No valid main method found in class '%s'", mainClass), nsme); + } + } + } +} diff --git a/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/PathClassLoader.java b/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/PathClassLoader.java new file mode 100644 index 0000000..036ca91 --- /dev/null +++ b/executable-jar/executable-jar-launcher/src/main/java/net/woggioni/executable/jar/PathClassLoader.java @@ -0,0 +1,161 @@ +package net.woggioni.executable.jar; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.*; + +/** + * A classloader that loads classes from a {@link Path} instance + */ +public final class PathClassLoader extends ClassLoader { + + private final Iterable paths; + + static { + registerAsParallelCapable(); + } + + public PathClassLoader(Path ...path) { + this(Arrays.asList(path), null); + } + + public PathClassLoader(Iterable paths) { + this(paths, null); + } + + public PathClassLoader(Iterable paths, ClassLoader parent) { + super(parent); + this.paths = paths; + } + + @Override + @SneakyThrows + protected Class findClass(String name) { + String resource = name.replace('.', '/').concat(".class"); + for(Path path : paths) { + Path classPath = path.resolve(resource); + if (Files.exists(classPath)) { + byte[] byteCode = Files.readAllBytes(classPath); + return defineClass(name, byteCode, 0, byteCode.length); + } + } + throw new ClassNotFoundException(name); + } + + @Override + @SneakyThrows + protected URL findResource(String name) { + for(Path path : paths) { + Path resolved = path.resolve(name); + if (Files.exists(resolved)) { + return toURL(resolved); + } + } + return null; + } + + @Override + protected Enumeration findResources(final String name) throws IOException { + final List resources = new ArrayList<>(1); + for(Path path : paths) { + Files.walkFileTree(path, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + if (!name.isEmpty()) { + this.addIfMatches(resources, file); + } + return super.visitFile(file, attrs); + } + + @Override + public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { + if (!name.isEmpty() || path.equals(dir)) { + this.addIfMatches(resources, dir); + } + return super.preVisitDirectory(dir, attrs); + } + + void addIfMatches(List resources, Path file) throws IOException { + if (path.relativize(file).toString().equals(name)) { + resources.add(toURL(file)); + } + } + }); + } + return Collections.enumeration(resources); + } + + private static URL toURL(Path path) throws IOException { + return new URL(null, path.toUri().toString(), PathURLStreamHandler.INSTANCE); + } + + private static final class PathURLConnection extends URLConnection { + + private final Path path; + + PathURLConnection(URL url, Path path) { + super(url); + this.path = path; + } + + @Override + public void connect() {} + + @Override + public long getContentLengthLong() { + try { + return Files.size(this.path); + } catch (IOException e) { + throw new RuntimeException("could not get size of: " + this.path, e); + } + } + + @Override + public InputStream getInputStream() throws IOException { + return Files.newInputStream(this.path); + } + + @Override + public OutputStream getOutputStream() throws IOException { + return Files.newOutputStream(this.path); + } + + @Override + @SneakyThrows + public String getContentType() { + return Files.probeContentType(this.path); + } + + @Override + @SneakyThrows + public long getLastModified() { + BasicFileAttributes attributes = Files.readAttributes(this.path, BasicFileAttributes.class); + return attributes.lastModifiedTime().toMillis(); + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + private static final class PathURLStreamHandler extends URLStreamHandler { + + static final URLStreamHandler INSTANCE = new PathURLStreamHandler(); + + @Override + @SneakyThrows + protected URLConnection openConnection(URL url) { + URI uri = url.toURI(); + Path path = Paths.get(uri); + return new PathURLConnection(url, path); + } + } +} \ No newline at end of file diff --git a/executable-jar/executable-jar-launcher/src/test/java/net/woggioni/executable/jar/FooTest.java b/executable-jar/executable-jar-launcher/src/test/java/net/woggioni/executable/jar/FooTest.java new file mode 100644 index 0000000..8033caa --- /dev/null +++ b/executable-jar/executable-jar-launcher/src/test/java/net/woggioni/executable/jar/FooTest.java @@ -0,0 +1,147 @@ +package net.woggioni.executable.jar; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URI; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLStreamHandler; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.HashMap; +import java.util.List; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import lombok.SneakyThrows; +import org.junit.jupiter.api.Test; + +public class FooTest { + @Test + @SneakyThrows + void foo() { + Path.of(new URI("jar:file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar")); + } + + @Test + @SneakyThrows + void foo2() { + Path p = Path.of(new URI("file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar")); + FileSystem fs = FileSystems.newFileSystem(p, null); + StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function>() { + @Override + @SneakyThrows + public Stream apply(Path path) { + return Files.list(path); + } + }).forEach(r -> { + System.out.println(r); + }); + } + + @Test + @SneakyThrows + void test() { + Path fatJar = Path.of("/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"); + List jars = StreamSupport.stream(FileSystems.newFileSystem(fatJar, null).getRootDirectories().spliterator(), false) + .flatMap(new Function>() { + @Override + @SneakyThrows + public Stream apply(Path root) { + Path libDir = root.resolve("/LIB-INF"); + if (Files.exists(libDir) && Files.isDirectory(libDir)) { + return Files.list(libDir); + } else { + return Stream.empty(); + } + } + }).flatMap(new Function>() { + @Override + @SneakyThrows + public Stream apply(Path path) { + return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false); + } + }).collect(Collectors.toList()); + PathClassLoader p = new PathClassLoader(jars.toArray(new Path[jars.size()])); + Class cl = p.loadClass("net.woggioni.wson.serialization.binary.JBONParser"); + System.out.println(cl); + URL resource = p.findResource("citylots.json.xz"); + resource.openStream(); + } + + @Test + @SneakyThrows + void test2() { + FileSystem fs = FileSystems.newFileSystem(new URI("jar:file:/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"), new HashMap<>()); + String s = "jar:jar:file:///home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar!/LIB-INF/wson-test-utils-1.0.jar!/citylots.json.xz"; + URI uri = new URI(s); + Files.list(Path.of(uri)).forEach(System.out::println); + } + + private static final class PathURLConnection extends URLConnection { + + private final Path path; + + PathURLConnection(URL url, Path path) { + super(url); + this.path = path; + } + + @Override + public void connect() {} + + @Override + public long getContentLengthLong() { + try { + return Files.size(this.path); + } catch (IOException e) { + throw new RuntimeException("could not get size of: " + this.path, e); + } + } + + @Override + public InputStream getInputStream() throws IOException { + return Files.newInputStream(this.path); + } + + @Override + public OutputStream getOutputStream() throws IOException { + return Files.newOutputStream(this.path); + } + + @Override + @SneakyThrows + public String getContentType() { + return Files.probeContentType(this.path); + } + + @Override + @SneakyThrows + public long getLastModified() { + BasicFileAttributes attributes = Files.readAttributes(this.path, BasicFileAttributes.class); + return attributes.lastModifiedTime().toMillis(); + } + } + + @NoArgsConstructor(access = AccessLevel.PRIVATE) + private static final class PathURLStreamHandler extends URLStreamHandler { + + static final URLStreamHandler INSTANCE = new PathURLStreamHandler(); + + @Override + @SneakyThrows + protected URLConnection openConnection(URL url) { + URI uri = url.toURI(); + Path path = Paths.get(uri); + return new PathURLConnection(url, path); + } + } +} diff --git a/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarPlugin.java b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarPlugin.java new file mode 100644 index 0000000..8caeb06 --- /dev/null +++ b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarPlugin.java @@ -0,0 +1,18 @@ +package net.woggioni.gradle.executable.jar; + +import org.gradle.api.Plugin; +import org.gradle.api.Project; +import org.gradle.api.plugins.BasePluginExtension; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.tasks.bundling.Jar; + +public class ExecutableJarPlugin implements Plugin { + @Override + public void apply(Project project) { + BasePluginExtension basePluginExtension = project.getExtensions().getByType(BasePluginExtension.class); + project.getTasks().register("executable-jar", ExecutableJarTask.class, t -> { + t.includeLibraries(project.getConfigurations().named(JavaPlugin.RUNTIME_CLASSPATH_CONFIGURATION_NAME)); + t.includeLibraries(project.getTasks().named(JavaPlugin.JAR_TASK_NAME, Jar.class)); + }); + } +} diff --git a/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarTask.java b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarTask.java new file mode 100644 index 0000000..b863b44 --- /dev/null +++ b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/ExecutableJarTask.java @@ -0,0 +1,253 @@ +package net.woggioni.gradle.executable.jar; + +import java.io.File; +import java.io.InputStream; +import java.security.MessageDigest; +import java.util.Base64; +import java.util.Objects; +import java.util.function.Supplier; +import java.util.jar.Attributes; +import java.util.jar.JarFile; +import java.util.jar.Manifest; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; +import javax.annotation.Nonnull; +import javax.inject.Inject; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; +import net.woggioni.executable.jar.Common; +import net.woggioni.executable.jar.Constants; +import org.gradle.api.GradleException; +import org.gradle.api.internal.file.CopyActionProcessingStreamAction; +import org.gradle.api.internal.file.copy.CopyAction; +import org.gradle.api.internal.file.copy.CopyActionProcessingStream; +import org.gradle.api.internal.file.copy.FileCopyDetailsInternal; +import org.gradle.api.model.ObjectFactory; +import org.gradle.api.plugins.BasePluginExtension; +import org.gradle.api.plugins.JavaApplication; +import org.gradle.api.plugins.JavaPlugin; +import org.gradle.api.plugins.JavaPluginExtension; +import org.gradle.api.provider.Property; +import org.gradle.api.tasks.Input; +import org.gradle.api.tasks.Optional; +import org.gradle.api.tasks.WorkResult; +import org.gradle.api.tasks.bundling.AbstractArchiveTask; +import org.gradle.util.GradleVersion; + + +import static java.util.zip.Deflater.BEST_COMPRESSION; +import static java.util.zip.Deflater.NO_COMPRESSION; +import static net.woggioni.executable.jar.Constants.*; + +@SuppressWarnings({ "UnstableApiUsage", "unused" }) +public class ExecutableJarTask extends AbstractArchiveTask { + + private static final String MINIMUM_GRADLE_VERSION = "6.0"; + + static { + if (GradleVersion.current().compareTo(GradleVersion.version(MINIMUM_GRADLE_VERSION)) < 0) { + throw new GradleException(ExecutableJarTask.class.getName() + + " requires Gradle " + MINIMUM_GRADLE_VERSION + " or newer."); + } + } + + @Getter(onMethod_ = {@Input}) + private final Property mainClass; + + @Getter(onMethod_ = {@Input, @Optional}) + private final Property mainModule; + + public void includeLibraries(Object... files) { + into(LIBRARIES_FOLDER, (copySpec) -> copySpec.from(files)); + } + + + @Inject + public ExecutableJarTask(ObjectFactory objects) { + setGroup("build"); + setDescription("Creates an executable jar file, embedding all of its runtime dependencies"); + BasePluginExtension basePluginExtension = getProject().getExtensions().getByType(BasePluginExtension.class); + getDestinationDirectory().set(basePluginExtension.getLibsDirectory()); + getArchiveBaseName().convention(getProject().getName()); + getArchiveExtension().convention("jar"); + getArchiveVersion().convention(getProject().getVersion().toString()); + getArchiveAppendix().convention("executable"); + + mainClass = objects.property(String.class); + mainModule = objects.property(String.class); + JavaApplication javaApplication = getProject().getExtensions().findByType(JavaApplication.class); + if(!Objects.isNull(javaApplication)) { + mainClass.convention(javaApplication.getMainClass()); + mainModule.convention(javaApplication.getMainModule()); + } + from(getProject().tarTree(LauncherResource.instance), copySpec -> exclude(JarFile.MANIFEST_NAME)); + } + + @Input + public String getLauncherArchiveHash() { + return Common.bytesToHex(Common.computeSHA256Digest(LauncherResource.instance::read)); + } + + @RequiredArgsConstructor + private static class StreamAction implements CopyActionProcessingStreamAction { + + private final ZipOutputStream zoos; + private final Manifest manifest; + private final MessageDigest md; + private final ZipEntryFactory zipEntryFactory; + private final byte[] buffer; + + @Override + @SneakyThrows + public void processFile(FileCopyDetailsInternal fileCopyDetails) { + String entryName = fileCopyDetails.getRelativePath().toString(); + if (!fileCopyDetails.isDirectory() && entryName.startsWith(LIBRARIES_FOLDER)) { + Supplier streamSupplier = () -> Common.read(fileCopyDetails.getFile(), false); + Attributes attr = manifest.getEntries().computeIfAbsent(entryName, it -> new Attributes()); + md.reset(); + attr.putValue(Constants.ManifestAttributes.ENTRY_HASH, + Base64.getEncoder().encodeToString(Common.computeDigest(streamSupplier, md, buffer))); + } + if (METADATA_FOLDER.equals(entryName)) return; + if (fileCopyDetails.isDirectory()) { + ZipEntry zipEntry = zipEntryFactory.createDirectoryEntry(entryName, fileCopyDetails.getLastModified()); + zoos.putNextEntry(zipEntry); + } else { + ZipEntry zipEntry = zipEntryFactory.createZipEntry(entryName, fileCopyDetails.getLastModified()); + boolean compressed = Common.splitExtension(fileCopyDetails.getSourceName()) + .map(entry -> ".jar".equals(entry.getValue())) + .orElse(false); + if (!compressed) { + zipEntry.setMethod(ZipEntry.DEFLATED); + } else { + try (InputStream is = Common.read(fileCopyDetails.getFile(), false)) { + Common.computeSizeAndCrc32(zipEntry, is, buffer); + } + zipEntry.setMethod(ZipEntry.STORED); + } + zoos.putNextEntry(zipEntry); + try (InputStream is = Common.read(fileCopyDetails.getFile(), false)) { + Common.write2Stream(is, zoos, buffer); + } + } + } + } + + @SuppressWarnings("SameParameterValue") + @RequiredArgsConstructor + private static final class ZipEntryFactory { + + private final boolean isPreserveFileTimestamps; + private final long defaultLastModifiedTime; + + @Nonnull + ZipEntry createZipEntry(String entryName, long lastModifiedTime) { + ZipEntry zipEntry = new ZipEntry(entryName); + zipEntry.setTime(isPreserveFileTimestamps ? lastModifiedTime : ZIP_ENTRIES_DEFAULT_TIMESTAMP); + return zipEntry; + } + + @Nonnull + ZipEntry createZipEntry(String entryName) { + return createZipEntry(entryName, defaultLastModifiedTime); + } + + @Nonnull + ZipEntry createDirectoryEntry(@Nonnull String entryName, long lastModifiedTime) { + ZipEntry zipEntry = createZipEntry(entryName.endsWith("/") ? entryName : entryName + '/', lastModifiedTime); + zipEntry.setMethod(ZipEntry.STORED); + zipEntry.setCompressedSize(0); + zipEntry.setSize(0); + zipEntry.setCrc(0); + return zipEntry; + } + + @Nonnull + ZipEntry createDirectoryEntry(@Nonnull String entryName) { + return createDirectoryEntry(entryName, defaultLastModifiedTime); + } + + @Nonnull + ZipEntry copyOf(@Nonnull ZipEntry zipEntry) { + if (zipEntry.getMethod() == ZipEntry.STORED) { + return new ZipEntry(zipEntry); + } else { + ZipEntry newEntry = new ZipEntry(zipEntry.getName()); + newEntry.setMethod(ZipEntry.DEFLATED); + newEntry.setTime(zipEntry.getTime()); + newEntry.setExtra(zipEntry.getExtra()); + newEntry.setComment(zipEntry.getComment()); + return newEntry; + } + } + } + + @Override + @Nonnull + protected CopyAction createCopyAction() { + File destination = getArchiveFile().get().getAsFile(); + return new CopyAction() { + + private final ZipEntryFactory zipEntryFactory = new ZipEntryFactory(isPreserveFileTimestamps(), System.currentTimeMillis()); + + @Override + @Nonnull + @SneakyThrows + public WorkResult execute(@Nonnull CopyActionProcessingStream copyActionProcessingStream) { + Manifest manifest = new Manifest(); + Attributes mainAttributes = manifest.getMainAttributes(); + mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); + mainAttributes.put(Attributes.Name.MAIN_CLASS, DEFAULT_LAUNCHER); + mainAttributes.putValue(Constants.ManifestAttributes.MAIN_CLASS, mainClass.get()); + if(mainModule.isPresent()) { + mainAttributes.putValue(Constants.ManifestAttributes.MAIN_MODULE, mainModule.get()); + } + + MessageDigest md = MessageDigest.getInstance("SHA-256"); + byte[] buffer = new byte[Constants.BUFFER_SIZE]; + + /** + * The manifest has to be the first zip entry in a jar archive, as an example, + * {@link java.util.jar.JarInputStream} assumes the manifest is the first (or second at most) + * entry in the jar and simply returns a null manifest if that is not the case. + * In this case the manifest has to contain the hash of all the jar entries, so it cannot + * be computed in advance, we write all the entries to a temporary zip archive while computing the manifest, + * then we write the manifest to the final zip file as the first entry and, finally, + * we copy all the other entries from the temporary archive. + * + * The {@link org.gradle.api.Task#getTemporaryDir} directory is guaranteed + * to be unique per instance of this task. + */ + File temporaryJar = new File(getTemporaryDir(), "premature.zip"); + try (ZipOutputStream zipOutputStream = new ZipOutputStream(Common.write(temporaryJar, true))) { + zipOutputStream.setLevel(NO_COMPRESSION); + StreamAction streamAction = new StreamAction(zipOutputStream, manifest, md, zipEntryFactory, buffer); + copyActionProcessingStream.process(streamAction); + } + + try (ZipOutputStream zipOutputStream = new ZipOutputStream(Common.write(destination, true)); + ZipInputStream zipInputStream = new ZipInputStream(Common.read(temporaryJar, true))) { + zipOutputStream.setLevel(BEST_COMPRESSION); + ZipEntry zipEntry = zipEntryFactory.createDirectoryEntry(METADATA_FOLDER); + zipOutputStream.putNextEntry(zipEntry); + zipEntry = zipEntryFactory.createZipEntry(JarFile.MANIFEST_NAME); + zipEntry.setMethod(ZipEntry.DEFLATED); + zipOutputStream.putNextEntry(zipEntry); + manifest.write(zipOutputStream); + + while (true) { + zipEntry = zipInputStream.getNextEntry(); + if (zipEntry == null) break; + // Create a new ZipEntry explicitly, without relying on + // subtle (undocumented?) behaviour of ZipInputStream. + zipOutputStream.putNextEntry(zipEntryFactory.copyOf(zipEntry)); + Common.write2Stream(zipInputStream, zipOutputStream, buffer); + } + return () -> true; + } + } + }; + } +} \ No newline at end of file diff --git a/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/LauncherResource.java b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/LauncherResource.java new file mode 100644 index 0000000..178bb9b --- /dev/null +++ b/executable-jar/src/main/java/net/woggioni/gradle/executable/jar/LauncherResource.java @@ -0,0 +1,42 @@ +package net.woggioni.gradle.executable.jar; + +import java.io.InputStream; +import java.net.URI; +import java.net.URL; +import javax.annotation.Nonnull; +import lombok.SneakyThrows; +import org.gradle.api.resources.ReadableResource; +import org.gradle.api.resources.ResourceException; + +final class LauncherResource implements ReadableResource { + static final ReadableResource instance = new LauncherResource(); + + private final URL url; + + private LauncherResource() { + url = getClass().getResource(String.format("/META-INF/%s", getDisplayName())); + } + + @Override + @Nonnull + @SneakyThrows + public InputStream read() throws ResourceException { + return url.openStream(); + } + + @Override + public String getDisplayName() { + return getBaseName() + ".tar"; + } + + @Override + @SneakyThrows + public URI getURI() { + return url.toURI(); + } + + @Override + public String getBaseName() { + return "executable-jar-launcher"; + } +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 14fc348..6550134 100644 --- a/settings.gradle +++ b/settings.gradle @@ -17,3 +17,6 @@ include("multi-release-jar") include("wildfly") include("lombok") include("jlink") +include("executable-jar") +include("executable-jar:executable-jar-common") +include("executable-jar:executable-jar-launcher")