This seems like something that’s much harder to accomplish than it should be. How do I get a full set of arguments to java that the run
task uses? Technically I can copy all properties like so:
project.getTasks().register("runDetached", JavaFork.class, task -> {
var runTask = (JavaExec) project.getTasks().getByName("run");
task.setGroup("application");
task.setDescription("Runs this project as a JVM application in the background");
task.getMainClass().set(runTask.getMainClass());
task.getMainModule().set(runTask.getMainModule());
task.setClasspath(runTask.getClasspath());
task.getJavaLauncher().set(runTask.getJavaLauncher());
task.setJvmArgs(runTask.getJvmArgs() != null ? runTask.getJvmArgs() : List.of());
task.getJvmArguments().set(runTask.getJvmArguments());
});
Then put all of this together in the @TaskAction
of my custom task:
var command = new ArrayList<String>(16);
command.add(getJavaLauncher().get().getExecutablePath().toString());
command.addAll(getAllJvmArgs());
command.addAll(getJvmArgs());
if (getMainModule().isPresent()) {
command.add("--module-path");
command.add(getClasspath().getAsPath());
command.add("--module");
command.add(getMainModule().get() + '/' + getMainClass().get());
} else {
command.add("-cp");
command.add(getClasspath().getAsPath());
command.add(getMainClass().get());
}
command.addAll(getArgs());
But surely there has to be a better way, right? After all, the full command used by run
is already logged when running with the --info
flag.