Dissecting a build.gradle file

As a reference, the following parts form a build.gradle file for a simple Java project:

group 'org.asysob'
version '1.0'

The organisational identifier for the project and the current version.

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3'
        classpath 'com.github.jengelman.gradle.plugins:shadow:2.0.0'
    }
}

Repositories and dependencies needed to succeed with some of the build stages (e.g. unit test).

repositories {
    mavenCentral()
}

Repositories that are searched for project dependencies.

ext {
    junit4Version = '4.12'
    junitVintageVersion = '4.12.0-M3'
    junitPlatformVersion = '1.0.0-M3'
    junitJupiterVersion = '5.0.0-M3'
    log4jVersion = '2.6.2'
}

The version numbers of required packages; explicitly defined as variables.

apply plugin: 'com.github.johnrengelman.shadow'
apply plugin: 'java'
apply plugin: 'org.junit.platform.gradle.plugin'

The plugins required for the gradle build project. In this case, the Java plugin is used which handles all the Java specific build functionality, the plugin to integrate JUnit testing, and finally the shadow plugin is applied in order to create fat jars.

sourceCompatibility = 1.8
targetCompatibility = 1.8

compileTestJava {
    sourceCompatibility = 1.8
    targetCompatibility = 1.8
    options.compilerArgs += '-parameters'
}

Defines the version of Java required for this project.

jar {
    manifest {
        attributes 'Main-Class' : 'org.asysob.Actor'
    }
}

shadowJar {
    baseName = 'vs2017-actor-seed'
    classifier = 'shadow'
    version = '1.0'
}

For the traditional jar and the fat jar, the entry point and additional naming parts need to be set.

junitPlatform {
    logManager 'org.apache.logging.log4j.jul.LogManager'
}

JUnit needs additional configuration information for logging. This part still doesn’t work as expected.

dependencies {
    compile group: 'org.zeromq', name: 'jeromq', version: '0.3.6'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'

    // JUnit Jupiter API and TestEngine implementation
    testCompile("org.junit.jupiter:junit-jupiter-api:${junitJupiterVersion}")
    testRuntime("org.junit.jupiter:junit-jupiter-engine:${junitJupiterVersion}")

    // If you also want to support JUnit 3 and JUnit 4 tests
    testCompile("junit:junit:${junit4Version}")
    testRuntime("org.junit.vintage:junit-vintage-engine:${junitVintageVersion}")

    testRuntime("org.apache.logging.log4j:log4j-core:${log4jVersion}")
    testRuntime("org.apache.logging.log4j:log4j-jul:${log4jVersion}")
}

Last but not least, the project dependencies are configured. The project needs JeroMQ and GSON when compiled and various JUnit packages to compile and execute unit tests.

The complete file is available here: build.gradle.

Leave a Reply

Your email address will not be published. Required fields are marked *