Java with Bitbucket Pipelines

This guide shows you how to use Bitbucket Pipelines for building and testing a Java software project in a Docker container, using either Maven or Gradle as your build tool.

If you'd prefer to quickly import a demo repository with a working pipeline to experiment with, have a look at our demo java repo.
Instructions to import the demo repo

For this demo, all you need to do is import this repo and enable pipelines to get your first pipeline to run!

Note: Make sure you have Maven installed.

  1. Import this repository by visiting bitbucket.org and log in. Then:

    Import gif

  2. On the left navigation bar, click Pipelines and scroll down to view the bitbucket-pipelines.yml file. Click Enable

    Enable gif

  3. Watch your build run!

    Build gif

Note: Bitbucket Pipelines includes fifty free minutes per account, at the time of writing. You can check your team or account's minutes usage for the month by clicking your Avatar > Bitbucket settings > Plan details.




If you'd prefer to set it up by hand, most of the configuration happens in the bitbucket-pipelines.yml file that Pipelines uses to define the build.

Build and test 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

You can check your bitbucket-pipelines.yml file with our online validator.

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/.

Build and run 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 two secure variables, MAVEN_USERNAME and MAVEN_PASSWORD, so we can pass these values 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

Build and test Gradle projects

Add the Gradle Wrapper to your repository

Our recommended approach (and Gradle's) to building Gradle projects is to commit the Gradle Wrapper to your repository.

The Gradle wrapper ensures two things:

  • the build environment doesn't need to have Gradle manually installed to build your project
  • your project is always built with the same Gradle version

We recommend making the wrapper script executable before committing it to your repository:

chmod +x gradlew

Then you can build your Gradle project with the wrapper:

bitbucket-pipelines.yml
image: openjdk:8
pipelines:
  default:
    - step:
        script:
          - bash ./gradlew build


Remember, you can check your bitbucket-pipelines.yml file with our online validator.

Last modified on Jan 2, 2019

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.