JetBrains Space Help

Maven for Java and Kotlin


  • You have a project written in Java or Kotlin.

  • Your project is configured to use Maven.

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.

Building the project and running 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("maven:latest") { shellScript { content = """ mvn clean install """ } } }

Publishing 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. First, you should 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 -->
  2. Next, you should 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>
  3. Finally, you can create the .space.kts file with the build script:

    job("Build, run tests, publish") { container("maven:3-openjdk-8-slim") { // url of a Space Packages repository env["REPOSITORY_URL"] = "" shellScript { content = """ echo Build and run tests... mvn clean install echo Publish artifacts... 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.

Publishing Maven artifacts to external repositories

Publishing to external Maven repositories from Automation jobs doesn't differ from publishing to a Space Packages repository. The only difference that you should store credentials to the repository in the Secrets&Parameters storage.

  1. Repeat steps 1 and 2 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. Edit the .space.kts file:

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

Last modified: 29 January 2021