Data & Intelligence

Iterative BI + Gradle Tips and Tricks: Building a Custom Plugin

To build a custom plugin in groovy, do this.  I know this seems redundant, but it wasn’t as clear as it should have been.

1. Create a project directory.  Just a plain ol’ directory. Wherever you like.

2. Add the source file subdirectories:

src\main\groovy\…  (with the package path you’d like to use.  We have src\main\groovy\com\perficient\gradle)

3. Create a plugin class (in a file named <classname>.groovy, we have DatabasePlugin.groovy here).

package com.perficient.gradle

import org.gradle.api.Project
import org.gradle.api.Plugin

class DatabasePlugin implements Plugin<Project> {
    void apply(Project project) {
        project.convention.plugins.database = new DatabaseConvention(project) // if you have a custom convention class. Omit if not.
        // Configure your tasks here.  Add as many as you need.
        project.task('build') {
            description = 'Configure the environment with the specific environment code/name.'
        }

        project.task('build') << { println "Add actions like this if needed." }

        // All the normal task configuration (dependsOn, Type) can go as the first param.
        project.task(dependsOn: 'build', 'test') {
            decription = 'Run tests after building the database.'
        }
    }
}
Data Intelligence - The Future of Big Data
The Future of Big Data

With some guidance, you can craft a data platform that is right for your organization’s needs and gets the most return from your data capital.

Get the Guide

4. Add a properties file in src\main\resources\META-INF\gradle-plugins with the name of your plugin.  We have Database.properties and AgileBiEnv.properties in here.  In these files add one line (modified as needed):

implementation-class=com.perficient.gradle.DatabasePlugin

5. Add a build.gradle file to the plugin root:

apply plugin: 'groovy'

dependencies {
    compile gradleApi()
    groovy localGroovy()
}

Once you start getting non-trivial, you may need to add external jars to the classpath (add this to build.gradle):

sourceSets {
    main {
        groovy {
            compileClasspath += fileTree(dir: '../../../tools/lib', includes:['*.jar'])
        }
    }
}

And, if you want the jar to end up somewhere specific:

jar {
    destinationDir = new File('../../lib')
}

6. Finally, add a settings.gradle file to the root to set the name of the jar you’ll create:

rootProject.name = 'AgileBi'

You’re all set! Run “gradle build” from the command line in the plugin root directory and you should have a shiny new plugin jar to use in your build.

OK, I have a jar. Now what?

Elsewhere in your system, you’ll have a place where you want to use your plugin.  In the build.gradle there, add this:

buildscript {
    dependencies {
        classpath fileTree(dir: '../system/build/lib', include: '*.jar')
    }
}

apply plugin: 'Database'

When you run “gradle tasks” from this directory, you should now see the tasks you created in your plugins Apply() function.

 

About the Author

More from this Author

Thoughts on “Iterative BI + Gradle Tips and Tricks: Building a Custom Plugin”

  1. Nice! There are some conventions that could be used to reduce the code set and I would prefer a nexus server or artifactory to jars on the file system. nice clarification though!

    I would like to see posts on use case… why to build a plugin in the BI space. what the advantages? etc.

    As a PRFT alumni… kudos

  2. Chris Grenz Post author

    Thanks Ken – a repository (like artifactory) may be in the future, but for right now we’re trying to keep it simple for all us non-programmer BI folks!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to the Weekly Blog Digest:

Sign Up