Spring Boot & Gradle – Project Setup

Spring Boot & Gradle – Project Setup

This guide shows you how to setup Spring Boot project using Gradle, and how to create the Hello World application.

Spring Boot 2.0.X.RELEASE

There is a little difference between 1.5.X-RELEASE setup and 2.0.X-RELEASE. First of all you have to upgrade Gradle Wpapper to use Gradle 4.0 or later what I described here. Then you have to edit build.gradle  and add the following lines.

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

group 'com.bettercoding.spring'
version '1.0-SNAPSHOT'

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

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
}

 

Hello World Application

Add MyApp class with the flowing content and run it.

package com.bettercoding.spring;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return new CommandLineRunner() {
            @Override
            public void run(String... args) throws Exception {
                System.out.println("Hello World");
            }
        };
    }
}

 

 

Spring Boot 1.5.X.RELEASE

Edit build.gradle file and add the following lines:

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

group 'com.bettercoding.spring'
version '1.0-SNAPSHOT'

apply plugin: 'java'
apply plugin: "org.springframework.boot"

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter")
}

 

 

2
Leave a Reply

avatar
2 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
0 Comment authors
Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
trackback

[…] Spring Boot & Gradle – Project Setup […]

trackback

[…] Spring Boot & Gradle – Project Setup […]

Close Menu