JetBrains Space Help

Java, Kotlin, Gradle

Prerequisites

  • You have a project written in Java or Kotlin.

  • Your project is configured to use Gradle. A Gradle wrapper gradlew exists in the project's root directory.

Eligible images

Gradle is one of the most popular tools for building Java and Kotlin projects. Typically, all you need to build a project and run tests is execute the gradle build command in the root directory of the project. There are three ways to do this in Automation:

Note that this topic does not provide instructions on how to publish build artifacts of a Gradle project. For the instructions, refer to Publish Maven artifacts from Space Automation

(Basic) Build the project and run tests using Gradle wrapper

It is the easiest way to build a Gradle project. You don't need to think about how things work, just specify a containter image with JRE/JDK and a command to run. For example:

job("Build and run tests") { gradlew("openjdk:11", "build") }

This job runs gradlew build using the Gradle wrapper from the project sources directory /mnt/space/work/${repo-name} (learn more). If the wrapper is not found, the job will fail.

Build the project and run tests using Kotlin script

Sometimes, your build process may contain complex logic written in Kotlin. In this case, you should run Gradle commands using the Space Gradle API. The best practice is to use the Gradle wrapper by calling the gradlew() function. This lets you use container images with no Gradle included (JRE/JDK is still required).

job("Build and run tests") { container("openjdk:11") { resources { memory = 768 } kotlinScript { api -> // here can be your complex logic api.gradlew("build") } } }

Where:

  • memory: sets the amount of memory for the container in MiB. The default value is 256, while we recommend at least 768 MiB for Gradle jobs.

Instead of the Gradle wrapper, you can use Gradle directly with the help of the gradle() API function. Note that in this case the container image must have Gradle installed.

job("Build and run tests") { container("gradle:jdk11") { resources { memory = 768 } kotlinScript { api -> // here can be your complex logic api.gradle("build") } } }

Build the project and run tests in a custom container image

In some cases, you may need to use a custom Docker image (e.g. stored in Packages) to perform the build. This scenario requires you to understand how Automation works under the hood. The main trick is to receive test run results and pass them to Automation. If you use gradlew command or api.gradle to run the build (as described above), this is solved by the special test results listener initialized in init.gradle. Thus, the bootstrap job (learn more) gets the additional step: It initializes Gradle using this init.gradle file. When you run a custom Docker container, you have to do this manually, i.e. add Gradle initialization to your Automation script:

job("Gradle build") { container("mycompany.registry.jetbrains.space/p/projectkey/mydocker/myimage") { shellScript { content = "gradle build --init-script $mountDir/system/gradle/init.gradle" } } }
Last modified: 20 November 2020