Spring Boot multi module with Gradle

Jan 1, 0001

Gradle is a build tool in the java world that will one day hopefully deprecate maven. I hate editing XML (like most other humans) and Gradle uses it’s own DSL (looks like groovy) to define you build file. Spring boot has a great plugin for Gradle which make setting up the build easier. To create a multi module project for Spring boot with Gradle here are the steps you need to follow:

Root configuration

in the root directory of your project create a file called settings.gradle and add this content

include "my-sub-module1"
include "my-sub-module2"

add all the sub modules with an include directive. Now create a file build.gradle in the root directory and add the following

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

Sub-module configuration

Now for each sub-module that you have you will create a build.gradle file in the sub-module directory with the content

apply plugin: "java"
apply plugin: "idea"
apply plugin: "eclipse"
apply plugin: "spring-boot"
apply plugin: "war"
repositories {
    mavenCentral()
    maven { url "http://mvn.dendiz.com/repository/internal" }
}

dependencies {
    testCompile "junit:junit:4.11"
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // tag::jetty[]
    compile("org.springframework.boot:spring-boot-starter-web") {
        exclude group:"org.springframework.boot", module: "spring-boot-starter-jetty"
    }
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
    compile("org.springframework.boot:spring-boot-starter-freemarker")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    testCompile("junit:junit")
}

adjust your plugins and dependencies as needed, these are just some example dependencies.