Responding to command line options in a plugin

This problem arises from the default behaviour of Intellij to delegate to gradle for build/test operations and the apparent inability to be aware of command line options in a plugin.

We are using junit5 and have a convention that allows a test to be tagged such that it is not executed during a CI build. This means applying

test.useJUnitPlatform {
    excludeTags 'foo'
}

This is fine until you attempt to run such a test in your IDE and gradle reports there is nothing to run so fails.

The build executed by intellij is of the form

gradlew :cleanTest :test --tests 'a.b.c.D'

A simple solution for this particular behaviour would be to detect if a command line option has been applied to the filter and react accordingly however there are 2 problems

  • there is no public API that allows access to these CLI options
  • the option is not passed through to the task until task graph creation time

This leads to the following hack

    project.gradle.taskGraph.whenReady {
        project.tasks.withType(Test).all { Test t ->
            if (t.filter instanceof DefaultTestFilter && ((DefaultTestFilter)t.filter).commandLineIncludePatterns) {
                t.useJUnitPlatform()
            } else {
                t.useJUnitPlatform {
                    excludeTags 'foo'
                }
            }
        }
    }

Is there a better way to do this?