Skip to content

Build Runnable Jar

Wuyi Chen edited this page Jul 3, 2019 · 10 revisions

Overview


Check Gradle Version

Before configuring build.gradle file, you need to check the version of Gradle and make sure the version is > 4.0. Because we need to apply a Gradle plugin called spring-boot-gradle-plugin to all the sub-projects, that plugin needs the Gradle version is > 4.0. For checking the gradle version, you can use the following command:

gradle -v

Modify build.gradle

The key point of modifying build.gradle is applying the gradle plugin pring-boot-gradle-plugin. With this plugin, you don't have to specify where is the class has the main function, that plugin will automatically figure out where it is.

build.gradle

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

apply plugin: 'java'
apply plugin: 'org.springframework.boot'                                                  // apply the spring-boot-gradle-plugin
apply plugin: 'io.spring.dependency-management'

bootJar {
    baseName = 'root-project-name'
    version =  '1.0.0'
}

repositories {
    mavenCentral()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

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

If your project consists several sub-projects and each sub-project is a microservice. You need to apply the spring-boot-gradle-plugin to sub-project level rather root project level.

build.gradle

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

apply plugin: 'io.spring.dependency-management'                                           // apply the plugin in root project level

subprojects{
    apply plugin: 'org.springframework.boot'                                              // apply the pluin in sub-project level

    // omit other lines
}

References

Clone this wiki locally