Running Spring app built with gradle on Heroku Running Spring app built with gradle on Heroku spring spring

Running Spring app built with gradle on Heroku


After a short discussion in comments it seems (according to this instructions) that You need add the following task to build.gradle file:

task stage(type: Copy, dependsOn: [clean, build]) {    from jar.archivePath    into project.rootDir     rename {        'app.jar'    }}stage.mustRunAfter(clean)clean << {    project.file('app.jar').delete()}

And the content of Procfile will be:

web: java $JAVA_OPTS -jar app.jar

Now it should work fine.

Explanation:

Task stage prepares jar file to be run. After it's finished the output jar is copied into project.rootDir and renamed to app.jar. It guarantees that it will have always the same name and can be easily run with the command from Procfile. stage task also depends on clean (which has an additional action to remove app.jar) and build (which builds the app). It's important to set stage.mustRunAfter(clean) because otherwise the order in which tasks are run is not determined (this probably happens now - check it locally). What I mean is if just dependsOn is specified build may be run, then clean and finally stage - and nothing is created. I hope it's clear.


I've made an example so noobies can really check out and understand every line of code. Because I really know how frustrating this can be to figure out.

This is what your Procfile should look like

web: java $JAVA_OPTS -Dserver.port=$PORT -jar app.jar

This is what your Gradle file should look like

group 'com.springtest.api'version '1.0-SNAPSHOT'buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE")    }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'idea'apply plugin: 'spring-boot'//Path to the Main Web ClassmainClassName = "hello.Application"jar {    baseName = 'spring-test-api'    version = '0.1.0'}repositories {    mavenCentral()}sourceCompatibility = 1.8dependencies {    testCompile 'junit:junit:4.11'    // Spring Framework    compile 'org.springframework.boot:spring-boot-starter-web'}task wrapper(type: Wrapper) {    gradleVersion = '2.6'}task stage(type: Copy, dependsOn: [clean, build]) {    from jar.archivePath    into project.rootDir    rename {        'app.jar'    }}stage.mustRunAfter(clean)clean << {    project.file('app.jar').delete()}

If the gradlew and gradlew.bat aren't in your main project directory or is not up-to-date then in cmd run the command gradle wrapper.

If you don't have Gradle actually installed on your computer install it from the main gradle website.

My example on github is below.https://github.com/arose13/Heroku-Spring-Gradle_Example