From 51ceac69daae29ca9feecfae5aa297784e729e0e Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Mon, 2 Aug 2021 11:38:43 +0200 Subject: [PATCH] Makes the jar building finally work --- .../utility/JarBuilder.java | 48 +++++++------------ .../utility/JarBuilderTest.java | 11 ++++- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java index 00671b2..e5d9926 100644 --- a/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java +++ b/src/main/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilder.java @@ -8,17 +8,13 @@ import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.nio.file.Path; import java.nio.file.Paths; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; public class JarBuilder { private String downloadedBuildToolsVersion = "#124"; - private String buildDirectory; - private String jarDirectory; + private final String buildDirectory; + private final String jarDirectory; public JarBuilder(String buildDirectory, String jarDirectory) { this.buildDirectory = buildDirectory; @@ -30,7 +26,7 @@ public class JarBuilder { */ public void buildSpigotJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev", + ProcessBuilder processBuilder = new ProcessBuilder("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\java.exe", "-jar", "BuildTools.jar", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); System.out.println("Finished building spigot .jar. Moving it to the correct location"); @@ -43,7 +39,7 @@ public class JarBuilder { */ public void buildBukkitJar() { downloadBuildTools(); - ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--compile", + ProcessBuilder processBuilder = new ProcessBuilder("C:\\Program Files\\Java\\jdk-16.0.1\\bin\\java.exe", "-jar", "BuildTools.jar", "--compile", "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory); executeBuildProcess(processBuilder); System.out.println("Finished building craftbukkit .jar. Moving it to the correct location"); @@ -100,29 +96,19 @@ public class JarBuilder { Process process = processBuilder.start(); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); - ScheduledExecutorService builderOutputExecutor = Executors.newSingleThreadScheduledExecutor(); - builderOutputExecutor.scheduleAtFixedRate(() -> { - try { - String readText = CommonFunctions.readBufferedReader(reader); - if (!readText.equals("")) { - writer.write(readText); - writer.flush(); - } - if (!process.isAlive()) { - writer.write("Closing process"); - writer.flush(); - builderOutputExecutor.shutdown(); - writer.close(); - reader.close(); - process.destroyForcibly(); - } - } catch (IOException e) { - e.printStackTrace(); - } - }, 1000, 5000, TimeUnit.MILLISECONDS); - process.waitFor(); - System.out.println("Stopped waiting for process"); - } catch (IOException | InterruptedException e) { + + do { + writer.write(CommonFunctions.readBufferedReader(reader)); + } while (process.isAlive()); + + writer.flush(); + + if (process.exitValue() == 0) { + System.out.println("Jar building process finished successfully."); + } else { + throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + "."); + } + } catch (IOException e) { e.printStackTrace(); } } diff --git a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java index 08cd53b..63d7039 100644 --- a/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java +++ b/src/test/java/net/knarcraft/minecraftserverlauncher/utility/JarBuilderTest.java @@ -1,7 +1,6 @@ package net.knarcraft.minecraftserverlauncher.utility; import net.knarcraft.minecraftserverlauncher.Main; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.Order; @@ -34,13 +33,21 @@ public class JarBuilderTest { @Test @Order(3) public void buildLatestSpigotJarTest() { + File spigotFile = new File(jarDirectory + "SpigotLatest.jar"); + if (spigotFile.exists() && !spigotFile.delete()) { + throw new IllegalArgumentException("Unable to remove existing spigot .jar"); + } jarBuilder.buildSpigotJar(); - assertTrue(new File(jarDirectory + "SpigotLatest.jar").exists()); + assertTrue(spigotFile.exists()); } @Test @Order(4) public void buildLatestBukkitJarTest() { + File bukkitFile = new File(jarDirectory + "BukkitLatest.jar"); + if (bukkitFile.exists() && !bukkitFile.delete()) { + throw new IllegalArgumentException("Unable to remove existing bukkit .jar"); + } jarBuilder.buildBukkitJar(); assertTrue(new File(jarDirectory + "BukkitLatest.jar").exists()); }