Better incremental cucumber task than this?

I’m running cucumber with junit-5 which means my cucumber task looks like this

configurations {
    cucumberRuntime {
        extendsFrom testImplementation
    }
}

tasks.withType(Jar) {
    from configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
}

task cucumber() {
    dependsOn build
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

To make this incremental, I’ve done this:

task cucumberClasspathWrapper(type: JavaExec) {
    classpath = configurations.cucumberRuntime
}

task cucumber() {
    dependsOn build
    inputs.files(cucumberClasspathWrapper.classpath)
    outputs.dirs(sourceSets.main.output.files.toList())
    outputs.dirs(sourceSets.test.output.files.toList())
    doLast {
        javaexec {
            main = "cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'gradle.cucumber', 'src/test/resources']
        }
    }
}

This has the behaviour I want, where any changes to the java files under test, or any change to the cucumber test files, reruns the tests. However, I’ve wired this together with the use of this task cucumberClasspathWrapper which is only there to calculate the classpath.

Can you recommend a better way of doing this?

Further Reading / Resources