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:
Kristian Knarvik 2021-06-11 20:41:30 +02:00
parent 8cdb1f143c
commit 123a8eddda
10 changed files with 245 additions and 21 deletions

1
.gitignore vendored
View File

@ -28,4 +28,5 @@ hs_err_pid*
bin/
out/
.idea
*.txt

View File

@ -126,9 +126,9 @@
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>

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 #", " [");
}
}

View File

@ -1,11 +1,11 @@
package net.knarcraft.minecraftserverlauncher.server;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.naming.ConfigurationException;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
public class ServerTest {

View File

@ -1,13 +1,13 @@
package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import javax.naming.ConfigurationException;
import java.util.Arrays;
import static junit.framework.TestCase.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ServerTypeHandlerTest {

View File

@ -2,13 +2,13 @@ package net.knarcraft.minecraftserverlauncher.server;
import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.*;
import static junit.framework.TestCase.assertEquals;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.createAllFolders;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ServerVersionContainerTest {
@ -16,7 +16,7 @@ public class ServerVersionContainerTest {
private final String versionFile = filesDirectory + File.separator + "versions.csv";
private ServerVersionContainer serverVersionContainer;
@Before
@BeforeEach
public void setUp() {
try {
createAllFolders();

View File

@ -1,10 +1,10 @@
package net.knarcraft.minecraftserverlauncher.utility;
import net.knarcraft.minecraftserverlauncher.Main;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static net.knarcraft.minecraftserverlauncher.utility.CommonFunctions.stringBetween;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CommonFunctionsTest {

View File

@ -0,0 +1,79 @@
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;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.io.File;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class JarBuilderTest {
private static JarBuilder jarBuilder;
private static String targetDirectory;
private static String jarDirectory;
@BeforeEach
public void setUp() {
targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"BuildTools" + File.separator;
jarDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"testjars" + File.separator;
jarBuilder = new JarBuilder(targetDirectory, jarDirectory);
removeBuildToolsFiles();
}
@Test
@Order(3)
public void buildLatestSpigotJarTest() {
jarBuilder.buildSpigotJar();
assertTrue(new File(jarDirectory + "SpigotLatest.jar").exists());
}
@Test
@Order(4)
public void buildLatestBukkitJarTest() {
jarBuilder.buildBukkitJar();
assertTrue(new File(jarDirectory + "BukkitLatest.jar").exists());
}
@Test
@Order(2)
public void downloadLatestBuildToolsJarTest() {
jarBuilder.downloadBuildTools();
assertTrue(new File(targetDirectory + "BuildTools.jar").exists());
}
@Test
@Order(1)
public void getLatestBuildToolsVersionTest() {
try {
String latestVersion = jarBuilder.getLatestBuildToolsVersion();
assertNotEquals("", latestVersion);
int newVersion = Integer.parseInt(latestVersion);
assertNotEquals(newVersion, 0);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Removes build tools files
*/
private static void removeBuildToolsFiles() {
File target = new File(targetDirectory);
if (!target.exists() && !target.mkdirs()) {
throw new IllegalArgumentException("Unable to create the test files directory");
}
CommonFunctions.removeFilesRecursively(target);
}
}

View File

@ -4,28 +4,29 @@ import net.knarcraft.minecraftserverlauncher.Main;
import net.knarcraft.minecraftserverlauncher.server.ServerTypeHandler;
import net.knarcraft.minecraftserverlauncher.server.servertypes.ServerType;
import net.knarcraft.minecraftserverlauncher.userinterface.FakeGUI;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import javax.naming.ConfigurationException;
import java.io.File;
import java.io.IOException;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class JarDownloaderTest {
private static String targetDirectory;
@BeforeClass
@BeforeAll
public static void setUp() {
targetDirectory = Main.getApplicationWorkDirectory() + File.separator + "files" + File.separator +
"testjars" + File.separator;
removeDownloadedFiles();
}
@AfterClass
@AfterAll
public static void cleanUp() {
removeDownloadedFiles();
}