2015-11-23

Spring-Boot / Groovy / Gradle "Hello World" application


The complete code for this project can be found here:
https://github.com/mtrojahn/spring-boot-groovy-console-app

Last weekend I was playing around with Gradle which I have never used before. The result is this very basic spring-boot project just to illustrate how I did it. Gradle isn't that difficult but I had a few problems until I learn about the spring-boot-gradle-plugin and such.

This is the build.gradle:

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

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

apply plugin: 'groovy'
apply plugin: 'spring-boot'

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile "org.springframework.boot:spring-boot-starter"
    compile 'org.codehaus.groovy:groovy-all'
    testCompile "junit:junit"
}

bootRun {
    addResources = true
}

As you can see it's fairly simple and way more organized than Maven in my opinion. The spring-boot-gradle-plugin is the key here. It provides all the version information for the dependencies and also adds a Gradle task "bootRun" which you can use to run your project.

Now we just need a main class:
@SpringBootApplication
class Application {
    static main(args) {
        def context = new SpringApplicationBuilder()
                .sources(Application.class)
                .bannerMode(Banner.Mode.OFF)
                .run()

        def app = context.getBean(Application.class)
        app.start()
    }

    def start() {
        println("Hello World!")
    }
}

Not that different from Java, right?

I'm a complete newbie on Gradle and Groovy but I really liked what I saw so far... If I made some mistake please leave a comment.

See ya!

No comments:

Post a Comment

Blog Archive