2

My organization uses the same Gradle plugins and dependencies for a lot of our projects. My custom plugin knowledge is pretty weak, but what I'd like to do is wrap these plugins and dependencies into a single, standalone plugin. I'm stuck on understanding how to separate the plugins/dependencies required for the plugin versus the ones that I want to use in the consuming project. Here's a simple example that I put together based on the gradle custom plugin docs, and some information about storing the plugin in a maven repo to allow it to automatically download dependencies:

// build.gradle from standalone plugin
plugins {
  id 'java-gradle-plugin'
  id 'maven-publish'

  // these ones I don't need in the plugin, just in the project where I apply the plugin
  id 'war'
  id 'org.springframework.boot' version '2.2.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'org.asciidoctor.convert' version '1.5.8'
}

group = 'org.sample'
version = '1.0.0'

publishing {
  repositories {
    maven {
      url "../maven-repo"
    }
  }
}

gradlePlugin {
  plugins {
    greeting {
      id = "org.sample.greeter"
      implementationClass = "org.sample.GreetingPlugin"
    }
  }
}

dependencies {
  implementation gradleApi() // I think I need this for the imports in GreetingPlugin.java
  implementation localGroovy() // I think I would need this if GreetingPlugin was written in Groovy

  // these ones I don't need in the plugin, just in the project where I apply the plugin
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test' {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}

// this is only needed in the project where I apply the plugin
// I believe this should be in the GreetingPlugin.java file though
test {
 useJUnitPlatform()
}

and the backing class...

package org.sample;

import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;

class Greeting Plugin implements Plugin<Project> {
  @Override
  public void apply(Project project) {
    project.getTasks().create("hello", MyTask.class);
  }

  public static class MyTask extends DefaultTask {
    @TaskAction
    public void myTask() {
      System.out.println("Hello, World!");
    }    
  }
}

In the project I'm trying to consume the plugin, I have the following files:

// settings.gradle
pluginManagement {
  repositories {
    maven {
      url "../maven-repo"
    }
    gradlePluginPortal()
  }
}
// build.gradle
plugins {
  id 'org.sample.greeter' version '1.0.0'
}

My thinking is that using the plugin in this way, the project inherits the plugins and dependencies listed in the plugin code. I think I'm close, as when I ./gradlew publish I can see the plugin being applied, but it doesn't like that the spring-starter-web dependency doesn't have a version (I know that when I do a multi-project gradle repo, I need to include the dependencyManagement block with mavenBOM, so maybe that's the key?) I'm trying to follow the SpringBoot gradle plugin for insight, but it's a bit too complicated for me.

So, is this the correct way to create a standalone plugin that includes plugins/dependencies baked in? And why isn't the spring dependency manager applying the versioning?


EDIT: I followed the link from @Alan Hay, and instead of a custom plugin, I tried to use the 'apply from'. However, it still doesn't work. Here's files based on that approach:

// build.gradle from 'parent' build.gradle
plugins {
  id 'war'
  id 'org.springframework.boot' version '2.2.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'org.asciidoctor.convert' version '1.5.8'
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test' {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}

test {
 useJUnitPlatform()
}

and attempting to reference from another project, it's the only line in the file:

apply from: '<path-to-above>/build.gradle'

This error I get is the following:

  script '<path-to-above>/build.gradle': 15: Only Project build scripts can contain plugins {} blocks

  See https://docs.gradle.org/5.5.1/userguide/plugins.html#sec:plugins_block for information on the plugins {} block

   @ line 15, column 1.
     plugins {
     ^

  1 error
1

1 Answer 1

1

A standalone, binary plugin is the preferred approach when you need to share the same build logic across multiple independent projects. Additionally, good plugin design separates capabilities from convention. In this case, the capabilities are provided by Gradle and some third-party plugins, but you're adding your own conventions on top in this plugin.

When you're implementing this, you essentially need to push the code down one level. Anything that would be configuration in the build.gradle needs to be in your plugin's source code. Anything that would impact the classpath of the buildscript (i.e. buildscript { } or plugins { }) belongs in the dependencies of your plugin. The plugins { } block in your plugin should only have the build plugins required the build the plugin itself.

// build.gradle from standalone plugin

// plugins {} should contain only plugins you need in the build of the plugin itself
plugins {
  id 'java-gradle-plugin'
  id 'maven-publish'
}

group = 'org.sample'
version = '1.0.0'

dependencies {
  implementation gradleApi()

  // Dependencies for plugins you will apply to the target build
  implementation 'io.spring.gradle:dependency-management-plugin:1.0.9.RELEASE'
  implementation 'org.asciidoctor:asciidoctor-gradle-jvm:2.4.0'
  implementation 'org.springframework.boot:spring-boot-gradle-plugin:2.2.4.RELEASE'
}

gradlePlugin {
  plugins {
    greeting {
      id = "org.sample.greeter"
      implementationClass = "org.sample.GreetingPlugin"
    }
  }
}

publishing {
  repositories {
    maven {
      url "../maven-repo"
    }
  }
}
package org.sample;

import org.gradle.api.DefaultTask;
import org.gradle.api.Plugin;
import org.gradle.api.plugins.JavaPlugin;
import org.gradle.api.Project;
import org.gradle.api.tasks.TaskAction;
import org.gradle.api.tasks.testing.Test;

class Greeting Plugin implements Plugin<Project> {

  @Override
  public void apply(Project project) {

    // Apply plugins to the project (already on the classpath)
    project.getPluginManager().apply("war");
    project.getPluginManager().apply("org.springframework.boot");
    project.getPluginManager().apply("io.spring.dependency-management");
    project.getPluginManager().apply(" org.asciidoctor.convert");

    // Dependencies that you need for the code in the project that this plugin is applied
    DependencyHandler dependencies = project.getDependencies();
    dependencies.add(JavaPlugin.IMPLEMENTATION_CONFIGURATION_NAME, "org.springframework.boot:spring-boot-starter-web");
    dependencies.add(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, "org.junit.jupiter:junit-jupiter-engine");
    dependencies.add(JavaPlugin.TEST_IMPLEMENTATION_CONFIGURATION_NAME, springBootStarterTest(dependencies));

    projects.getTasks().withType(Test.class, test -> {
      test.useJUnitPlatform();
    });
  }

  private Dependency springBootStarterTest(DependencyHandler dependencies) {
    Map<String, String> exclude = new HashMap<>();
    exclude.put("group", "org.junit.vintage");
    exclude.put("module", "junit-vintage-engine");
    return ((ModuleDependency) dependencies.module("org.springframework.boot:spring-boot-starter-test")).exclude(exclude);
  }

}

This is more verbose due to being written in Java, but it is functionally equivalent to putting this in your project's build.gradle:

plugins {
  id 'war'
  id 'org.springframework.boot' version '2.2.4.RELEASE'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'org.asciidoctor.convert' version '1.5.8'
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'
  testImplementation 'org.springframework.boot:spring-boot-starter-test' {
    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
  }
  testImplementation 'org.junit.jupiter:junit-jupiter-engine'
}

test {
 useJUnitPlatform()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.