JetBrains Space Help

Maven for Java and Kotlin


  • You have a project written in Java or Kotlin.

  • Your project uses Maven.

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

Eligible images

The typical task when working with a Maven project is to build the project and run tests, and publish Maven artifacts after that. Space Automation doesn't provide any helper functions for Maven, therefore your Automation scripts must directly use the mvn tool.

Build the project and run tests

To build a project and run tests, you should run the mvn clean install command. This is how you can do this in .space.kts:

job("Build and run tests") { container(displayName = "Run mvn install", image = "maven:latest") { shellScript { content = """ mvn clean install """ } } }

Publish Maven artifacts to Space Packages

To publish artifacts of a Maven project to a Packages repository, you should configure artifact properties, reference a repository, and specify authentication credentials. This configuration is performed in the pom.xml file. The problem is that for security reasons, user credentials for a Maven repository must be stored not in the project directory but in the user's directory in a separate settings.xml file. The solution is to put settings.xml to the project directory and provide user credentials through environment variables.

  1. Make sure your project has a Maven repository.

  2. Configure repository settings in the project's pom.xml. Typically, all you should do is add the distributionManagement node with the Space Packages repository parameters:

    <!-- other content --> <distributionManagement> <repository> <id>space-maven</id> <!-- provide url via the 'repositoryUrl' command-line arg --> <url>${repositoryUrl}</url> </repository> </distributionManagement> <!-- other content -->
  3. Create settings.xml in the project root. The file should contain credentials to the Packages repository which we will provide as command-line arguments:

    <!-- other content --> <settings> <servers> <server> <id>space-maven</id> <!-- provide credentials via the command-line args: --> <!-- 'spaceUsername' and 'spacePassword' --> <username>${spaceUsername}</username> <password>${spacePassword}</password> </server> </servers> </settings>
  4. In the project root, create the .space.kts file with the build script:

    job("Build, run tests, publish") { container(displayName = "Run publish script", image = "maven:3-openjdk-8-slim") { // url of a Space Packages repository env["REPOSITORY_URL"] = "" shellScript { content = """ echo Build and publish artifacts... set -e -x -u mvn versions:set -DnewVersion=1.0.${'$'}JB_SPACE_EXECUTION_NUMBER mvn deploy -s settings.xml \ -DrepositoryUrl=${'$'}REPOSITORY_URL \ -DspaceUsername=${'$'}JB_SPACE_CLIENT_ID \ -DspacePassword=${'$'}JB_SPACE_CLIENT_SECRET """ } } }

    Here we:

    • Provide the repository URL through the REPOSITORY_URL environment variable.

    • Specify that settings.xml are stored in the project root directory.

    • Use the JB_SPACE_EXECUTION_NUMBER variable to specify the artifact version as 1.0.build_number.

    • Provide Space Automation credentials through the JB_SPACE_CLIENT_ID and JB_SPACE_CLIENT_SECRET environment variables.

Publish 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 and 2 from Publish 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. Edit the .space.kts file:

    job("Build, run tests, publish") { container(displayName = "Run publish script", image = "maven:3-openjdk-8-slim") { env["REPOSITORY_URL"] = "" env["USERNAME"] = Params("repo_user") env["PASSWORD"] = Secrets("repo_password") shellScript { content = """ echo Build and publish artifacts... set -e -x -u mvn versions:set -DnewVersion=1.0.${'$'}JB_SPACE_EXECUTION_NUMBER mvn deploy -s settings.xml \ -DrepositoryUrl=${'$'}REPOSITORY_URL \ -DspaceUsername=${'$'}JB_SPACE_CLIENT_ID \ -DspacePassword=${'$'}JB_SPACE_CLIENT_SECRET """ } } }

Last modified: 15 December 2023