diff --git a/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java b/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java index 6b14f5bd..276c6d51 100644 --- a/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java +++ b/src/main/java/net/fabricmc/loom/task/AbstractRunTask.java @@ -25,20 +25,30 @@ package net.fabricmc.loom.task; import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; import org.gradle.api.Project; +import org.gradle.api.file.ConfigurableFileCollection; +import org.gradle.api.file.FileCollection; import org.gradle.api.specs.Spec; import org.gradle.api.tasks.JavaExec; +import org.jetbrains.annotations.NotNull; import net.fabricmc.loom.configuration.ide.RunConfig; import net.fabricmc.loom.util.Constants; public abstract class AbstractRunTask extends JavaExec { private final RunConfig config; + // We control the classpath, as we use a ArgFile to pass it over the command line: https://docs.oracle.com/javase/7/docs/technotes/tools/windows/javac.html#commandlineargfile + private final ConfigurableFileCollection classpath = getProject().getObjects().fileCollection(); public AbstractRunTask(Function configProvider) { super(); @@ -46,6 +56,9 @@ public abstract class AbstractRunTask extends JavaExec { this.config = configProvider.apply(getProject()); setClasspath(config.sourceSet.getRuntimeClasspath().filter(File::exists).filter(new LibraryFilter())); + // Pass an empty classpath to the super JavaExec. + super.setClasspath(getProject().files()); + args(config.programArgs); getMainClass().set(config.mainClass); } @@ -69,12 +82,46 @@ public abstract class AbstractRunTask extends JavaExec { @Override public List getJvmArgs() { - List superArgs = super.getJvmArgs(); - List args = new ArrayList<>(superArgs != null ? superArgs : Collections.emptyList()); + final List superArgs = super.getJvmArgs(); + final List args = new ArrayList<>(); + + final String content = "-classpath\n" + this.classpath.getFiles().stream() + .map(File::getAbsolutePath) + .collect(Collectors.joining(System.getProperty("path.separator"))); + + try { + final Path argsFile = Files.createTempFile("loom-classpath", ".args"); + Files.writeString(argsFile, content, StandardCharsets.UTF_8); + args.add("@" + argsFile.toAbsolutePath()); + } catch (IOException e) { + throw new UncheckedIOException("Failed to create classpath file", e); + } + + if (superArgs != null) { + args.addAll(superArgs); + } + args.addAll(config.vmArgs); return args; } + @Override + public @NotNull JavaExec setClasspath(@NotNull FileCollection classpath) { + this.classpath.setFrom(classpath); + return this; + } + + @Override + public @NotNull JavaExec classpath(Object @NotNull... paths) { + this.classpath.from(paths); + return this; + } + + @Override + public @NotNull FileCollection getClasspath() { + return this.classpath; + } + private class LibraryFilter implements Spec { private List excludedLibraryPaths = null; diff --git a/src/main/resources/eclipse_run_config_template.xml b/src/main/resources/eclipse_run_config_template.xml index 737e9cd3..a8c380fd 100644 --- a/src/main/resources/eclipse_run_config_template.xml +++ b/src/main/resources/eclipse_run_config_template.xml @@ -16,4 +16,5 @@ + diff --git a/src/main/resources/idea_run_config_template.xml b/src/main/resources/idea_run_config_template.xml index 8340e663..55278f7b 100644 --- a/src/main/resources/idea_run_config_template.xml +++ b/src/main/resources/idea_run_config_template.xml @@ -11,5 +11,6 @@ %IDEA_ENV_VARS% + diff --git a/src/test/groovy/net/fabricmc/loom/test/unit/IdeaClasspathModificationsTest.groovy b/src/test/groovy/net/fabricmc/loom/test/unit/IdeaClasspathModificationsTest.groovy index 073978fc..42d00c38 100644 --- a/src/test/groovy/net/fabricmc/loom/test/unit/IdeaClasspathModificationsTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/unit/IdeaClasspathModificationsTest.groovy @@ -84,6 +84,7 @@ class IdeaClasspathModificationsTest extends Specification { %IDEA_ENV_VARS% + '''.trim() @@ -103,6 +104,7 @@ class IdeaClasspathModificationsTest extends Specification { %IDEA_ENV_VARS% + '''.trim()