Maven for Java and Kotlin
Prerequisites
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
:
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.
Make sure your project has a Maven repository.
Configure repository settings in the project's
pom.xml
. Typically, all you should do is add thedistributionManagement
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 -->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>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"] = "https://maven.pkg.jetbrains.space/mycompany/p/key/my-maven-repo" 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 as1.0.build_number
.Provide Space Automation credentials through the
JB_SPACE_CLIENT_ID
andJB_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.
Repeat steps 1 and 2 from Publish Maven artifacts to Space Packages.
Create a parameter and a secret for storing the username and password that the script must use to access the external repository.
Edit the
.space.kts
file:job("Build, run tests, publish") { container(displayName = "Run publish script", image = "maven:3-openjdk-8-slim") { env["REPOSITORY_URL"] = "https://externalrepo.example.com" 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 """ } } }