Austin Brown
Austin Brown

Reputation: 937

Gradle: Call Task from Imported Plugin in My Own Tasks

I'm used to Maven but currently I'm using Gradle and I'm not really sure how to call tasks defined by other plugins. (Edit: I'm able to call these tasks in the CLI, but I'd like to also invoke them in my own, custom-defined tasks.)

But I'm importing this plugin to format (and enforce format) of my Java project; the tasks I'm most interested in calling are goJF and verGJF.

I've tried a few ways to either call included tasks and I've done even more Googling. I can share some of the (probably embarrassing) ways I've tried to call other tasks if it's helpful, but figured that might be unnecessary information at this point.

Here is my build.gradle:

plugins {
    id 'java'
    // https://github.com/sherter/google-java-format-gradle-plugin
    id 'com.github.sherter.google-java-format' version '0.9'
}

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.google.guava:guava:30.0-jre")
    testImplementation(platform('org.junit:junit-bom:5.7.0'))
    testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
}

// Alias for goJF:
task fmt  {
    goJF
}

// Alias for verGJF:
task vfmt {
    verGJF
}

test {
    useJUnitPlatform()
}

Upvotes: 3

Views: 2076

Answers (2)

Michael Easter
Michael Easter

Reputation: 24468

Working example here.

From the documentation, we note that there are examples of configuring the plugin tasks. So aliasing is a simplification of that approach. Consider:

plugins {
    id 'java'
    // https://github.com/sherter/google-java-format-gradle-plugin
    id 'com.github.sherter.google-java-format' version '0.9'
}

import com.github.sherter.googlejavaformatgradleplugin.GoogleJavaFormat
import com.github.sherter.googlejavaformatgradleplugin.VerifyGoogleJavaFormat

group 'org.example'
version '1.0-SNAPSHOT'

sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.google.guava:guava:30.0-jre")
    testImplementation(platform('org.junit:junit-bom:5.7.0'))
    testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
}

task fmt(type: GoogleJavaFormat) {
}

task vfmt(type: VerifyGoogleJavaFormat) {
}

test {
    useJUnitPlatform()
}

Here fmt is a new task of type GoogleJavaFormat; vfmt is of type VerifyGoogleJavaFormat. These instances can specify their own configuration (and do other things with doFirst, doLast, etc). But as-is, they act as aliases.

Upvotes: 2

Matias Bjarland
Matias Bjarland

Reputation: 4482

A few distinctions to begin with. The built in tasks defined by the plugin are called googleJavaFormat and verifyGoogleJavaFormat.

These tasks are immediately available to you once you have included the plugin which it seems you have done correctly from what I can see.

On the gradle command line, gradle implements a abbreviation functionality where you can call things with shorthand like:

~> gradle gooJF

which is shorthand for:

~> gradle googleJavaFormat

but this only works on the command line and only as long as your shorthand uniquely identifies a task name.

So when you work with tasks in the build.gradle file you will need to use the full name.

In gradle you create a new task via:

task SomeTaskName(type: SomeClassImplementingTheTask) {
  // some configuration of the task
}

In your case you would want to do one of two things:

  1. configure the two existing tasks added by the plugin so that they do what you want or if the tasks already do what they should, you might not need to configure them and can just run them as is.
  2. create your own tasks with your own names but using the implementing classes (i.e. replacing SomeClassNameImplementingTheTask above with GoogleJavaFormat or VerifyGoogleJavaFormat) defined by the plugin.

The simplest of the two is to configure the already existing tasks. This can be done as follows:

googleJavaFormat {
  source = sourceSets*.allJava
  source 'src/special_dir'
  include '**/*.java'
  exclude '**/*Template.java'
  exclude 'src/test/template_*'
}

The googleJavaFormat used here is actually a "plugin extension" which is exposed by the plugin. Plugin extensions are explicitly there for you to be able to alter the behavior of the plugin through configuration.

Note that the configurations options I defined are just examples, there are probably more things you can set here. The above would modify the two existing tasks with your custom settings and you could then call them from the command line using:

~> gradle googleJavaFormat

and

~> gradle verifyGoogleJavaFormat

Again, perhaps you don't even need to configure things and in that case you should just be able to call the tasks as in the above example.

Upvotes: 0

Related Questions