Split up code into steps
This commit is contained in:
@@ -1,178 +0,0 @@
|
||||
package org.strassburger.mcrun;
|
||||
|
||||
import org.apache.maven.plugin.AbstractMojo;
|
||||
import org.apache.maven.plugin.MojoExecutionException;
|
||||
import org.apache.maven.plugin.MojoFailureException;
|
||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||
import org.apache.maven.plugins.annotations.Mojo;
|
||||
import org.apache.maven.plugins.annotations.Parameter;
|
||||
import org.strassburger.mcrun.downloader.ServerJarDownloadException;
|
||||
import org.strassburger.mcrun.downloader.ServerJarDownloader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE)
|
||||
public class RunServerMojo extends AbstractMojo {
|
||||
/**
|
||||
* Path to the plugin jar to deploy. Defaults to the project's build output jar.
|
||||
* Override if your jar is assembled elsewhere or has a classifier.
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
|
||||
private File pluginJar;
|
||||
|
||||
/**
|
||||
* Directory where the test server will be created and run.
|
||||
* The server jar, plugins folder, and all generated files (eula.txt, world, logs, etc.)
|
||||
* will be placed here. Defaults to <code>${project.basedir}/server</code>.
|
||||
*/
|
||||
@Parameter(defaultValue = "${project.basedir}/server")
|
||||
private File serverDirectory;
|
||||
|
||||
/**
|
||||
* File name for the downloaded or provided server jar, relative to {@code serverDirectory}.
|
||||
* Defaults to <code>server.jar</code>.
|
||||
*/
|
||||
@Parameter(defaultValue = "server.jar")
|
||||
private String serverJarName;
|
||||
|
||||
/**
|
||||
* The server software to download. Defaults to <code>paper</code>.
|
||||
* Can be one <code>paper</code> or <code>spigot</code>.
|
||||
*/
|
||||
@Parameter(defaultValue = "paper")
|
||||
private String serverSoftware;
|
||||
|
||||
/**
|
||||
* The Minecraft version to run, e.g. <code>1.21.4</code>. Required.
|
||||
*/
|
||||
@Parameter(required = true)
|
||||
private String mcVersion;
|
||||
|
||||
/**
|
||||
* Heap size passed to both {@code -Xmx} and {@code -Xms}.
|
||||
* Accepts standard JVM memory notation, e.g. <code>2G</code>, <code>512M</code>.
|
||||
* Defaults to <code>2G</code>.
|
||||
*/
|
||||
@Parameter(defaultValue = "2G")
|
||||
private String memory;
|
||||
|
||||
/**
|
||||
* When {@code true}, automatically creates {@code eula.txt} with {@code eula=true}
|
||||
* in the server directory if it does not already exist, bypassing the manual
|
||||
* acceptance step. Defaults to {@code false}.
|
||||
*
|
||||
* <p>By setting this to {@code true} you are indicating that you accept the
|
||||
* <a href="https://aka.ms/MinecraftEULA">Minecraft End User License Agreement</a>.</p>
|
||||
*/
|
||||
@Parameter(defaultValue = "false")
|
||||
private boolean acceptEula;
|
||||
|
||||
/**
|
||||
* Additional JVM arguments to pass to the server process.
|
||||
* These are inserted after -Xmx/-Xms and before -jar.
|
||||
*
|
||||
* <pre>{@code
|
||||
* <jvmArgs>
|
||||
* <arg>-XX:+UseG1GC</arg>
|
||||
* <arg>-XX:+ParallelRefProcEnabled</arg>
|
||||
* </jvmArgs>
|
||||
* }</pre>
|
||||
*/
|
||||
@Parameter
|
||||
private List<String> jvmArgs = new ArrayList<>();
|
||||
|
||||
@Parameter(defaultValue = "false")
|
||||
private boolean remoteDebug;
|
||||
|
||||
@Parameter(defaultValue = "5005")
|
||||
private int debugPort;
|
||||
|
||||
/**
|
||||
* The port the Minecraft server will listen on. Passed to the server via --port.
|
||||
*/
|
||||
@Parameter
|
||||
private Integer port;
|
||||
|
||||
@Override
|
||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
||||
copyPlugin();
|
||||
downloadServerJar();
|
||||
if (acceptEula) acceptEula();
|
||||
startServerProcess();
|
||||
}
|
||||
|
||||
private void copyPlugin() throws MojoExecutionException {
|
||||
File pluginsDir = new File(serverDirectory, "plugins");
|
||||
pluginsDir.mkdirs();
|
||||
try {
|
||||
Files.copy(pluginJar.toPath(),
|
||||
new File(pluginsDir, pluginJar.getName()).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new MojoExecutionException("Failed to copy plugin jar file: " + pluginJar.getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadServerJar() throws MojoExecutionException {
|
||||
try {
|
||||
ServerJarDownloader jarDownloader = ServerJarDownloader.forServerSoftware(serverSoftware)
|
||||
.withLog(getLog()).withServerDirectory(serverDirectory)
|
||||
.withServerJarName(serverJarName).withMcVersion(mcVersion)
|
||||
.build();
|
||||
jarDownloader.run();
|
||||
} catch (ServerJarDownloadException e) {
|
||||
throw new MojoExecutionException("Failed to download server jar", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptEula() throws MojoExecutionException {
|
||||
File eula = new File(serverDirectory, "eula.txt");
|
||||
if (eula.exists()) return;
|
||||
try {
|
||||
Files.writeString(eula.toPath(), "eula=true\n");
|
||||
getLog().info("eula.txt created and accepted.");
|
||||
} catch (IOException e) {
|
||||
throw new MojoExecutionException("Failed to write eula.txt: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> buildCommand() {
|
||||
List<String> cmd = new ArrayList<>();
|
||||
cmd.add("java");
|
||||
cmd.add("-Xmx" + memory);
|
||||
cmd.add("-Xms" + memory);
|
||||
if (remoteDebug && debugPort > 0) {
|
||||
cmd.add(String.format("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:%d", debugPort));
|
||||
}
|
||||
cmd.addAll(jvmArgs);
|
||||
cmd.add("-jar");
|
||||
cmd.add(serverJarName);
|
||||
cmd.add("--nogui");
|
||||
if (port != null) {
|
||||
cmd.add("--port");
|
||||
cmd.add(String.valueOf(port));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
private void startServerProcess() throws MojoExecutionException {
|
||||
List<String> command = buildCommand();
|
||||
getLog().info("Starting server with command: " + String.join(" ", command));
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(command);
|
||||
pb.directory(serverDirectory);
|
||||
pb.inheritIO();
|
||||
Process process;
|
||||
try {
|
||||
process = pb.start();
|
||||
process.waitFor();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new MojoExecutionException("Failed to start server: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user