Introduction To Gradle

The Gradle is a groovy based build tool. I used to install sdkman to setup Gradle as well as maven in my machine because sdkman allow you to install number of version and use any version of the tool at the time it necessary. This is a great feature I was expecting as a developer. So exciting with sdkman.

In the following example, I used Gradle version 3.4.1 to setup the multi-moudle project. Here the parent gradle.build file:


task wrapper (type: Wrapper){
    gradleVersion = '3.4.1'
    distributionType = Wrapper.DistributionType.ALL
}
subprojects{
    apply plugin: 'java'
    dependencies {
        // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
        compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    }

    repositories {
        mavenCentral()
    }

    jar {
        baseName = 'example1'
        version = '0.0.1-SNAPSHOT'
    }
}

As shown in the above, If you need to use wrapper (which is recommended) you have to use gradlew command instead of gradle. First you need to create wrapper with the gradel warpper command before use. Under the dependencies I have added one dependency for slf4j library for example.

In the parent root directory, modules can be created for example, I've created two modules: module1 and module2. Each module has the same folder structure such as src/main/java for the Java projects. Each module can be have its own gradle.build file specially if one module depends on other module, for example, module1 is depends on module2:

dependencies{
    compile project(':module2')
}

The reason is, module1 has a Java class Hello.java which implements the IHello interface from the module2 for simplicity.

In the module1:

public class Hello implements IHello{
    
    public String greetings(String name){
        return "Hello "+name;
    }
}

In the module2:

public interface IHello {
    public String greetings(String name);
}

when you type gradle build , this should compile.

Use of BOM

You can convert this simple project to use Spring BOM as follows:

plugins {
    id 'io.spring.dependency-management' version '1.0.0.RELEASE' apply false
}

task wrapper (type: Wrapper){
    gradleVersion = '3.4.1'
    distributionType = Wrapper.DistributionType.ALL
}


subprojects{
    apply plugin: 'java'
    apply plugin: 'io.spring.dependency-management'
    dependencies {
        // https://mvnrepository.com/artifact/org.slf4j/slf4j-api
        compile group: 'org.slf4j', name: 'slf4j-api'
    }
    dependencyManagement {
        imports {
            mavenBom 'io.spring.platform:platform-bom:Cairo-SR2'
        }
    }

    repositories {
        mavenCentral()
    }

    jar {
        baseName = 'example1'
        version = '0.0.1-SNAPSHOT'
    }
}

The important thing is that adding a plugin at root level and use that inside the subprojects. Secondly remove the SLF4J version number to use the BOM dependency version. If you need to see the dependencies, run the command for module2 gradle module2:dependencies.

Comments

Popular posts from this blog

How To: GitHub projects in Spring Tool Suite

Spring 3 Part 7: Spring with Databases

Parse the namespace based XML using Python