Skip to content

Build file annotated

Vyacheslav Rusakov edited this page Jan 7, 2016 · 1 revision

Here is sample build file with annotations

plugins {
    id 'groovy'               // enable groovy support as groovy is the main language
    id 'java-gradle-plugin'   // configure project for plugin develpment
    id 'com.gradle.plugin-publish' version '0.9.2'       // publish to gradle plugins portal
    id 'net.researchgate.release' version '2.3.4'        // maven-like release process
    id 'ru.vyarus.quality' version '1.2.0'               // enables CodeNarc plugin and configure default rules
    id 'com.jfrog.bintray' version '1.5'                 // upload to bintray (jcenter and maven central)
    id 'ru.vyarus.java-lib' version '1.0.1'              // artifacts definition
    id 'ru.vyarus.github-info' version '1.0.0'           // configure pom and bintray with github links
    id "com.github.ben-manes.versions" version "0.11.3"  // dependencyUpdates task to check for new versions
}

New gradle plugins dsl. It may be used together with legacy plugins definition through buildscipt and plugin apply.

sourceCompatibility = 1.6

Java compatibility. Note that targetCompatibility option is not required because its the same as source by defaut.

wrapper {
    gradleVersion = 2.9
}

Gradle wrapper version. When need to update project wrapper simply update version here and call wrapper task (it will update scripts and jar in gradle dir)

task createClasspathManifest {
    def outputDir = file("$buildDir/$name")

    inputs.files sourceSets.main.runtimeClasspath
    outputs.dir outputDir

    doLast {
        outputDir.mkdirs()
        file("$outputDir/plugin-classpath.txt").text = sourceSets.test.runtimeClasspath.join("\n")
    }
}

Task creates classpath file for TestKit tests. To run tests from IDE this task must be called first.

repositories { jcenter() }
dependencies {
    testCompile gradleTestKit()
    testCompile('org.spockframework:spock-core:1.0-groovy-2.4') {
        exclude module: 'groovy-all'
    }
    testRuntime files(createClasspathManifest)
}

Groovy version is implicitly set to current wrapper's gradle groovy version.

group = 'com.johnd'
description = 'Test plugin'

Maven group id and project short description. Description is used in pom and for bintray package description.

github {
    user 'johnd'
    license 'MIT'
}

Github info configuration used by ru.vyarus.github-info plugin to configre github links in pom, for bintray package and for plugin-publish plugin.

pom {
    developers {
        developer {
            id "johnd"
            name "John Doe"
            email "johnd@somemail.com"
        }
    }
}

Free pom customization provided by ru.vyarus.pom plugin. Most pom sections will be pre-filled by other plugins. Use generatePomFileForMavenPublication task to verify pom (generated pom licated in build/publications/maven/pom-default.xml)

bintray {
    user = project.hasProperty('bintrayUser') ? bintrayUser : 'USER'
    key = project.hasProperty('bintrayKey') ? bintrayKey : 'KEY'
    publications = ['maven']
    dryRun = false
    publish = true
    pkg {
        repo = 'mvn'
        name = project.name
        desc = project.description
        labels = ['java', 'sample', 'lib']
        publicDownloadNumbers = true
        version {
            gpg {
                sign = true
                passphrase = project.hasProperty('gpgPassphrase') ? gpgPassphrase : ''
            }
            mavenCentralSync {
                sync = false // enable after project acceptance in jcenter
                user = project.hasProperty('sonatypeUser') ? sonatypeUser : 'USER'
                password = project.hasProperty('sonatypePassword') ? sonatypePassword : 'PASSWORD'
            }
        }
    }
}

com.jfrog.bintry plugin configuration.

pluginBundle {
    description = 'Test plugin for ...'
    tags = ['java', 'sample', 'plugin']

    plugins {
        githubInfoPlugin {
            id = 'com.johnd.test'
            displayName = project.description
        }
    }

    mavenCoordinates {
        groupId = project.group
        artifactId = project.name
    }
}

// HOTFIX: plugin-publish don't see properties in global gradle.properties
System.setProperty('gradle.publish.key', project.properties['gradle.publish.key'] ?: '')
System.setProperty('gradle.publish.secret', project.properties['gradle.publish.secret'] ?: '')

Plugin-publish plugin configuration. Manual properties set us required due to plugin-publish implmentation, which ignores global gradle properties.

afterReleaseBuild {
    dependsOn = [bintrayUpload, publishPlugins]
    doLast {
        logger.warn "RELEASED $project.group:$project.name:$project.version"
    }
}

Release process configuration: bintrayUpload must be called during release.

test {
    testLogging {
        events "skipped", "failed", "standard_error"
        exceptionFormat "full"
    }
    maxHeapSize = "512m"
}

Tests configuration is important for travis. Test logging configured to log errors with exception to simplify debug. maxHeapSize is important to workaround travis 137 error: tests are executed in forked vm and inside docker container (sudo:false in .travis.yml) java see overall memory instead of allowed for current container and set incorrect default for max memory (as a result vm gets killed by travis monitoring agent and we see error 137 in log)

dependencyUpdates.revision = 'release'

Configure dependencies plugin to show only released newer versions.

Clone this wiki locally