Gradle builds with private Maven repositories on Bitbucket Pipelines

It’s common to use private Maven repositories to host internal artifacts. These repositories usually require authentication when accessing. The username and password for these repositories are stored in the ~/.m2/settings.xml. This post shows how can we configure Gradle to publish to private repositories and integrate with Bitbucket Pipelines or other CI services.

Configure Gradle to use Maven settings.xml

The Maven settings.xml already contains user authentication information, so we can use it when publishing. We’ll use the plugin maven-publish for publishing and maven-publish-auth for retrieving the authentication information.

In the build.gradle file below, gradle-maven-publish-auth is the plugin to retrieve authentication information. The repository snapshots is the private repository for publishing.

When we run gradle publish, the built artifact is published to the Maven repository.

Integrate with CI services

To integrate with CI services, we need to configure the build environment for using the private Maven repository. For Bitbucket Pipelines, it’s possible to use mvn -s to specify the path of settings.xml. However, this doesn’t work for Gradle builds. For Gradle builds, it’s also possible to use the environment variable maven.settings to specify the path of settings.xml. But this requires each repository to have its own copy of settings.xml file.

A better choice is to use a custom Maven Docker image that includes the settings.xml.