Initial commit

This commit is contained in:
2026-05-23 23:13:12 +02:00
commit e58680b78b
12 changed files with 621 additions and 0 deletions
@@ -0,0 +1,95 @@
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.PaperServerJarDownloader;
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;
@Mojo(name = "run", defaultPhase = LifecyclePhase.PACKAGE)
public class RunServerMojo extends AbstractMojo {
@Parameter(defaultValue = "${project.build.directory}/${project.build.finalName}.jar")
private File pluginJar;
@Parameter(defaultValue = "${project.basedir}/server")
private File serverDirectory;
@Parameter(defaultValue = "server.jar")
private String serverJarName;
@Parameter(defaultValue = "paper")
private String serverSoftware;
@Parameter(required = true)
private String mcVersion;
@Parameter(defaultValue = "2G")
private String memory;
@Parameter(defaultValue = "true")
private boolean acceptEula;
@Override
public void execute() throws MojoExecutionException, MojoFailureException {
getLog().info("Server directory: " + serverDirectory.getAbsolutePath());
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);
}
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);
}
if (acceptEula) acceptEula();
ProcessBuilder pb = new ProcessBuilder(
"java", "-Xmx" + memory, "-Xms" + memory,
"-jar", serverJarName, "--nogui"
);
pb.directory(serverDirectory);
pb.inheritIO();
Process process;
try {
process = pb.start();
} catch (IOException e) {
throw new MojoExecutionException("Failed to start server: " + e.getMessage(), e);
}
try {
process.waitFor();
} catch (InterruptedException e) {
throw new MojoExecutionException("Failed to start server: " + e.getMessage(), 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);
}
}
}