JetBrains Space Help

Gradle for Java and Kotlin

Prerequisites

  • You have a project written in Java or Kotlin.

  • Your project uses Gradle. A Gradle wrapper gradlew exists in the project's root directory.

  • If you want to publish artifacts to Space Packages, make sure the project has a Maven repository.

Eligible images

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:

The next task after building the project is, typically, publishing build artifacts.

(Basic) Building the project and running tests with the Gradle wrapper

The easiest way to build a Gradle project is to use the Gradle wrapper in the project root. Note that the container image must have JRE/JDK 9 or later. For example:

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

If the wrapper is not found, the job will fail.

Building the project and running tests with a Kotlin script

If your build script contains complex logic written in Kotlin, 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(displayName = "Run gradle build", image = "openjdk:11") { kotlinScript { api -> // here can be your complex logic api.gradlew("build") } } }

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("displayName = "Run gradle build", image = gradle:jdk11") { kotlinScript { api -> // here can be your complex logic api.gradle("build") } } }

Building the project and running 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("Build and run tests") { container(displayName = "Run gradle build", image = "mycompany.registry.jetbrains.space/p/pkey/mydocker/myimage") { shellScript { content = "gradle build --init-script $mountDir/system/gradle/init.gradle" } } }

Publishing Maven artifacts to Space Packages

  1. Open the project's build.gradle file.

  2. Add the reference to the Maven plugin:

    apply plugin: 'maven-publish'

  3. In the publishing section, specify the package properties (the generated package name will be groupId:artifactId ). For example, we want to publish a .jar file (for the sake of length, the section content is reduced):

    publishing { publications { maven(MavenPublication) { groupId = 'org.company' artifactId = 'sample' version = "0.9-SNAPSHOT" from components.java pom { name = 'My Library' description = 'A description of my library' ... } } } }

  4. In the publishing section, in repositories, specify the repository URL and credentials using the environment variables:

    publishing { repositories { maven { credentials { // Automation has a special account for authentication in Space // account credentials are accessible via env vars username = "$System.env.JB_SPACE_CLIENT_ID" password = "$System.env.JB_SPACE_CLIENT_SECRET" } url = "https://maven.pkg.jetbrains.space/mycompany/p/projectkey/my-maven-repo" } } }

  5. Edit the project's .space.kts file. For example:

    job("Build and publish") { container(displayName = "Run publish script", image = "gradle") { kotlinScript { api -> api.gradle("build") api.gradle("publish") } } }

Publishing Maven artifacts to external repositories

Publishing artifacts to external Maven repositories is almost the same as publishing to a Space Packages repository. The only difference is that you should store credentials to the repository in the Secrets&Parameters storage.

  1. Repeat steps 1 – 3 from Publishing Maven artifacts to Space Packages.

  2. Create a parameter and a secret for storing the username and password that the script must use to access the external repository.

    Secrets and parameters
  3. In build.gradle, in the publishing, repositories, specify the URL of the external repository and credentials using the environment variables:

    publishing { repositories { maven { credentials { // we'll set these env vars in .space.kts username = "$System.env.USERNAME" password = "$System.env.PASSWORD" } url = "https://externalrepo.example.com/my-maven-repo" } } }

  4. Edit the project's .space.kts file. For example:

    job("Build and publish") { container(displayName = "Run publish script", image = "gradle") { // env vars for build.gradle env["USERNAME"] = Params("repo_user") env["PASSWORD"] = Secrets("repo_password") kotlinScript { api -> api.gradle("build") api.gradle("publish") } } }

Last modified: 11 March 2021