diff --git a/README.md b/README.md index 167caff4..1f5c7fa7 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,12 @@ neoForge { ] systemProperty("a.b.c", "xyz") + // Set or add environment variables + environment = [ + "FOO_BAR": "123" + ] + environment("FOO_BAR", "123") + // Optionally set the log-level used by the game logLevel = org.slf4j.event.Level.DEBUG diff --git a/src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java b/src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java index 65364170..46a7ed28 100644 --- a/src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java +++ b/src/main/java/net/neoforged/moddevgradle/dsl/RunModel.java @@ -60,6 +60,12 @@ public String getName() { public abstract DirectoryProperty getGameDirectory(); + public abstract MapProperty getEnvironment(); + + public void environment(String key, String value) { + getEnvironment().put(key, value); + } + public abstract MapProperty getSystemProperties(); public void systemProperty(String key, String value) { diff --git a/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java b/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java index d65a123d..ae5bc835 100644 --- a/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java +++ b/src/main/java/net/neoforged/moddevgradle/internal/ModDevPlugin.java @@ -313,6 +313,7 @@ public void apply(Project project) { task.getClasspathProvider().from(sourceSet.getRuntimeClasspath()); task.getGameDirectory().set(run.getGameDirectory()); + task.getEnvironmentProperty().set(run.getEnvironment()); task.jvmArgs(RunUtils.getArgFileParameter(prepareRunTask.get().getVmArgsFile().get()).replace("\\", "\\\\")); task.getMainClass().set(RunUtils.DEV_LAUNCH_MAIN_CLASS); task.args(RunUtils.getArgFileParameter(prepareRunTask.get().getProgramArgsFile().get()).replace("\\", "\\\\")); @@ -645,6 +646,7 @@ private static void addIntelliJRunConfiguration(Project project, var sourceSets = ExtensionUtils.getExtension(project, "sourceSets", SourceSetContainer.class); appRun.setModuleRef(new ModuleRef(project, sourceSets.getByName("main"))); appRun.setWorkingDirectory(run.getGameDirectory().get().getAsFile().getAbsolutePath()); + appRun.setEnvs(run.getEnvironment().get()); appRun.setJvmArgs( RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(prepareTask.getVmArgsFile().get())) @@ -817,6 +819,7 @@ private static void addEclipseLaunchConfiguration(Project project, RunUtils.escapeJvmArg(RunUtils.getIdeaModFoldersProvider(project, outputDirectory, run.getMods(), false).getArgument()) ) .args(RunUtils.escapeJvmArg(RunUtils.getArgFileParameter(prepareTask.getProgramArgsFile().get()))) + .envVar(run.getEnvironment().get()) .workingDirectory(run.getGameDirectory().get().getAsFile().getAbsolutePath()) .jreContainer("JavaSE-21") // TODO .build(RunUtils.DEV_LAUNCH_MAIN_CLASS); diff --git a/src/main/java/net/neoforged/moddevgradle/internal/RunGameTask.java b/src/main/java/net/neoforged/moddevgradle/internal/RunGameTask.java index 67cba302..88e70af8 100644 --- a/src/main/java/net/neoforged/moddevgradle/internal/RunGameTask.java +++ b/src/main/java/net/neoforged/moddevgradle/internal/RunGameTask.java @@ -2,7 +2,9 @@ import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.DirectoryProperty; +import org.gradle.api.provider.MapProperty; import org.gradle.api.tasks.Classpath; +import org.gradle.api.tasks.Input; import org.gradle.api.tasks.InputFiles; import org.gradle.api.tasks.Internal; import org.gradle.api.tasks.JavaExec; @@ -13,6 +15,7 @@ import java.io.IOException; import java.io.UncheckedIOException; import java.nio.file.Files; +import java.util.HashMap; /** * By extending JavaExec, we allow IntelliJ to automatically attach a debugger to the forked JVM, making @@ -24,6 +27,9 @@ public abstract class RunGameTask extends JavaExec { @InputFiles public abstract ConfigurableFileCollection getClasspathProvider(); + @Input + public abstract MapProperty getEnvironmentProperty(); + @Internal public abstract DirectoryProperty getGameDirectory(); @@ -41,6 +47,8 @@ public void exec() { throw new UncheckedIOException("Failed to create run directory", e); } + getEnvironment().putAll(getEnvironmentProperty().get()); + classpath(getClasspathProvider()); setWorkingDir(runDir); super.exec(); diff --git a/testproject/build.gradle b/testproject/build.gradle index 7dba33ed..d21b04cd 100644 --- a/testproject/build.gradle +++ b/testproject/build.gradle @@ -32,6 +32,7 @@ neoForge { } server { server() + environment("BLAH_BLUBB", "123"); } }