diff --git a/src/common/java/net/minecraftforge/gradle/common/util/Utils.java b/src/common/java/net/minecraftforge/gradle/common/util/Utils.java index 4505ce960..22f6b9c22 100644 --- a/src/common/java/net/minecraftforge/gradle/common/util/Utils.java +++ b/src/common/java/net/minecraftforge/gradle/common/util/Utils.java @@ -144,6 +144,12 @@ public static byte[] base64DecodeStringList(List strings) throws IOExcep return bos.toByteArray(); } + public static void mkdirs(File file) { + if (!file.exists() && !file.mkdirs()) { + throw new RuntimeException("Could not create directory: " + file.getAbsolutePath()); + } + } + public static File delete(File file) { if (!file.getParentFile().exists()) file.getParentFile().mkdirs(); if (file.exists()) file.delete(); diff --git a/src/common/java/net/minecraftforge/gradle/common/util/runs/MinecraftRunTask.java b/src/common/java/net/minecraftforge/gradle/common/util/runs/MinecraftRunTask.java deleted file mode 100644 index 7faa1e626..000000000 --- a/src/common/java/net/minecraftforge/gradle/common/util/runs/MinecraftRunTask.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (c) Forge Development LLC and contributors - * SPDX-License-Identifier: LGPL-2.1-only - */ - -package net.minecraftforge.gradle.common.util.runs; - -import net.minecraftforge.gradle.common.util.RunConfig; -import org.gradle.api.Project; -import org.gradle.api.file.ConfigurableFileCollection; -import org.gradle.api.plugins.JavaPluginExtension; -import org.gradle.api.provider.ListProperty; -import org.gradle.api.provider.Property; -import org.gradle.api.tasks.Input; -import org.gradle.api.tasks.InputFiles; -import org.gradle.api.tasks.JavaExec; -import org.gradle.api.tasks.SourceSet; -import org.gradle.api.tasks.TaskAction; -import org.gradle.work.DisableCachingByDefault; - -import java.io.File; -import java.util.Map; -import java.util.function.Supplier; - -@DisableCachingByDefault(because = "Running Minecraft cannot be cached") -abstract class MinecraftRunTask extends JavaExec { - public MinecraftRunTask() { - this.setGroup(RunConfig.RUNS_GROUP); - this.setImpliesSubProjects(true); // Running the game in the current project and child projects is a bad idea - - this.getJavaLauncher().convention(this.getJavaToolchainService().launcherFor(this.getProject().getExtensions().getByType(JavaPluginExtension.class).getToolchain())); - } - - @TaskAction - @Override - public void exec() { - Project project = this.getProject(); - RunConfig runConfig = this.getRunConfig().get(); - File workDir = prepareWorkingDirectory(runConfig); - - Map> updatedTokens = RunConfigGenerator.configureTokensLazy(project, runConfig, - RunConfigGenerator.mapModClassesToGradle(project, runConfig), - this.getMinecraftArtifacts().getFiles(), this.getRuntimeClasspathArtifacts().getFiles()); - - this.setWorkingDir(workDir); - this.args(RunConfigGenerator.getArgsStream(runConfig, updatedTokens, false).toArray()); - runConfig.getJvmArgs().forEach(arg -> this.jvmArgs(runConfig.replace(updatedTokens, arg))); - if (runConfig.isClient()) { - getAdditionalClientArgs().get().forEach(arg -> this.jvmArgs(runConfig.replace(updatedTokens, arg))); - } - runConfig.getEnvironment().forEach((key, value) -> this.environment(key, runConfig.replace(updatedTokens, value))); - runConfig.getProperties().forEach((key, value) -> this.systemProperty(key, runConfig.replace(updatedTokens, value))); - - runConfig.getAllSources().stream().map(SourceSet::getRuntimeClasspath).forEach(this::classpath); - - super.exec(); - } - - public static File prepareWorkingDirectory(RunConfig runConfig) { - File workDir = new File(runConfig.getWorkingDirectory()); - - if (!workDir.exists() && !workDir.mkdirs()) - throw new RuntimeException("Could not create working directory: " + workDir.getAbsolutePath()); - - return workDir; - } - - @Input - public abstract Property getRunConfig(); - - @Input - public abstract ListProperty getAdditionalClientArgs(); - - @InputFiles - public abstract ConfigurableFileCollection getMinecraftArtifacts(); - - @InputFiles - public abstract ConfigurableFileCollection getRuntimeClasspathArtifacts(); -} diff --git a/src/common/java/net/minecraftforge/gradle/common/util/runs/RunConfigGenerator.java b/src/common/java/net/minecraftforge/gradle/common/util/runs/RunConfigGenerator.java index 6789b3c5d..64c71e6e2 100644 --- a/src/common/java/net/minecraftforge/gradle/common/util/runs/RunConfigGenerator.java +++ b/src/common/java/net/minecraftforge/gradle/common/util/runs/RunConfigGenerator.java @@ -9,6 +9,7 @@ import net.minecraftforge.gradle.common.util.MinecraftExtension; import net.minecraftforge.gradle.common.util.RunConfig; +import net.minecraftforge.gradle.common.util.Utils; import org.apache.commons.io.IOUtils; import org.gradle.api.Project; import org.gradle.api.Task; @@ -16,8 +17,10 @@ import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.file.FileCollection; import org.gradle.api.plugins.JavaPluginExtension; +import org.gradle.api.tasks.JavaExec; import org.gradle.api.tasks.SourceSet; import org.gradle.api.tasks.TaskProvider; +import org.gradle.jvm.toolchain.JavaToolchainService; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -206,24 +209,45 @@ protected static FileCollection getRuntimeClasspathArtifacts(final Project proje return getArtifactFiles(project.getConfigurations().getByName("runtimeClasspath")); } - public static TaskProvider createRunTask(final RunConfig runConfig, final Project project, final TaskProvider prepareRuns, final List additionalClientArgs) { + public static TaskProvider createRunTask(final RunConfig runConfig, final Project project, final TaskProvider prepareRuns, final List additionalClientArgs) { TaskProvider prepareRun = project.getTasks().register(runConfig.getPrepareTaskName(), MinecraftPrepareRunTask.class, task -> task.dependsOn(prepareRuns)); TaskProvider prepareRunCompile = project.getTasks().register(runConfig.getPrepareCompileTaskName(), MinecraftPrepareRunTask.class, task -> task.dependsOn(runConfig.getAllSources().stream().map(SourceSet::getClassesTaskName).toArray())); - return project.getTasks().register(runConfig.getTaskName(), MinecraftRunTask.class, task -> { + return project.getTasks().register(runConfig.getTaskName(), JavaExec.class, task -> { task.setGroup(RunConfig.RUNS_GROUP); + task.setImpliesSubProjects(true); // Running the game in the current project and child projects is a bad idea task.dependsOn(prepareRun, prepareRunCompile); // The actual run task should always build sources regardless of if prepareRunXXX does task.dependsOn(runConfig.getAllSources().stream().map(SourceSet::getClassesTaskName).toArray()); - task.getRunConfig().set(runConfig); + // Use the same Java launcher as the project + task.getJavaLauncher().convention( + project.getExtensions().getByType(JavaToolchainService.class).launcherFor( + project.getExtensions().getByType(JavaPluginExtension.class).getToolchain() + ) + ); + + task.setWorkingDir(runConfig.getWorkingDirectory()); + task.doFirst("Create working directory", t -> Utils.mkdirs(task.getWorkingDir())); + task.getMainClass().set(runConfig.getMain()); - task.getAdditionalClientArgs().set(additionalClientArgs); - task.getMinecraftArtifacts().from(getMinecraftArtifacts(project)); - task.getRuntimeClasspathArtifacts().from(getRuntimeClasspathArtifacts(project)); + + Map> updatedTokens = RunConfigGenerator.configureTokensLazy(project, runConfig, + RunConfigGenerator.mapModClassesToGradle(project, runConfig), + getMinecraftArtifacts(project).getFiles(), getRuntimeClasspathArtifacts(project).getFiles()); + + task.args(RunConfigGenerator.getArgsStream(runConfig, updatedTokens, false).toArray()); + runConfig.getJvmArgs().forEach(arg -> task.jvmArgs(runConfig.replace(updatedTokens, arg))); + if (runConfig.isClient()) { + additionalClientArgs.forEach(arg -> task.jvmArgs(runConfig.replace(updatedTokens, arg))); + } + runConfig.getEnvironment().forEach((key, value) -> task.environment(key, runConfig.replace(updatedTokens, value))); + runConfig.getProperties().forEach((key, value) -> task.systemProperty(key, runConfig.replace(updatedTokens, value))); + + runConfig.getAllSources().stream().map(SourceSet::getRuntimeClasspath).forEach(task::classpath); }); } @@ -280,7 +304,7 @@ public final void createRunConfiguration(final MinecraftExtension minecraft, fin transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); minecraft.getRuns().forEach(runConfig -> { - MinecraftRunTask.prepareWorkingDirectory(runConfig); + Utils.mkdirs(new File(runConfig.getWorkingDirectory())); final Map documents = createRunConfiguration(minecraft, project, runConfig, docBuilder, additionalClientArgs, minecraftArtifacts, runtimeClasspathArtifacts); @@ -315,7 +339,7 @@ public final void createRunConfiguration(final MinecraftExtension minecraft, fin rootObject.addProperty("version", "0.2.0"); JsonArray runConfigs = new JsonArray(); minecraft.getRuns().forEach(runConfig -> { - MinecraftRunTask.prepareWorkingDirectory(runConfig); + Utils.mkdirs(new File(runConfig.getWorkingDirectory())); runConfigs.add(createRunConfiguration(project, runConfig, additionalClientArgs, minecraftArtifacts, runtimeClasspathArtifacts)); });