added old executable-jar plugin prototype (excluded from build)

This commit is contained in:
2021-09-25 21:33:03 +02:00
parent 87d040d125
commit f45e25bcd3
12 changed files with 225 additions and 53 deletions

View File

@@ -1,5 +1,4 @@
plugins { plugins {
id 'maven-publish'
id 'java-gradle-plugin' id 'java-gradle-plugin'
} }
@@ -23,6 +22,11 @@ tasks.named("processResources") {
inputs.files(copyLauncher) inputs.files(copyLauncher)
} }
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
jar { jar {
manifest { manifest {
attributes "version" : archiveVersion.get() attributes "version" : archiveVersion.get()
@@ -38,8 +42,8 @@ jar {
gradlePlugin { gradlePlugin {
plugins { plugins {
create("JlinkPlugin") { create("ExecutableJarPlugin") {
id = "net.woggioni.gradle.executable.jar" id = "net.woggioni.gradle.executable-jar"
implementationClass = "net.woggioni.gradle.executable.jar.ExecutableJarPlugin" implementationClass = "net.woggioni.gradle.executable.jar.ExecutableJarPlugin"
} }
} }

View File

@@ -1,3 +1,14 @@
plugins { plugins {
id 'java-library' id 'java-library'
} }
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
jar {
manifest {
attributes "Automatic-Module-Name" : "net.woggioni.executable.jar"
}
}

View File

@@ -11,6 +11,8 @@ public class Constants {
public static final String METADATA_FOLDER = "META-INF"; public static final String METADATA_FOLDER = "META-INF";
public static final int BUFFER_SIZE = 0x10000; public static final int BUFFER_SIZE = 0x10000;
public static final String DEFAULT_LAUNCHER = "net.woggioni.executable.jar.Launcher"; public static final String DEFAULT_LAUNCHER = "net.woggioni.executable.jar.Launcher";
public static final String AGENT_LAUNCHER = "net.woggioni.executable.jar.JavaAgentLauncher";
public static final String JAVA_AGENTS_FILE = METADATA_FOLDER + "/javaAgents.properties";
public static class ManifestAttributes { public static class ManifestAttributes {
public static final String MAIN_MODULE = "Executable-Jar-Main-Module"; public static final String MAIN_MODULE = "Executable-Jar-Main-Module";

View File

@@ -1,8 +1,19 @@
import java.util.jar.Attributes import java.util.jar.Attributes
buildscript {
dependencies {
classpath project(":multi-release-jar")
}
}
plugins { plugins {
id 'java-library' id 'java-library'
} }
ext.setProperty("jpms.module.name", "net.woggioni.executable.jar")
apply plugin: 'net.woggioni.gradle.multi-release-jar'
configurations { configurations {
embedded embedded
compileOnly.extendsFrom(embedded) compileOnly.extendsFrom(embedded)
@@ -10,12 +21,20 @@ configurations {
dependencies { dependencies {
embedded project(path: ":executable-jar:executable-jar-common", configuration: 'archives') embedded project(path: ":executable-jar:executable-jar-common", configuration: 'archives')
embedded group: "net.woggioni", name: "xclassloader", version: getProperty("version.xclassloader")
} }
java { java {
modularity.inferModulePath = true modularity.inferModulePath = true
} }
tasks.withType(JavaCompile).configureEach {
javaCompiler = javaToolchains.compilerFor {
languageVersion = JavaLanguageVersion.of(16)
}
options.forkOptions.jvmArgs << "--illegal-access=permit"
}
jar { jar {
manifest { manifest {
attributes([ attributes([
@@ -29,13 +48,17 @@ jar {
tasks.register("tar", Tar) { tasks.register("tar", Tar) {
archiveFileName = "${project.name}.tar" archiveFileName = "${project.name}.tar"
from(sourceSets.main.output) from(project.tasks.named(JavaPlugin.JAR_TASK_NAME)
from { .flatMap(Jar.&getArchiveFile)
configurations.named('embedded').map { .map(RegularFile.&getAsFile)
it.collect { .map(project.&zipTree))
it.isDirectory() ? it : zipTree(it) from(configurations.named('embedded').map {
} it.collect {
it.isDirectory() ? it : zipTree(it)
} }
}) {
exclude("**/module-info.class")
exclude("META-INF/MANIFEST.MF")
} }
} }

View File

@@ -0,0 +1,37 @@
package net.woggioni.executable.jar;
import java.io.InputStream;
import java.lang.instrument.Instrumentation;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;
import lombok.SneakyThrows;
class JavaAgentLauncher {
@SneakyThrows
static void premain(String agentArguments, Instrumentation instrumentation) {
ClassLoader cl = JavaAgentLauncher.class.getClassLoader();
Enumeration<URL> it = cl.getResources(Constants.JAVA_AGENTS_FILE);
while (it.hasMoreElements()) {
URL url = it.nextElement();
Properties properties = new Properties();
try (InputStream is = url.openStream()) {
properties.load(is);
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String agentClassName = (String) entry.getKey();
String agentArgs = (String) entry.getValue();
Class<?> agentClass = cl.loadClass(agentClassName);
Method premainMethod = agentClass.getMethod("premain", String.class, Instrumentation.class);
premainMethod.invoke(null, agentArgs, instrumentation);
}
}
}
static void agentmain(String agentArguments, Instrumentation instrumentation) {
premain(agentArguments, instrumentation);
}
}

View File

@@ -1,8 +1,6 @@
package net.woggioni.executable.jar; package net.woggioni.executable.jar;
import java.io.InputStream; import java.io.InputStream;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.net.URI; import java.net.URI;
import java.net.URL; import java.net.URL;
@@ -10,22 +8,23 @@ import java.nio.file.FileSystem;
import java.nio.file.FileSystems; import java.nio.file.FileSystems;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.jar.Manifest; import java.util.jar.Manifest;
import java.util.stream.Collector;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
import java.util.stream.StreamSupport; import java.util.stream.StreamSupport;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.java.Log; import lombok.extern.java.Log;
@Log @Log
public class Launcher { public class Launcher {
@@ -50,9 +49,14 @@ public class Launcher {
try(InputStream is = manifestResource.openStream()) { try(InputStream is = manifestResource.openStream()) {
mf.read(is); mf.read(is);
} }
try(FileSystem fs = FileSystems.newFileSystem(Path.of(currentJar.getPath()), null)) { try(FileSystem fs = FileSystems.newFileSystem(Paths.get(currentJar), null)) {
Attributes mainAttributes = mf.getMainAttributes(); Attributes mainAttributes = mf.getMainAttributes();
Collector<Path, ArrayList<Path>, List<Path>> immutableListCollector = Collector.of(
ArrayList::new,
List::add,
(l1, l2) -> { l1.addAll(l2); return l1; },
Collections::unmodifiableList);
List<Path> jarList = StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function<Path, Stream<Path>>() { List<Path> jarList = StreamSupport.stream(fs.getRootDirectories().spliterator(), false).flatMap(new Function<Path, Stream<Path>>() {
@Override @Override
@SneakyThrows @SneakyThrows
@@ -67,32 +71,11 @@ public class Launcher {
public Stream<Path> apply(Path path) { public Stream<Path> apply(Path path) {
return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false); return StreamSupport.stream(FileSystems.newFileSystem(path, null).getRootDirectories().spliterator(), false);
} }
}).collect(Collectors.toUnmodifiableList()); }).collect(immutableListCollector);
// for (Map.Entry<String, Attributes> 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 mainClassName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_CLASS);
String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE); String mainModuleName = mainAttributes.getValue(Constants.ManifestAttributes.MAIN_MODULE);
Class<?> mainClass; Class<?> mainClass = MainClassLoader.loadMainClass(jarList, mainModuleName, mainClassName);
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 { try {
Method mainMethod = mainClass.getMethod("main", String[].class); Method mainMethod = mainClass.getMethod("main", String[].class);
Class<?> returnType = mainMethod.getReturnType(); Class<?> returnType = mainMethod.getReturnType();

View File

@@ -0,0 +1,13 @@
package net.woggioni.executable.jar;
import java.nio.file.Path;
import lombok.SneakyThrows;
class MainClassLoader {
@SneakyThrows
static Class<?> loadMainClass(Iterable<Path> roots, String mainModuleName, String mainClassName) {
ClassLoader pathClassLoader = new net.woggioni.xclassloader.PathClassLoader(roots);
return pathClassLoader.loadClass(mainClassName);
}
}

View File

@@ -1,19 +1,26 @@
package net.woggioni.executable.jar; package net.woggioni.executable.jar;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URI;
import java.net.URL; import java.net.URL;
import java.net.URLConnection; import java.net.URLConnection;
import java.net.URLStreamHandler; import java.net.URLStreamHandler;
import java.nio.file.*; import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.BasicFileAttributes;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
/** /**
* A classloader that loads classes from a {@link Path} instance * A classloader that loads classes from a {@link Path} instance
@@ -153,8 +160,30 @@ public final class PathClassLoader extends ClassLoader {
@Override @Override
@SneakyThrows @SneakyThrows
protected URLConnection openConnection(URL url) { protected URLConnection openConnection(URL url) {
URI uri = url.toURI(); List<String> stack = new ArrayList<>();
Path path = Paths.get(uri); URL currentURL = url;
while(true) {
String file = currentURL.getFile();
int exclamationMark = file.lastIndexOf('!');
if(exclamationMark != -1) {
stack.add(file.substring(exclamationMark + 1));
currentURL = new URL(file.substring(0, exclamationMark));
} else {
stack.add(file);
break;
}
}
Path path;
FileSystem fs = FileSystems.getDefault();
while(true) {
String pathString = stack.remove(stack.size() - 1);
path = fs.getPath(pathString);
if(stack.isEmpty()) break;
else {
fs = FileSystems.newFileSystem(path, null);
}
}
return new PathURLConnection(url, path); return new PathURLConnection(url, path);
} }
} }

View File

@@ -1,4 +1,6 @@
module net.woggioni.executable.jar { module net.woggioni.executable.jar {
requires java.logging; requires java.logging;
requires static lombok; requires static lombok;
requires net.woggioni.xclassloader;
requires java.instrument;
} }

View File

@@ -0,0 +1,39 @@
package net.woggioni.executable.jar;
import java.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Optional;
import lombok.SneakyThrows;
import net.woggioni.xclassloader.PathClassLoader;
import net.woggioni.xclassloader.PathModuleFinder;
class MainClassLoader {
@SneakyThrows
static Class<?> loadMainClass(Iterable<Path> roots, String mainModuleName, String mainClassName) {
if (mainModuleName == null) {
ClassLoader pathClassLoader = new net.woggioni.xclassloader.PathClassLoader(roots);
return pathClassLoader.loadClass(mainClassName);
} else {
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration bootConfiguration = bootLayer.configuration();
Configuration cfg = bootConfiguration.resolve(new PathModuleFinder(roots), ModuleFinder.of(), Collections.singletonList(mainModuleName));
ClassLoader pathClassLoader = new PathClassLoader(roots, cfg, null);
ModuleLayer.Controller controller =
ModuleLayer.defineModules(cfg, Collections.singletonList(ModuleLayer.boot()), moduleName -> pathClassLoader);
ModuleLayer layer = controller.layer();
for(Module module : layer.modules()) {
controller.addReads(module, pathClassLoader.getUnnamedModule());
}
Module mainModule = layer.findModule(mainModuleName).orElseThrow(
() -> new IllegalStateException(String.format("Main module '%s' not found", mainModuleName)));
return Optional.ofNullable(mainClassName)
.or(() -> mainModule.getDescriptor().mainClass())
.map(className -> Class.forName(mainModule, className))
.orElseThrow(() -> new IllegalStateException(String.format("Unable to determine main class name for module '%s'", mainModule.getName())));
}
}
}

View File

@@ -22,6 +22,7 @@ import java.util.stream.StreamSupport;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.gradle.internal.impldep.org.junit.Ignore;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class FooTest { public class FooTest {
@@ -78,6 +79,7 @@ public class FooTest {
} }
@Test @Test
@Ignore
@SneakyThrows @SneakyThrows
void test2() { void test2() {
FileSystem fs = FileSystems.newFileSystem(new URI("jar:file:/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"), new HashMap<>()); FileSystem fs = FileSystems.newFileSystem(new URI("jar:file:/home/woggioni/code/wson/benchmark/build/libs/benchmark-executable-1.0.jar"), new HashMap<>());

View File

@@ -2,9 +2,18 @@ package net.woggioni.gradle.executable.jar;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.security.MessageDigest; import java.security.MessageDigest;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Base64; import java.util.Base64;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.function.Supplier; import java.util.function.Supplier;
import java.util.jar.Attributes; import java.util.jar.Attributes;
import java.util.jar.JarFile; import java.util.jar.JarFile;
@@ -27,8 +36,6 @@ import org.gradle.api.internal.file.copy.FileCopyDetailsInternal;
import org.gradle.api.model.ObjectFactory; import org.gradle.api.model.ObjectFactory;
import org.gradle.api.plugins.BasePluginExtension; import org.gradle.api.plugins.BasePluginExtension;
import org.gradle.api.plugins.JavaApplication; 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.provider.Property;
import org.gradle.api.tasks.Input; import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.Optional;
@@ -41,7 +48,7 @@ import static java.util.zip.Deflater.BEST_COMPRESSION;
import static java.util.zip.Deflater.NO_COMPRESSION; import static java.util.zip.Deflater.NO_COMPRESSION;
import static net.woggioni.executable.jar.Constants.*; import static net.woggioni.executable.jar.Constants.*;
@SuppressWarnings({ "UnstableApiUsage", "unused" }) @SuppressWarnings({"unused" })
public class ExecutableJarTask extends AbstractArchiveTask { public class ExecutableJarTask extends AbstractArchiveTask {
private static final String MINIMUM_GRADLE_VERSION = "6.0"; private static final String MINIMUM_GRADLE_VERSION = "6.0";
@@ -59,6 +66,17 @@ public class ExecutableJarTask extends AbstractArchiveTask {
@Getter(onMethod_ = {@Input, @Optional}) @Getter(onMethod_ = {@Input, @Optional})
private final Property<String> mainModule; private final Property<String> mainModule;
private final Properties javaAgents = new Properties();
@Input
public Set<Map.Entry<Object, Object>> getJavaAgents() {
return Collections.unmodifiableSet(javaAgents.entrySet());
}
public void javaAgent(String className, String args) {
javaAgents.put(className, args);
}
public void includeLibraries(Object... files) { public void includeLibraries(Object... files) {
into(LIBRARIES_FOLDER, (copySpec) -> copySpec.from(files)); into(LIBRARIES_FOLDER, (copySpec) -> copySpec.from(files));
} }
@@ -69,11 +87,12 @@ public class ExecutableJarTask extends AbstractArchiveTask {
setGroup("build"); setGroup("build");
setDescription("Creates an executable jar file, embedding all of its runtime dependencies"); setDescription("Creates an executable jar file, embedding all of its runtime dependencies");
BasePluginExtension basePluginExtension = getProject().getExtensions().getByType(BasePluginExtension.class); BasePluginExtension basePluginExtension = getProject().getExtensions().getByType(BasePluginExtension.class);
getDestinationDirectory().set(basePluginExtension.getLibsDirectory()); getDestinationDirectory().set(basePluginExtension.getDistsDirectory());
getArchiveBaseName().convention(getProject().getName()); getArchiveBaseName().convention(getProject().getName());
getArchiveExtension().convention("jar"); getArchiveExtension().convention("jar");
getArchiveVersion().convention(getProject().getVersion().toString()); getArchiveVersion().convention(getProject().getVersion().toString());
getArchiveAppendix().convention("executable"); getArchiveAppendix().convention("executable");
exclude("**/module-info.class");
mainClass = objects.property(String.class); mainClass = objects.property(String.class);
mainModule = objects.property(String.class); mainModule = objects.property(String.class);
@@ -200,6 +219,10 @@ public class ExecutableJarTask extends AbstractArchiveTask {
Attributes mainAttributes = manifest.getMainAttributes(); Attributes mainAttributes = manifest.getMainAttributes();
mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0"); mainAttributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
mainAttributes.put(Attributes.Name.MAIN_CLASS, DEFAULT_LAUNCHER); mainAttributes.put(Attributes.Name.MAIN_CLASS, DEFAULT_LAUNCHER);
mainAttributes.put(Attributes.Name.MULTI_RELEASE, "true");
mainAttributes.put(new Attributes.Name("Launcher-Agent-Class"), AGENT_LAUNCHER);
mainAttributes.put(new Attributes.Name("Can-Redefine-Classes"), "true");
mainAttributes.put(new Attributes.Name("Can-Retransform-Classes"), "true");
mainAttributes.putValue(Constants.ManifestAttributes.MAIN_CLASS, mainClass.get()); mainAttributes.putValue(Constants.ManifestAttributes.MAIN_CLASS, mainClass.get());
if(mainModule.isPresent()) { if(mainModule.isPresent()) {
mainAttributes.putValue(Constants.ManifestAttributes.MAIN_MODULE, mainModule.get()); mainAttributes.putValue(Constants.ManifestAttributes.MAIN_MODULE, mainModule.get());
@@ -228,7 +251,7 @@ public class ExecutableJarTask extends AbstractArchiveTask {
} }
try (ZipOutputStream zipOutputStream = new ZipOutputStream(Common.write(destination, true)); try (ZipOutputStream zipOutputStream = new ZipOutputStream(Common.write(destination, true));
ZipInputStream zipInputStream = new ZipInputStream(Common.read(temporaryJar, true))) { ZipInputStream zipInputStream = new ZipInputStream(Common.read(temporaryJar, true))) {
zipOutputStream.setLevel(BEST_COMPRESSION); zipOutputStream.setLevel(BEST_COMPRESSION);
ZipEntry zipEntry = zipEntryFactory.createDirectoryEntry(METADATA_FOLDER); ZipEntry zipEntry = zipEntryFactory.createDirectoryEntry(METADATA_FOLDER);
zipOutputStream.putNextEntry(zipEntry); zipOutputStream.putNextEntry(zipEntry);
@@ -236,6 +259,10 @@ public class ExecutableJarTask extends AbstractArchiveTask {
zipEntry.setMethod(ZipEntry.DEFLATED); zipEntry.setMethod(ZipEntry.DEFLATED);
zipOutputStream.putNextEntry(zipEntry); zipOutputStream.putNextEntry(zipEntry);
manifest.write(zipOutputStream); manifest.write(zipOutputStream);
zipEntry = zipEntryFactory.createZipEntry(JAVA_AGENTS_FILE);
zipEntry.setMethod(ZipEntry.DEFLATED);
zipOutputStream.putNextEntry(zipEntry);
javaAgents.store(zipOutputStream, null);
while (true) { while (true) {
zipEntry = zipInputStream.getNextEntry(); zipEntry = zipInputStream.getNextEntry();