Java with Bitbucket Pipelines

Here's how you can configure the bitbucket-pipelines.yml file to build and test a Java project, using either Maven or Gradle as your build tool.

Building and testing Maven projects

Specify your Maven version with Docker

Bitbucket Pipelines runs all your builds in Docker containers using an image that you specify at the beginning of your configuration file. You can easily use Maven with Bitbucket Pipelines by using one of the official Maven Docker images on Docker Hub.

For instance, you can use Maven 3.3.9 (current version as of writing) by specifying it at the beginning of your bitbucket-pipelines.yml file.

image: maven:3.3.9
pipelines:
  default:
    - step:
        script:
          - mvn -version

Java and JDK versions

By default, the Maven image will include a recent official version of the Java JDK, but you can specify a different version instead by using a specific Maven version. Below is an example using the Docker image for Maven 3.2.5 with JDK 1.7.

image: maven:3.2.5-jdk-7

You can find a list of available Maven/JDK versions and corresponding image tags, refer to https://hub.docker.com/r/library/maven/.

Building and running tests with Maven

Once you have selected your Docker image, running tests is just including the appropriate Maven commands in your Pipelines script. Here's a simple example.

image: maven:3.3.9
pipelines:
  default:
    - step:
        script:
          - mvn -B verify    # -B batch mode makes Maven less verbose

Private Maven repositories

To access a private Maven repository, you'll need to override the default Maven settings.xml found in the Docker image at /usr/share/maven/conf/settings.xml.

Before using this example, configure secure environment variables for MAVEN_USERNAME and MAVEN_PASSWORD to pass these values through securely to the Pipelines configuration.

bitbucket-pipelines.yml
image: maven:3.3.9

pipelines:
  branches:
    master:
      - step:
          script:
            - bash configure-maven.sh
            - mvn -B verify
configure-maven.sh
#!/bin/bash

sed -i~ "/<servers>/ a\
<server>\
  <id>private-repo</id>\
  <username>${MAVEN_USERNAME}</username>\
  <password>${MAVEN_PASSWORD}</password>\
</server>" /usr/share/maven/conf/settings.xml

sed -i "/<profiles>/ a\
<profile>\
  <id>private-repo</id>\
  <activation>\
    <activeByDefault>true</activeByDefault>\
  </activation>\
  <repositories>\
    <repository>\
      <id>private-repo</id>\
      <url>https://example.com/path/to/maven/repo/</url>\
    </repository>\
  </repositories>\
  <pluginRepositories>\
    <pluginRepository>\
      <id>private-repo</id>\
      <url>https://example.com/path/to/maven/repo/</url>\
    </pluginRepository>\
  </pluginRepositories>\
</profile>" /usr/share/maven/conf/settings.xml

Custom Maven settings.xml

Making more advanced changes to the default settings.xml with sed can be quite painful, so it may be easier to commit your own settings.xml to your Bitbucket repository. Then you can reference it using Maven's -s flag.

image: maven:3.3.9
pipelines:
  default:
    - step:
        script:
          - mvn -s settings.xml   # e.g. with settings.xml in the root of the project

Building and testing Gradle projects

Coming soon.

Was this helpful?

Thanks for your feedback!

Why was this unhelpful?

Have a question about this article?

See questions about this article

Powered by Confluence and Scroll Viewport