Adds some still not working code for building spigot and bukkit .jar files
All checks were successful
KnarCraft/Minecraft-Server-Launcher/pipeline/head This commit looks good

This commit is contained in:
2021-06-11 20:41:30 +02:00
parent 8cdb1f143c
commit 123a8eddda
10 changed files with 245 additions and 21 deletions

View File

@ -202,6 +202,8 @@ public final class CommonFunctions {
if (reader.read(readCharacters) > 0) {
text.append(readCharacters);
readCharacters = new char[1000];
} else {
return text.toString().trim();
}
}
return text.toString().trim();

View File

@ -0,0 +1,141 @@
package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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;
public JarBuilder(String buildDirectory, String jarDirectory) {
this.buildDirectory = buildDirectory;
this.jarDirectory = jarDirectory;
}
/**
* Builds the latest version of the spigot .jar file
*/
public void buildSpigotJar() {
downloadBuildTools();
ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", "BuildTools.jar", "--rev",
"latest", "--output-dir", jarDirectory);
executeBuildProcess(processBuilder);
System.out.println("Finished building spigot .jar. Moving it to the correct location");
moveBuiltJar("spigot-", "SpigotLatest.jar");
System.out.println("Finished moving spigot .jar");
}
/**
* Builds the latest version of the craftbukkit .jar file
*/
public void buildBukkitJar() {
downloadBuildTools();
ProcessBuilder processBuilder = new ProcessBuilder("java", "-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");
moveBuiltJar("craftbukkit-", "BukkitLatest.jar");
System.out.println("Finished moving craftbukkit .jar");
}
/**
* Downloads the latest BuildTools version
*/
public void downloadBuildTools() {
try {
String latestVersion = getLatestBuildToolsVersion();
if (!latestVersion.equals(downloadedBuildToolsVersion)) {
boolean success = CommonFunctions.downloadFile("https://hub.spigotmc.org/jenkins/job/BuildTools/" +
"lastSuccessfulBuild/artifact/target/BuildTools.jar", Paths.get(buildDirectory + "BuildTools.jar"));
if (!success) {
Main.getController().getGUI().setStatus("Unable to download the latest BuildTools");
}
}
} catch (IOException e) {
Main.getController().getGUI().setStatus("Unable to download the latest BuildTools");
}
}
/**
* Moves a built .jar file to its target file
* @param searchString <p>The start of the name of the built file used to find it</p>
* @param newName <p>The new name of the built file</p>
*/
private void moveBuiltJar(String searchString, String newName) {
File[] foundFiles = new File(jarDirectory).listFiles((file) -> file.isFile() && file.getName().startsWith(searchString));
if (foundFiles != null && foundFiles.length == 1) {
File newFileLocation = new File(jarDirectory + File.separator + newName);
if (newFileLocation.exists()) {
if (!newFileLocation.delete()) {
throw new IllegalArgumentException("Unable to delete previously built .jar");
}
}
if (!foundFiles[0].renameTo(newFileLocation)) {
throw new IllegalArgumentException("Unable to move built .jar");
}
}
}
/**
* Starts the build process and initializes
* @param processBuilder <p>The process builder to execute</p>
*/
private void executeBuildProcess(ProcessBuilder processBuilder) {
processBuilder.directory(new File(buildDirectory));
processBuilder.redirectErrorStream(true);
try {
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) {
e.printStackTrace();
}
}
/**
* Gets the latest build tools version available
* @return <p>The latest build tools version available</p>
* @throws IOException <p>If unable to read the version file</p>
*/
String getLatestBuildToolsVersion() throws IOException {
String versionDocument;
versionDocument = CommonFunctions.readFile("https://hub.spigotmc.org/jenkins/job/BuildTools/lastSuccessfulBuild/");
return CommonFunctions.stringBetween(versionDocument, "BuildTools #", " [");
}
}