To create a custom standalone Gradle plug-in using java (you can also use Groovy) you have to create a structure like this:
plugin
|-- build.gradle
|-- settings.gradle
|-- src
    |-- main
    |   |-- java
    |   |-- resources
    |       |-- META-INF
    |           |-- gradle-plugins
    |-- test
In the build.gradle file you define your project.
apply plugin: 'java'
apply plugin: 'maven'
dependencies {
    compile gradleApi()
} 
The java plugin will be used to write java code.
The gradleApi() dependency will give us all method and propertiess needed to create a Gradle plugin.
In the settings.gradle file:
rootProject.name = 'myplugin' 
It will define the artifact id in Maven.
If settings.gradle file is not present in the plugin directory the default value will be the name of the directory.
Define a class in the src/main/java/org/sample/MyPlugin.java implementing the Plugin interface.
import org.gradle.api.Plugin;
import org.gradle.api.Project;
public class MyPlugin implements Plugin<Project> {
    @Override
    public void apply(Project project) {
         project.getTasks().create("myTask", MyTask.class);
    }
} 
Define the task extending the DefaultTask class:
import org.gradle.api.DefaultTask;
import org.gradle.api.tasks.TaskAction;
public class MyTask extends DefaultTask {
    @TaskAction
    public void myTask() {
        System.out.println("Hello World");
    }
}
In the META-INF/gradle-plugins folder you have to create a properties file defining the implementation-class property that identifies the Plugin implementation class.
In the META-INF/gradle-plugins/testplugin.properties
implementation-class=org.sample.MyPlugin.java
Notice that the properties filename matches the plugin id.
Change the build.gradle file adding some info to upload the plugin in a maven repo:
apply plugin: 'java'
apply plugin: 'maven'
dependencies {
    compile gradleApi()
}
repositories {
    jcenter()
}
group = 'org.sample'
version = '1.0'
uploadArchives {
    repositories {
        mavenDeployer {
        repository(url: mavenLocal().url)
        }
    }
} 
You can build and publish the Gradle plug-in to the Maven repo defined in the plugin/build.gradle file using the following command.
$ ./gradlew clean uploadArchives 
To use the plugin add in the build.gradle of your project:
buildscript {
     repositories {
         mavenLocal()
     }
 dependencies {
    classpath group: 'org.sample',    // Defined in the build.gradle of the plugin
              name: 'myplugin',       // Defined by the rootProject.name 
              version: '1.0'
    }
 }
apply plugin: 'testplugin'            // Defined by the properties filename
Then you can call the task using:
 $ ./gradlew myTask