How to add my own Task validation annotations like @Input and @Optional

I like that when defining a Task class in my Gradle plugin, putting an @Input annotation on the input parameters will validate that they are specified. Like this:

class MyTask extends DefaultTask {
    String someProperty

    String someOtherProperty

What if I want to add some other kinds of custom validations around the property? Is there a good way to hook into what Gradle is already doing to validate additional things as well?

When I tried to write a unit test to make sure that the required inputs were specified, the task didn’t complain if I hadn’t specified required inputs. I was doing it like this:

Task testMyTask = project.tasks.create('testMyTask', MyTask) {
    someOtherProperty = 'someValue'

// This will not complain, even though I failed to provide someProperty
 testMyTask.doStuff() // where doStuff is a @TaskAction

Is there a good way to test whether the correct inputs are provided to the task?


That test won’t work, becasue the validation is performed at a different time and in a different manner. In your test your are calling the the task action directly.

If you want to test validation, the easiest way for plugin authors is (unfortunately) to use GradleRunner.

Hi Erik,

@Schalk_Cronje is right, in order to run the validation you need to use test kit to run the task in an integration test.

Regarding custom validation: It is currently not possibly to hook into Gradle’s task property validation. Your best choice right now is to validate the properties when your task is executing in the task action itself.