I have followed this guide for creating a custom task with Gradle.
http://www.ysofters.com/2015/02/26/how-to-create-gradle-project-with-custom-task-classes-in-groovy/
I also
you need to put a build.gradle file into your buildSrc directory. You can checkout the buildSrc/build.gradle file from the gradle project itself as an example: https://github.com/gradle/gradle/blob/v4.1.0/buildSrc/build.gradle