From 3e6afac39310798d9d8f2bdfd5a2122e30307d46 Mon Sep 17 00:00:00 2001 From: Huynh Tien Date: Wed, 28 Dec 2022 20:30:02 +0700 Subject: [PATCH] Improve Spigot Updater (#3) * process on SpigotUpdater * debug line * exception * not need to use debug line * debug line * log process * external property for SpigotUpdater * destroy the process --- README.md | 8 ++- .../updater/SpigotUpdater.java | 57 ++++++++++++------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index b7e98cc..b34ba98 100644 --- a/README.md +++ b/README.md @@ -96,4 +96,10 @@ class Main { } ``` -3. Do whatever you want with the `UpdateStatus` \ No newline at end of file +3. Do whatever you want with the `UpdateStatus` + +## Additional System Properties + +| Name | Description | Default | +|----------------------------------|----------------------------------------------------------------------|---------| +| `MCServerUpdater.javaExecutable` | The Java executable to run external processes (Spigot Updater, etc.) | `java` | \ No newline at end of file diff --git a/mc-server-updater-lib/src/main/java/me/hsgamer/mcserverupdater/updater/SpigotUpdater.java b/mc-server-updater-lib/src/main/java/me/hsgamer/mcserverupdater/updater/SpigotUpdater.java index 712d713..5ec0ca7 100644 --- a/mc-server-updater-lib/src/main/java/me/hsgamer/mcserverupdater/updater/SpigotUpdater.java +++ b/mc-server-updater-lib/src/main/java/me/hsgamer/mcserverupdater/updater/SpigotUpdater.java @@ -8,9 +8,6 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Method; -import java.net.URL; -import java.net.URLClassLoader; import java.net.URLConnection; import java.nio.file.Files; import java.nio.file.StandardCopyOption; @@ -27,6 +24,7 @@ private File downloadBuildTools() { File file = new File(updateBuilder.workingDirectory(), "BuildTools.jar"); try { String buildToolsURL = "https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/artifact/target/BuildTools.jar"; + updateBuilder.debug("Downloading BuildTools from " + buildToolsURL); URLConnection connection = UserAgent.CHROME.assignToConnection(WebUtils.createConnection(buildToolsURL)); InputStream inputStream = connection.getInputStream(); Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING); @@ -44,25 +42,44 @@ public boolean update(File file, String version, String build) throws Exception return false; } File outputDir = new File(updateBuilder.workingDirectory(), "output"); - try (URLClassLoader classLoader = new URLClassLoader(new URL[]{buildTools.toURI().toURL()}, getClass().getClassLoader())) { - Class clazz = Class.forName("org.spigotmc.builder.Bootstrap", true, classLoader); - Method method = clazz.getMethod("main", String[].class); - method.invoke(null, (Object) new String[]{ - "--rev", version, - "--output-dir", outputDir.getAbsolutePath(), - "--compile-if-changed" - }); - - for (File outputFile : Objects.requireNonNull(outputDir.listFiles())) { - String name = outputFile.getName(); - if (name.startsWith("spigot-") && name.endsWith(".jar")) { - Files.copy(outputFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); - Files.delete(outputFile.toPath()); - break; - } + updateBuilder.debug("Running BuildTools..."); + if (!runBuildTools(buildTools, outputDir, version)) { + return false; + } + for (File outputFile : Objects.requireNonNull(outputDir.listFiles())) { + String name = outputFile.getName(); + if (name.startsWith("spigot-") && name.endsWith(".jar")) { + updateBuilder.debug("Copying " + name + " to " + file.getName()); + Files.copy(outputFile.toPath(), file.toPath(), StandardCopyOption.REPLACE_EXISTING); + Files.delete(outputFile.toPath()); + break; } - return true; } + return true; + } + + private boolean runBuildTools(File buildTools, File outputDir, String version) throws IOException, InterruptedException { + String javaExecutable = System.getProperty("MCServerUpdater.javaExecutable", "java"); + ProcessBuilder processBuilder = new ProcessBuilder( + javaExecutable, + "-jar", + buildTools.getAbsolutePath(), + "--rev", version, + "--output-dir", outputDir.getAbsolutePath(), + "--compile-if-changed", + "--disable-java-check" + ); + processBuilder.directory(updateBuilder.workingDirectory()); + processBuilder.redirectErrorStream(true); + Process process = processBuilder.start(); + Runtime.getRuntime().addShutdownHook(new Thread(process::destroy)); + InputStream inputStream = process.getInputStream(); + byte[] buffer = new byte[1024]; + int length; + while ((length = inputStream.read(buffer)) != -1) { + updateBuilder.debug(new String(buffer, 0, length).trim()); + } + return process.waitFor() == 0; } @Override