Gradle multi project (module) example – Type I (nested)

In this series of posts, we will see various ways to implement multi module, hierarchical / parent-child projects using gradle. To provide a working example, this application implements a simple spring boot based rest like service.

This series:
Maven multi module example
Gradle multi project (module) example – Type I (nested)
Gradle multi project (module) example – Type II (flat)

The application contains 3 modules.

  • parent – parent gradle project
  • service – service module
  • web – web module

Though, the parent project can also contain src directory, in this example, src directory is not present and it’s just contains sub-projects and gradle files.

Directory structure

sample-gradle-hierarchical-nested-example
   |---parent
   |------build.gradle
   |------settings.gradle
   |------service
   |---------src
   |---------build.gradle
   |------web
   |---------src
   |---------build.gradle

As you can see above, the root project directory ‘sample-gradle-hierarchical-nested-example’ contains the ‘parent’ project directory which contains ‘build.gradle’, ‘settings.gradle’ and sub-projects ‘service’ and ‘web’. This follows a nested structure, the very basic way of implementing multi module projects using gradle.

parent/build.gradle


subprojects {
    apply plugin: 'java'

    group 'com.idodevjobs'
    version '1.0-SNAPSHOT'

    buildscript {
        ext {
            springVersion = '4.3.5.RELEASE'
            springBootVersion = '1.4.1.RELEASE'
            junitVersion = '4.11'
        }
        repositories {
            mavenCentral()
        }
    }

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {
        mavenCentral()
    }

    dependencies {
        testCompile "junit:junit:$junitVersion"
    }
}

parent/settings.gradle


rootProject.name = 'parent'
include 'service'
include 'web'

service/build.gradle


dependencies {
    compile "org.springframework:spring-context:$springVersion"
}

web/build.gradle


apply plugin: 'spring-boot'

buildscript {
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.1.RELEASE")
    }
}

dependencies {
    compile project (':service')
    compile "org.springframework.boot:spring-boot-starter-web:$springBootVersion"
}

By using multi module structure, we get a few advantages like controlling the versions at 1 place, controlling common parameters, dependencies and tasks.

The parent/build.gradle file defines everything that’s common to all the sub-projects. The sub-projects build.gradle is very simple and they just contain what pertains to the module and inherit most of the settings from parent build.gradle. Also, the web/build.gradle refers to service module as a project dependency and not as an artifactory dependency.

service/src/main/java/com/idodevjobs/example/model/SampleModel.java


package com.idodevjobs.example.model;

public class SampleModel {
    private String message;
    private int id;

    public SampleModel() {
    }

    public SampleModel(String message, int id) {
        this.message = message;
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

web/src/main/java/com/idodevjobs/example/controller/SampleController.java


package com.idodevjobs.example.controller;

import com.idodevjobs.example.model.SampleModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SampleController {

    @RequestMapping("/hello")
    public SampleModel hello() {
        return new SampleModel("hello world", 1);
    }
}

web/src/main/java/com/idodevjobs/example/controller/SampleController.java


package com.idodevjobs.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleApplication {
    public static void main(String[] args) {
        SpringApplication.run(SampleApplication.class, args);
    }
}

web/src/main/resources/application.properties


server.context-path=/sample
server.port=8888

Build & Test

To build and execute, under parent directory, execute the following command.

gradle bootRun

To test, on browser, point to the following url.

http://localhost:8888/sample/hello

Please refer to gradle documentation to explore more about multi module builds using gradle.

Posted in java | Tagged , , , , , , , , , , , , , , | 2 Comments