Gradle Plugins

Gradle plugins extend the functionality of your Gradle build script. They can handle various tasks such as compiling code, packaging binaries, running tests, and deploying applications. This guide will cover some essential Gradle plugins and how to use them with the latest versions.

Applying Plugins

To apply a plugin in Gradle, you can use the plugins block in your build.gradle file:

plugins {
    id 'java' // Java plugin
    id 'application' // Application plugin
}

Essential Gradle Plugins

Java Plugin

The Java plugin adds support for Java projects, including compiling Java source code, generating JAR files, and running tests.

plugins {
    id 'java'
}

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}

test {
    useJUnitPlatform()
}

Application Plugin

The Application plugin facilitates building and packaging Java applications. It provides tasks for running and creating distributions.

plugins {
    id 'application'
}

mainClassName = 'com.example.Main' // Replace with your main class

application {
    applicationDefaultJvmArgs = ["-Dmyapp.property=somevalue"]
}

Maven Publish Plugin

The Maven Publish plugin allows you to publish build artifacts to a Maven repository.

plugins {
    id 'maven-publish'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            url = uri('https://repo.mycompany.com/maven2')
            credentials {
                username = project.findProperty("repo.user")
                password = project.findProperty("repo.password")
            }
        }
    }
}

Shadow Plugin

The Shadow plugin packages an application and its dependencies into a single executable JAR file, also known as a fat JAR.

plugins {
    id 'com.github.johnrengelman.shadow' version '8.0.0'
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
}

shadowJar {
    archiveClassifier.set('all')
}

Checkstyle Plugin

The Checkstyle plugin performs static code analysis to help enforce coding standards.

plugins {
    id 'checkstyle'
}

checkstyle {
    toolVersion = '10.12.0'
    config = resources.text.fromFile('config/checkstyle/checkstyle.xml')
}

tasks.withType(Checkstyle).configureEach {
    reports {
        xml.required.set(false)
        html.required.set(true)
    }
}

SpotBugs Plugin

The SpotBugs plugin analyzes code for bugs and potential issues.

plugins {
    id 'com.github.spotbugs' version '5.0.13'
}

tasks.withType(com.github.spotbugs.SpotBugsTask).configureEach {
    reports {
        xml.enabled = false
        html.enabled = true
        html.stylesheet = 'fancy-hist.xsl'
    }
}

Jacoco Plugin

The Jacoco plugin provides code coverage metrics for your tests.

plugins {
    id 'jacoco'
}

jacoco {
    toolVersion = '0.8.8'
}

tasks.test {
    finalizedBy tasks.jacocoTestReport
}

tasks.jacocoTestReport {
    reports {
        xml.required.set(true)
        html.required.set(true)
    }
}

Example: Complete Build Script

Here is a complete example of a build.gradle file using some of the plugins mentioned above:

plugins {
    id 'java'
    id 'application'
    id 'maven-publish'
    id 'com.github.johnrengelman.shadow' version '8.0.0'
    id 'checkstyle'
    id 'com.github.spotbugs' version '5.0.13'
    id 'jacoco'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.2'
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.10.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.10.0'
}

mainClassName = 'com.example.Main' // Replace with your main class

application {
    applicationDefaultJvmArgs = ["-Dmyapp.property=somevalue"]
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }

    repositories {
        maven {
            url = uri('https://repo.mycompany.com/maven2')
            credentials {
                username = project.findProperty("repo.user")
                password = project.findProperty("repo.password")
            }
        }
    }
}

shadowJar {
    archiveClassifier.set('all')
}

checkstyle {
    toolVersion = '10.12.0'
    config = resources.text.fromFile('config/checkstyle/checkstyle.xml')
}

tasks.withType(Checkstyle).configureEach {
    reports {
        xml.required.set(false)
        html.required.set(true)
    }
}

tasks.withType(com.github.spotbugs.SpotBugsTask).configureEach {
    reports {
        xml.enabled = false
        html.enabled = true
        html.stylesheet = 'fancy-hist.xsl'
    }
}

jacoco {
    toolVersion = '0.8.8'
}

tasks.test {
    useJUnitPlatform()
    finalizedBy tasks.jacocoTestReport
}

tasks.jacocoTestReport {
    reports {
        xml.required.set(true)
        html.required.set(true)
    }
}

Conclusion

Gradle plugins are essential for extending the functionality of your build scripts. They simplify tasks such as compiling code, packaging applications, and running tests. This guide covered some of the most commonly used plugins with examples. Always ensure to use the latest versions of dependencies and plugins for better performance and security.

Comments