diff --git a/pom.xml b/pom.xml
index 49cb129..808b213 100644
--- a/pom.xml
+++ b/pom.xml
@@ -7,7 +7,7 @@
org.strassburger
mcrun-maven-plugin
- 0.0.12
+ 0.0.13
maven-plugin
MCRun Maven Plugin
diff --git a/src/main/java/org/strassburger/mcrun/RunServerMojo.java b/src/main/java/org/strassburger/mcrun/StartServerMojo.java
similarity index 51%
rename from src/main/java/org/strassburger/mcrun/RunServerMojo.java
rename to src/main/java/org/strassburger/mcrun/StartServerMojo.java
index d7d9f16..c7ed8e2 100644
--- a/src/main/java/org/strassburger/mcrun/RunServerMojo.java
+++ b/src/main/java/org/strassburger/mcrun/StartServerMojo.java
@@ -6,18 +6,20 @@ 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 org.strassburger.mcrun.config.ServerInstallConfig;
+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.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 {
+@Mojo(name = "start", defaultPhase = LifecyclePhase.PACKAGE)
+public class StartServerMojo 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.
@@ -100,79 +102,23 @@ public class RunServerMojo extends AbstractMojo {
@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);
+ for (var step : getSteps()) {
+ step.run();
+ }
+ } catch (StepExecutionException e) {
+ throw new MojoExecutionException(e.getMessage(), e.getCause());
}
}
- 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 buildCommand() {
- List 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 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);
- }
+ private List> getSteps() {
+ List> steps = new ArrayList<>();
+ steps.add(new ServerInstallStep(getLog(),
+ new ServerInstallConfig(serverDirectory, pluginJar, serverJarName, serverSoftware, mcVersion, acceptEula)
+ ));
+ steps.add(new ServerRunStep(getLog(),
+ new ServerRunStepConfig(memory, jvmArgs, remoteDebug, debugPort, port, serverDirectory, serverJarName)
+ ));
+ return steps;
}
}
\ No newline at end of file
diff --git a/src/main/java/org/strassburger/mcrun/config/ServerInstallConfig.java b/src/main/java/org/strassburger/mcrun/config/ServerInstallConfig.java
new file mode 100644
index 0000000..bda5be1
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/config/ServerInstallConfig.java
@@ -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
+) {
+}
diff --git a/src/main/java/org/strassburger/mcrun/config/ServerRunStepConfig.java b/src/main/java/org/strassburger/mcrun/config/ServerRunStepConfig.java
new file mode 100644
index 0000000..0ae0642
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/config/ServerRunStepConfig.java
@@ -0,0 +1,13 @@
+package org.strassburger.mcrun.config;
+
+import java.io.File;
+import java.util.List;
+
+public record ServerRunStepConfig(String memory,
+ List jvmArgs,
+ boolean remoteDebug,
+ int debugPort,
+ Integer port,
+ File serverDirectory,
+ String serverJarName) {
+}
diff --git a/src/main/java/org/strassburger/mcrun/steps/ServerInstallStep.java b/src/main/java/org/strassburger/mcrun/steps/ServerInstallStep.java
new file mode 100644
index 0000000..ecf968c
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/steps/ServerInstallStep.java
@@ -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 {
+ 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);
+ }
+ }
+}
diff --git a/src/main/java/org/strassburger/mcrun/steps/ServerRunStep.java b/src/main/java/org/strassburger/mcrun/steps/ServerRunStep.java
new file mode 100644
index 0000000..f8f96e0
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/steps/ServerRunStep.java
@@ -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 {
+ public ServerRunStep(Log log, ServerRunStepConfig config) {
+ super(log, config);
+ }
+
+ @Override
+ public void run() throws StepExecutionException {
+ startServerProcess();
+ }
+
+ private List buildCommand() {
+ List 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 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);
+ }
+ }
+}
diff --git a/src/main/java/org/strassburger/mcrun/steps/Step.java b/src/main/java/org/strassburger/mcrun/steps/Step.java
new file mode 100644
index 0000000..ad7bd69
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/steps/Step.java
@@ -0,0 +1,23 @@
+package org.strassburger.mcrun.steps;
+
+import org.apache.maven.plugin.logging.Log;
+
+public abstract class Step {
+ 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;
+ }
+}
diff --git a/src/main/java/org/strassburger/mcrun/steps/StepExecutionException.java b/src/main/java/org/strassburger/mcrun/steps/StepExecutionException.java
new file mode 100644
index 0000000..5b12739
--- /dev/null
+++ b/src/main/java/org/strassburger/mcrun/steps/StepExecutionException.java
@@ -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);
+ }
+}