Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents
outlinetrue

...

This page gathers information on plugins that you can use in your Gradle build scripts to enhance it with new capabilities.

...

Some documentation exists in the header of that plug-in file but here is an example usage from the header:

No Format

task checkstyleReport(type: Xslt, dependsOn: check) {
    source project.checkstyleResultsDir
    include '*.xml'
    destDir = project.checkstyleResultsDir
    extension = 'html'

    stylesheetFile = file( 'config/checkstyle/checkstyle-noframes.xsl' )
}

...

Add the following to your build script:

Code Block

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    classpath 'de.huxhorn.gradle:de.huxhorn.gradle.pgp-plugin:0.0.3'
  }
}

You can then apply the plugin:

Code Block

allprojects {
  // below doesn't work yet...
  // apply plugin: 'sign'
  apply plugin: de.huxhorn.gradle.pgp.PgpPlugin


  pgp {
    secretKeyRingFile = new File("${System.properties['user.home']}/.gnupg/secring.gpg")
    keyId = '740A1840'
  }
}

Configuration is either done by the pgp convention like this:

Code Block

pgp {
  secretKeyRingFile = new File("${System.properties['user.home']}/.gnupg/secring.gpg")
  keyId = '740A1840'
  password = 'youShouldNotDoThis'
}

or using gradle properties:

Code Block

-PpgpKeyId=740A1840
-PpgpSecretKeyRingFile=/path/to/file
-PpgpPassword=youShoudlntDoThisEither

...

Code Block
titleExample of usage

apply plugin: 'org.linkedin.userConfig'
topBuildDir = userConfig.top.build.dir ?: "${rootDir}/out/build"

...

Code Block
titleExample of usage

//project-spec.groovy
spec = [
    name: 'gradle-plugins',
    group: 'org.linkedin',
    version: '1.1.0',
    versions: [
      groovy: '1.7.5'
    ]
]
spec.external = [
    json: 'org.json:json:20090211',
    groovy: "org.codehaus.groovy:groovy:${spec.versions.groovy}"
]

// build.gradle:
apply plugin: 'org.linkedin.spec'
allprojects {
  group = spec.group
  version = spec.version
  dependencies {
   compile spec.external.json
   groovy spec.external.groovy
  }
}

...

Code Block
titleExample of usage

// repositories.gradle
allRepositories.buildscript = {
  mavenCentral()
}

// build.gradle:
apply plugin: 'org.linkedin.repository'
subprojects {
  buildscript {
    allRepositories.buildscript.configure(repositories)
  }
}

...

Code Block
titleExample of usage

gradle release

// if using the org.linkedin.spec plugin
gradle publish -Psnapshot=true

...

source: https://github.com/kellyrob99/gradle-jslint-plugin

Versions Plugin

A plugin to discover dependency updates.

author: Ben Manes

source: https://github.com/ben-manes/gradle-versions-plugin

Writing Custom Plugins

See: Writing Custom Plugins chapter in Gradle User Guide

...

Code Block
titleVersionClass.groovy

import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.plugins.JavaPlugin
import java.util.Date;

class VersionClass implements Plugin<Project>  {
    
    VersionClass() {
    }
    
    def void apply(Project project) {
        project.getPlugins().apply( JavaPlugin.class )
        def genSrc = 'generated-src/version'
        def generatedSrcDir = new File(project.buildDir, genSrc)
        
        def makeVersionClassTask = project.task('makeVersionClass') << {
            def now = new Date()
            def outFilename = "java/"+project.group.replace('.','/')+"/"+project.name+"/BuildVersion.java"
            def outFile = new File(generatedSrcDir, outFilename)
            outFile.getParentFile().mkdirs()
            def f = new FileWriter(outFile)
            f.write('package  '+project.group+"."+project.name+';\n')
            f.write("""
/**
 * Simple class for storing the version derived from the gradle build.gradle file.
 *
 */
public class BuildVersion {

    private static final String version = \""""+project.version+"""\";
    private static final String name = \""""+project.name+"""\";
    private static final String group = \""""+project.group+"""\";
    private static final String date = \""""+now+"""\";

    /** returns the version of the project from the gradle build.gradle file. */
    public static String getVersion() {
        return version;
    }
    /** returns the name of the project from the gradle build.gradle file. */
    public static String getName() {
        return name;
    }
    /** returns the group of the project from the gradle build.gradle file. */
    public static String getGroup() {
        return group;
    }
    /** returns the date this file was generated, usually the last date that the project was modified. */
    public static String getDate() {
        return date;
    }
    public static String getDetailedVersion() {
        return getGroup()+":"+getName()+":"+getVersion()+" "+getDate();
    }
""")
            
            f.write("}\n")
            f.close()
        }
        project.sourceSets {
            version {
                java {
                    srcDir project.buildDir.name+'/'+genSrc+'/java'
                }
            }
        }
        makeVersionClassTask.getInputs().files(project.sourceSets.main.getAllSource() )
        makeVersionClassTask.getOutputs().files(generatedSrcDir)
        if (project.getBuildFile() != null && project.getBuildFile().exists()) {
            makeVersionClassTask.getInputs().files(project.getBuildFile())
        }
        project.getTasks().getByName('compileJava').dependsOn('compileVersionJava')
        project.getTasks().getByName('compileVersionJava').dependsOn('makeVersionClass')
        project.getTasks().getByName('jar') {
            from project.sourceSets.version.classes
        }
    }
}

...