Configuring the maven settings.xml in the Bitbucket pipeline

Configuring the maven settings.xml in the Bitbucket pipeline
Photo by Quinten de Graaf / Unsplash

This week I was setting up a minimal Continuous Integration solution based on Java/Maven running on Bitbucket pipelines. I was looking for the cleanest way to integrate the maven settings.xml into the pipeline, I found some solutions in the internet but they were not as clean as I want. In this article I share with you a simple and clean solution to do it.

Environment variables

The first thing to do is to define a couple of variables in Bitbucket. My advice is to define them at the workspace level rather than the repository level.

  • MVN_SERVER1_USERNAME : Will contain the username of the server with the id SERVER1
  • MVN_SERVER1_PASSWORD : Will contain the password of the server with the id SERVER1
  • SETTINGS_XML : Will contain the settings.xml payload, BUT with placeholder for the server credentials(Yes, bitbucket Accept nested variables, good news !!!)
  • You can define as many servers credentials as you need...

    The server element in the settings.xml look like this :
<server>
    <id>SERVER1</id>
    <username>${MVN_SERVER1_USERNAME}</username>
    <password>${MVN_SERVER1_PASSWORD}</password>
</server>

Here are the environment variables in Bitbucket

Pipeline

The pipeline is straightforward and easy to understand :

image: maven:3.6.3-jdk-11

pipelines:
  default:      
      - step:
          name: Build and Test
          script:
            - echo $SETTINGS_XML > settings.xml
            - mvn -s settings.xml clean install

I hope this can help you in your Bitbucket pipelines journey :)

Salam