Split up code into steps
This commit is contained in:
@@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
<groupId>org.strassburger</groupId>
|
<groupId>org.strassburger</groupId>
|
||||||
<artifactId>mcrun-maven-plugin</artifactId>
|
<artifactId>mcrun-maven-plugin</artifactId>
|
||||||
<version>0.0.12</version>
|
<version>0.0.13</version>
|
||||||
<packaging>maven-plugin</packaging>
|
<packaging>maven-plugin</packaging>
|
||||||
|
|
||||||
<name>MCRun Maven Plugin</name>
|
<name>MCRun Maven Plugin</name>
|
||||||
|
|||||||
+22
-76
@@ -6,18 +6,20 @@ import org.apache.maven.plugin.MojoFailureException;
|
|||||||
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
import org.apache.maven.plugins.annotations.LifecyclePhase;
|
||||||
import org.apache.maven.plugins.annotations.Mojo;
|
import org.apache.maven.plugins.annotations.Mojo;
|
||||||
import org.apache.maven.plugins.annotations.Parameter;
|
import org.apache.maven.plugins.annotations.Parameter;
|
||||||
import org.strassburger.mcrun.downloader.ServerJarDownloadException;
|
import org.strassburger.mcrun.config.ServerInstallConfig;
|
||||||
import org.strassburger.mcrun.downloader.ServerJarDownloader;
|
import org.strassburger.mcrun.config.ServerRunStepConfig;
|
||||||
|
import org.strassburger.mcrun.steps.ServerInstallStep;
|
||||||
|
import org.strassburger.mcrun.steps.ServerRunStep;
|
||||||
|
import org.strassburger.mcrun.steps.Step;
|
||||||
|
import org.strassburger.mcrun.steps.StepExecutionException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE)
|
@Mojo(name = "start", defaultPhase = LifecyclePhase.PACKAGE)
|
||||||
public class RunServerMojo extends AbstractMojo {
|
public class StartServerMojo extends AbstractMojo {
|
||||||
/**
|
/**
|
||||||
* Path to the plugin jar to deploy. Defaults to the project's build output jar.
|
* 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.
|
* Override if your jar is assembled elsewhere or has a classifier.
|
||||||
@@ -100,79 +102,23 @@ public class RunServerMojo extends AbstractMojo {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute() throws MojoExecutionException, MojoFailureException {
|
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 {
|
try {
|
||||||
Files.copy(pluginJar.toPath(),
|
for (var step : getSteps()) {
|
||||||
new File(pluginsDir, pluginJar.getName()).toPath(),
|
step.run();
|
||||||
StandardCopyOption.REPLACE_EXISTING);
|
}
|
||||||
} catch (IOException e) {
|
} catch (StepExecutionException e) {
|
||||||
throw new MojoExecutionException("Failed to copy plugin jar file: " + pluginJar.getAbsolutePath(), e);
|
throw new MojoExecutionException(e.getMessage(), e.getCause());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void downloadServerJar() throws MojoExecutionException {
|
private List<Step<?>> getSteps() {
|
||||||
try {
|
List<Step<?>> steps = new ArrayList<>();
|
||||||
ServerJarDownloader jarDownloader = ServerJarDownloader.forServerSoftware(serverSoftware)
|
steps.add(new ServerInstallStep(getLog(),
|
||||||
.withLog(getLog()).withServerDirectory(serverDirectory)
|
new ServerInstallConfig(serverDirectory, pluginJar, serverJarName, serverSoftware, mcVersion, acceptEula)
|
||||||
.withServerJarName(serverJarName).withMcVersion(mcVersion)
|
));
|
||||||
.build();
|
steps.add(new ServerRunStep(getLog(),
|
||||||
jarDownloader.run();
|
new ServerRunStepConfig(memory, jvmArgs, remoteDebug, debugPort, port, serverDirectory, serverJarName)
|
||||||
} catch (ServerJarDownloadException e) {
|
));
|
||||||
throw new MojoExecutionException("Failed to download server jar", e);
|
return steps;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package org.strassburger.mcrun.config;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
public record ServerInstallConfig(File serverDirectory,
|
||||||
|
File pluginJar,
|
||||||
|
String serverJarName,
|
||||||
|
String serverSoftware,
|
||||||
|
String mcVersion,
|
||||||
|
boolean acceptEula
|
||||||
|
) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package org.strassburger.mcrun.config;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public record ServerRunStepConfig(String memory,
|
||||||
|
List<String> jvmArgs,
|
||||||
|
boolean remoteDebug,
|
||||||
|
int debugPort,
|
||||||
|
Integer port,
|
||||||
|
File serverDirectory,
|
||||||
|
String serverJarName) {
|
||||||
|
}
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package org.strassburger.mcrun.steps;
|
||||||
|
|
||||||
|
import org.apache.maven.plugin.logging.Log;
|
||||||
|
import org.strassburger.mcrun.config.ServerInstallConfig;
|
||||||
|
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;
|
||||||
|
|
||||||
|
public class ServerInstallStep extends Step<ServerInstallConfig> {
|
||||||
|
public ServerInstallStep(Log log, ServerInstallConfig config) {
|
||||||
|
super(log, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() throws StepExecutionException {
|
||||||
|
copyPlugin();
|
||||||
|
downloadServerJar();
|
||||||
|
acceptEula();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void copyPlugin() throws StepExecutionException {
|
||||||
|
File pluginsDir = new File(getConfig().serverDirectory(), "plugins");
|
||||||
|
pluginsDir.mkdirs();
|
||||||
|
try {
|
||||||
|
Files.copy(getConfig().pluginJar().toPath(),
|
||||||
|
new File(pluginsDir, getConfig().pluginJar().getName()).toPath(),
|
||||||
|
StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new StepExecutionException("Failed to copy plugin jar file: " + getConfig().pluginJar().getAbsolutePath(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void downloadServerJar() throws StepExecutionException {
|
||||||
|
try {
|
||||||
|
ServerJarDownloader jarDownloader = ServerJarDownloader.forServerSoftware(getConfig().serverSoftware())
|
||||||
|
.withLog(getLog()).withServerDirectory(getConfig().serverDirectory())
|
||||||
|
.withServerJarName(getConfig().serverJarName()).withMcVersion(getConfig().mcVersion())
|
||||||
|
.build();
|
||||||
|
jarDownloader.run();
|
||||||
|
} catch (ServerJarDownloadException e) {
|
||||||
|
throw new StepExecutionException("Failed to download server jar", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void acceptEula() throws StepExecutionException {
|
||||||
|
if (!getConfig().acceptEula()) return;
|
||||||
|
File eula = new File(getConfig().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 StepExecutionException("Failed to write eula.txt: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package org.strassburger.mcrun.steps;
|
||||||
|
|
||||||
|
import org.apache.maven.plugin.logging.Log;
|
||||||
|
import org.strassburger.mcrun.config.ServerRunStepConfig;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ServerRunStep extends Step<ServerRunStepConfig> {
|
||||||
|
public ServerRunStep(Log log, ServerRunStepConfig config) {
|
||||||
|
super(log, config);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() throws StepExecutionException {
|
||||||
|
startServerProcess();
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<String> buildCommand() {
|
||||||
|
List<String> cmd = new ArrayList<>();
|
||||||
|
cmd.add("java");
|
||||||
|
cmd.add("-Xmx" + getConfig().memory());
|
||||||
|
cmd.add("-Xms" + getConfig().memory());
|
||||||
|
if (getConfig().remoteDebug() && getConfig().debugPort() > 0) {
|
||||||
|
cmd.add(String.format("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:%d", getConfig().debugPort()));
|
||||||
|
}
|
||||||
|
cmd.addAll(getConfig().jvmArgs());
|
||||||
|
cmd.add("-jar");
|
||||||
|
cmd.add(getConfig().serverJarName());
|
||||||
|
cmd.add("--nogui");
|
||||||
|
if (getConfig().port() != null) {
|
||||||
|
cmd.add("--port");
|
||||||
|
cmd.add(String.valueOf(getConfig().port()));
|
||||||
|
}
|
||||||
|
return cmd;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void startServerProcess() throws StepExecutionException {
|
||||||
|
List<String> command = buildCommand();
|
||||||
|
getLog().info("Starting server with command: " + String.join(" ", command));
|
||||||
|
|
||||||
|
ProcessBuilder pb = new ProcessBuilder(command);
|
||||||
|
pb.directory(getConfig().serverDirectory());
|
||||||
|
pb.inheritIO();
|
||||||
|
Process process;
|
||||||
|
try {
|
||||||
|
process = pb.start();
|
||||||
|
process.waitFor();
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
throw new StepExecutionException("Failed to start server: " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package org.strassburger.mcrun.steps;
|
||||||
|
|
||||||
|
import org.apache.maven.plugin.logging.Log;
|
||||||
|
|
||||||
|
public abstract class Step<T> {
|
||||||
|
private final Log log;
|
||||||
|
private final T config;
|
||||||
|
|
||||||
|
public Step(Log log, T config) {
|
||||||
|
this.log = log;
|
||||||
|
this.config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void run() throws StepExecutionException;
|
||||||
|
|
||||||
|
protected Log getLog() {
|
||||||
|
return log;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected T getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package org.strassburger.mcrun.steps;
|
||||||
|
|
||||||
|
public class StepExecutionException extends RuntimeException {
|
||||||
|
public StepExecutionException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StepExecutionException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user