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


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.


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 {

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    repositories {

    dependencies {
        testCompile "junit:junit:$junitVersion"

parent/settings.gradle = 'parent'
include 'service'
include 'web'


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


apply plugin: 'spring-boot'

buildscript {
    dependencies {

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.


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; = id;

    public String getMessage() {
        return message;

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

    public int getId() {
        return id;

    public void setId(int id) { = id;


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;

public class SampleController {

    public SampleModel hello() {
        return new SampleModel("hello world", 1);


package com.idodevjobs.example;

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

public class SampleApplication {
    public static void main(String[] args) {, args);



Build & Test

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

gradle bootRun

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


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

This entry was posted in java and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to Gradle multi project (module) example – Type I (nested)

  1. Pingback: Gradle multi module example – Type II (flat) | iDoDevJobs

  2. Pingback: Maven multi module example | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s