Source code on github:

005-Using Spring boot

Copy then the demo003 to demo005! I choose to transform the startup project in a Spring boot application!

Choose the version

First we should add the version of the plugins that are needed to build a spring boot app.

Two plugins will be used for this target with a new ext.version array in the build.gradle

ext.versions = [
        'SpringBoot'        : '2.2.2.RELEASE',
        'SpringDependencies': '1.0.8.RELEASE'

Adding the spring boot plugins!

Inside the startup project the following is needed. To build the project we need the following libs in classpath. The versions variable contains the dependency version. The rest of classpath is taken from Maven Central.

A simple way to write this would be the following.

    id: 'org.springframework.boot:spring-boot-gradle-plugin:2.2.2.RELEASE'
    id: 'io.spring.gradle:dependency-management-plugin:1.0.8.RELEASE'

You can think... WTF!!! i can change the version with a variable!

    id: 'org.springframework.boot:spring-boot-gradle-plugin:' + versions.SpringBoot
    id: 'io.spring.gradle:dependency-management-plugin:' + versions.SpringDependencies

Sorry, no, you can't. The only way i founded to do this is a combination of "buildscript" to add the plugins during the build, followed by "apply" to apply the plugins declared on the buildscript section

This section must be BEFORE the plugin section! These plugins are the first loaded!

buildscript {
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:'+versions.SpringBoot,

plugins {

Right after the plugins we apply the plugins to the project

plugins {

apply plugin:'org.springframework.boot'
apply plugin:'io.spring.dependency-management'

And finally the plugin usage. Note the versions.SpringBoot in the first line!! The with dependency constraint we remove the optional dependency snakeyaml

Please notice the difference of declarations of the boot dependencies and boot starter web. Here there are "real strings" that can use the variables. All this stuff is normally inside a totally bloated pom.xml.

dependencies {
    ... //Other dependencies
    implementation group: 'org.springframework.boot', name: 'spring-boot-dependencies', version: versions.SpringBoot
    implementation 'org.springframework.boot:spring-boot-starter-web:'+versions.SpringBoot

    testImplementation 'org.springframework.boot:spring-boot-starter-test:'+versions.SpringBoot

    components {
        withModule('org.springframework:spring-beans') {
            allVariants {
                withDependencyConstraints {
                    it.findAll { == 'snakeyaml' }
                        .each { it.version { strictly '1.19' } }

For me a definitve advantage is that i don't have to declare spring-boot as parent pom for my whole project (maven afaik does not support multiple inheritance)

Now you will see, runnign "gradle tasks" the bootRun task!

gradle init

Try everything

Add the a new source file:

package org.kendar.controllers;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

public class HelloGradleController {

    public String helloGradle() {
        return "Hello Gradle!";

And modify the main to the following

package org.kendar;

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

import java.util.Collections;

public class HelloWorld {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(HelloWorld.class);
        app.setDefaultProperties(Collections.singletonMap("server.port", "8094"));;

Now requiring "gradle bootRun" you can go to http://localhost:8094 and see a fantastic "Hello Gradle"

gradle init

Last modified on: February 18, 2020