Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Gradle Custom Plugin with Java

Tags:

java

gradle

I'm creating a build process with Gradle and I want to provide a plugin that uses Java code. The Gradle plugin documentation says this is possible:

You can implement a custom plugin in any language you like, provided the implementation ends up compiled as bytecode. For the examples here, we are going to use Groovy as the implementation language. You could use Java or Scala instead, if you want.

However, after multiple hours of Googling and reading, I have yet to find any explanation of how to create a Gradle custom plugin with Java. It seems like you could create the code for it in a directory like:

<rootProjectDir>/buildSrc/src/main/java/   MyGradlePlugin.java   MyGradleTasks.java  

But the question then becomes:

  1. How to implement the plugin class and tasks in Java to be compatible with Gradle?
  2. How to get Gradle to recognize the Java classes and tasks so you can use them in a build?

    Can you just reference the plugin class like you do for the Groovy equivalent?

    src/main/resources/META-INF/gradle-plugins/myjavaplugin.properties implementation-class=org.me.MyGradlePlugin

I realize that I could just call the Java code with project.javaexec or JavaExec, but I'm concerned this will make my build process less portable.

like image 581
Joe Fernandez Avatar asked Jul 15 '13 21:07

Joe Fernandez


People also ask

Can a Gradle plugin be written in Java?

Java Gradle Development Plugin. When we're writing our plugins in Java, we can benefit from the Java Gradle Development Plugin. This will automatically compile and add gradleApi() dependencies. It will also perform plugin metadata validation as a part of the gradle jar task.

How do I create a Gradle plugin?

To create a Gradle plugin, you need to write a class that implements the Plugin interface. When the plugin is applied to a project, Gradle creates an instance of the plugin class and calls the instance's Plugin. apply() method.

What is Java Gradle plugin?

The Java Gradle Plugin development plugin can be used to assist in the development of Gradle plugins. It automatically applies the Java Library plugin, adds the gradleApi() dependency to the api configuration and performs validation of plugin metadata during jar task execution.


1 Answers

Here's a basic, stand-alone, Java-based Gradle plugin and the steps to get it working:

  1. Make sure Gradle 1.6 or higher and Java JDK is installed
  2. Create these files below with the directory structure indicated
  3. Change directories to the <projectRoot>/plugin directory
  4. Execute the plugin build: $ gradle uploadArchives This (very important) step compiles the Java code and puts it in your local Maven repo (../repo).
  5. Now execute the consumer script by changing directories to <projectRoot>/consumer
  6. Execute the script that depends on the plugin: $ gradle checkitout

Java Classes

projectRoot/plugin/src/main/java/org/joefernandez/gradle/MyJavaPlugin.java

package org.joefernandez.gradle;  import org.gradle.api.Project; import org.gradle.api.Plugin;  public class MyJavaPlugin implements Plugin<Project> {      @Override     public void apply(Project target) {         target.task("javaTask");     }  } 

projectRoot/plugin/src/main/java/org/joefernandez/gradle/MyJavaTask.java

package org.joefernandez.gradle;  import org.gradle.api.DefaultTask; import org.gradle.api.tasks.TaskAction;  public class MyJavaTask extends DefaultTask {      @TaskAction     public void javaTask() {         System.out.println("Hello from MyJavaTask");     }  } 

Plugin Class declaration

projectRoot/plugin/src/main/resources/META-INF/gradle-plugins/test-plugin.properties

implementation-class=org.joefernandez.gradle.MyJavaPlugin 

Plugin Build Script

Note the uploadArchives task: You must run this task to make the plugin available to the consumer script.

projectRoot/plugin/build.gradle

apply plugin: 'java'  dependencies {     compile gradleApi() }  apply plugin: 'maven'  repositories {     mavenCentral() }  dependencies {     testCompile 'junit:junit:4.11' }  group = 'org.joefernandez' version = '1.0-SNAPSHOT'  uploadArchives {     repositories {         mavenDeployer {             repository(url: uri('../repo'))         }     } } 

Settings for the Plugin

projectRoot/plugin/settings.gradle

rootProject.name = 'MyJavaPlugin' 

Root script

projectRoot/build.gradle

apply plugin: 'java'  dependencies {     compile gradleApi() } 

Consumer script

projectRoot/consumer/build.gradle

buildscript {     repositories {         maven {             url uri('../repo')         }     }     dependencies {         classpath group: 'org.joefernandez',                    name: 'MyJavaPlugin',                 version: '1.0-SNAPSHOT'     } } apply plugin: 'test-plugin'  task checkitout(type: org.joefernandez.gradle.MyJavaTask) {     println("running consumer task!") } 
like image 185
Joe Fernandez Avatar answered Sep 18 '22 20:09

Joe Fernandez