JetBrains Space EAP Help

Publish Artifacts from a Gradle Project

Let's assume you have a project that uses Gradle build tool and want to publish project artifacts to the newly created Maven repository using:

Publish Maven artifacts using Gradle command-line tool

Basically, to be able to publish artifacts of a Gradle project, you should configure artifact properties, reference a repository, and specify authentication credentials. This configuration is performed in the build.gradle file.

  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 repositories sub-section, add the repository settings including the credentials and the URL. As it's not secure to explicitly specify credentials in build.gradle, you should use variables, which in our example are $usr and $pwd. We'll assign values to these variables in the next step.

    publishing{ ... repositories { maven { credentials { username = "$usr" password = "$pwd" } url = "https://maven.pkg.jetbrains.space/mycompany/p/projectkey/my-maven-repo" } } }

  5. You can set variable values in the gradle.properties file, which is stored locally on your machine. The default location of this file is:

    • on Windows: %HOMEPATH%\.gradle\gradle.properties

    • on Linux / macOS: ~/.gradle/gradle.properties

    Open gradle.properties and specify variable values. Here you must use the credentials of either your own Space account (using a permanent token instead of a password is strongly recommended) or a separate service account:

    usr = admin pwd = 1234

  6. That's it! Now, we can publish project artifacts by using, for example, the Gradle command-line tool or project's Gradle wrapper.

    gladlew publish

Publish Maven artifacts from JetBrains TeamCity

Before configuring TeamCity, make sure your project is configured as described in Publish Maven artifacts using Gradle command-line tool. If it's done, then all you need is to authenticate TeamCity in Space and add a gradle publish build step.

  1. Log in to your JetBrains TeamCity instance.

  2. In Administration, in project settings, open the desired build configuration.

  3. In Parameters, add two system properties for the username and the password. Note that property names must match the corresponding variable names from build.gradle. For example:

    • system.usr (if in build.gradle, you've specified username = $usr).

      Set Space username in TeamCity
    • system.pwd (if in build.gradle, you've specified password = $pwd). In Spec, type password. This will hide the password from logs, reports, and so on.

      Set Space password in TeamCity

    Finally, you should get something like this:

    Space Packages credentials in TeamCity
  4. Add a Gradle build step that runs gradle publish. No additional configuration is needed:

    Gradle build step in TeamCity

Publish Maven artifacts from Space Automation

Before configuring Space Automation, make sure your project is configured as described in Publish Maven artifacts using Gradle command-line tool (everything except the repositories section). The main trick is that Automation has its own credentials for getting authenticated in Packages. You can specify these credentials using two environment variables, JB_SPACE_CLIENT_ID and JB_SPACE_CLIENT_SECRET.

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

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

    publishing { repositories { maven { credentials { 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" } } }

  3. Open or create the project's automation script file .space.kts.

  4. In .space.kts, add a job that runs publishing. For example:

    job("Build and publish") { container("gradle") { kotlinScript { api -> api.gradle("build") api.gradle("publish") } } }

  5. Commit and push .space.kts to the Git repository. By default, this should trigger script run.

Last modified: 01 September 2020