Makes the jar building finally work
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2021-08-02 11:38:43 +02:00
parent 123a8eddda
commit 51ceac69da
2 changed files with 26 additions and 33 deletions

View File

@ -8,17 +8,13 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class JarBuilder { public class JarBuilder {
private String downloadedBuildToolsVersion = "#124"; private String downloadedBuildToolsVersion = "#124";
private String buildDirectory; private final String buildDirectory;
private String jarDirectory; private final String jarDirectory;
public JarBuilder(String buildDirectory, String jarDirectory) { public JarBuilder(String buildDirectory, String jarDirectory) {
this.buildDirectory = buildDirectory; this.buildDirectory = buildDirectory;
@ -30,7 +26,7 @@ public class JarBuilder {
*/ */
public void buildSpigotJar() { public void buildSpigotJar() {
downloadBuildTools(); 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); "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); executeBuildProcess(processBuilder);
System.out.println("Finished building spigot .jar. Moving it to the correct location"); System.out.println("Finished building spigot .jar. Moving it to the correct location");
@ -43,7 +39,7 @@ public class JarBuilder {
*/ */
public void buildBukkitJar() { public void buildBukkitJar() {
downloadBuildTools(); 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); "craftbukkit", "--rev", "latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder); executeBuildProcess(processBuilder);
System.out.println("Finished building craftbukkit .jar. Moving it to the correct location"); System.out.println("Finished building craftbukkit .jar. Moving it to the correct location");
@ -100,29 +96,19 @@ public class JarBuilder {
Process process = processBuilder.start(); Process process = processBuilder.start();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
ScheduledExecutorService builderOutputExecutor = Executors.newSingleThreadScheduledExecutor();
builderOutputExecutor.scheduleAtFixedRate(() -> { do {
try { writer.write(CommonFunctions.readBufferedReader(reader));
String readText = CommonFunctions.readBufferedReader(reader); } while (process.isAlive());
if (!readText.equals("")) {
writer.write(readText); writer.flush();
writer.flush();
} if (process.exitValue() == 0) {
if (!process.isAlive()) { System.out.println("Jar building process finished successfully.");
writer.write("Closing process"); } else {
writer.flush(); throw new RuntimeException("Jar building process failed with exit code " + process.exitValue() + ".");
builderOutputExecutor.shutdown(); }
writer.close(); } catch (IOException e) {
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) {
e.printStackTrace(); e.printStackTrace();
} }
} }

View File

@ -1,7 +1,6 @@
package net.knarcraft.minecraftserverlauncher.utility; package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.Main;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@ -34,13 +33,21 @@ public class JarBuilderTest {
@Test @Test
@Order(3) @Order(3)
public void buildLatestSpigotJarTest() { 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(); jarBuilder.buildSpigotJar();
assertTrue(new File(jarDirectory + "SpigotLatest.jar").exists()); assertTrue(spigotFile.exists());
} }
@Test @Test
@Order(4) @Order(4)
public void buildLatestBukkitJarTest() { 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(); jarBuilder.buildBukkitJar();
assertTrue(new File(jarDirectory + "BukkitLatest.jar").exists()); assertTrue(new File(jarDirectory + "BukkitLatest.jar").exists());
} }