Task output ignored unless deleting the directory first

I have a task that create a java runtime with jlink.

task createRuntime(type: Exec) {
    commandLine 'jlink',
        '--module-path', '/usr/java/javafx-jmods-11',
        '--add-modules', 'java.base,java.desktop,java.logging,java.scripting,java.xml.bind,javafx.base,javafx.controls,javafx.graphics, javafx.fxml,javafx.media,javafx.web,javafx.swing',
        '--bind-services',
        '--no-header-files',
        '--no-man-pages',
        '--compress=2',
        '--strip-debug',
        '--output', "$buildDir/runtime"
}

I want to avoid this task from running if the directory ${buildDir}/runtime exists, mainly because jlink will fail if it exists, but also because it is unnecessary unless the input JDK has changed.

Task ':createRuntime' is not up-to-date because:
  Task has not declared any outputs despite executing actions.

Found this which tells me how to set the inputs and outputs to tasks.
https://docs.gradle.org/current/userguide/more_about_tasks.html#sec:task_input_output_runtime_api

task createRuntime(type: Exec) {
    outputs.dir("$buildDir/runtime")

    commandLine 'jlink',
        '--module-path', '/usr/java/javafx-jmods-11',
        '--add-modules', 'java.base,java.desktop,java.logging,java.scripting,java.xml.bind,javafx.base,javafx.controls,javafx.graphics,javafx.fxml,javafx.media,javafx.web,javafx.swing',
        '--bind-services',
        '--no-header-files',
        '--no-man-pages',
        '--compress=2',
        '--strip-debug',
        '--output', "$buildDir/runtime"
}

But it does not honor this output, thus trying to run jlink on every build.

Task ':createRuntime' is not up-to-date because:
  Task ':createRuntime' has additional actions that have changed
Starting process 'command 'jlink''. Working directory: /home/sverre/workspace/branches/application-java11 Command: jlink --module-path /usr/java/javafx-jmods-11 --add-modules java.base,java.desktop,java.logging,java.scripting,java.xml.bind,javafx.base,javafx.controls,javafx.graphics,javafx.fxml,javafx.media,javafx.web,javafx.swing --bind-services --no-header-files --no-man-pages --compress=2 --strip-debug --output /home/sverre/workspace/branches/application-java11/build/runtime
Successfully started process 'command 'jlink''
Error: directory already exists: /home/sverre/workspace/branches/application-java11/build/runtime

It seems outputs.dir("$buildDir/runtime") creates the directory first, thus jlink will fail.

I can get around this by adding

doFirst {
    delete "$buildDir/runtime"
}

Is there any other way to do this, a proper way?