Gradle

Scalingo supports Gradle, a famous build automation system for Java. The details of the Scalingo’s Java support are described in this article.

This buildpack will detect your app as using Gradle if it has a build.gradle file, settings.gradle file, or gradlew file at the root of your project. If you are using gradlew, do not forget to add the gradle/wrapper directory to your Git repository:

git commit gradlew gradle/wrapper -m "Gradle Wrapper"

Then deploy again with git push scalingo master.

Note: These files must not be in your .gitignore. If you forgot to add these files, you will receive the following error:

-----> executing ./gradlew stage
       Error: Could not find or load main class org.gradle.wrapper.GradleWrapperMain

Gradle/Gradle Wrapper JDK Compatibility

Be careful when you update the Java runtime of your application. If the Gradle version does not support the version of Java you specified, you may get an error like this:

-----> Installing JDK 14... done
-----> Building Gradle app...
-----> executing ./gradlew stage
       
       FAILURE: Build failed with an exception.
       
       * What went wrong:
       Could not determine java version from '14.0.2'.

Please use a Gradle version compatible with the Java version you want to use to fix this error. Refer to this official Gradle documentation page to know which versions are compatible.

Verify that your build file is set up correctly

The Gradle buildpack will run different build tasks depending on the framework it detects. If your framework is Spring Boot, it will run ./gradlew build -x test. For Ratpack, it will run ./gradlew installDist -x test. If no known web frameworks are detected, it will run ./gradlew stage.

To create a custom task, you can create a stage task in your build.gradle file like this:

task stage(dependsOn: ['build', 'clean'])
build.mustRunAfter clean

You can also override the default task by setting the GRADLE_TASK environment variable either on the dashboard or using the CLI tool:

scalingo env-set GRADLE_TASK="build"

Default web process type

Depending on the detected framework, this buildpack will automatically create a web process. For Spring Boot, the web process will be:

java -Dserver.port=$PORT $JAVA_OPTS -jar build/libs/*.jar

For Ratpack, the web process will be:

build/install/${rootProject.name}/bin/${rootProject.name}

with ${rootProject.name} the configured value in your Gradle build (probably in your settings.gradle).

If you need to customize the default web process, you must create a Procfile.

Using Grails 3

Grails 3 requires both to have webapp-runner and to setup a custom stage task in your build.gradle. First, add the following line in the dependencies section:

compile "com.github.jsimone:webapp-runner:8.5.5.0"

Then add this at the end of your build.gradle:

task stage() {
    dependsOn clean, war
}
tasks.stage.doLast() {
    delete fileTree(dir: "build/distributions")
    delete fileTree(dir: "build/assetCompile")
    delete fileTree(dir: "build/distributions")
    delete fileTree(dir: "build/libs", exclude: "*.war")
}
war.mustRunAfter clean

task copyToLib(type: Copy) {
    into "$buildDir/server"
    from(configurations.compile) {
        include "webapp-runner*"
    }
}

stage.dependsOn(copyToLib)

Eventually, create a Procfile at the root of your project with this web process:

web: cd build ; java -Dgrails.env=prod -jar ../build/server/webapp-runner-*.jar --expand-war --port $PORT ./libs/*.war

Moving into the build directory is mandatory for Grails.

Example

A live example is available here with the source code on GitHub.


Suggest edits

Gradle

©2024 Scalingo