Java と Bitbucket Pipelines
このガイドでは、Maven または Gradle をビルド ツールとして使用し、Bitbucket Pipelines を使用して、Docker コンテナで Java ソフトウェア プロジェクトをビルドおよびテストする方法について説明します。
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.
手動でセットアップしたい場合、構成の大部分は Pipelines がビルドを定義するために使用する bitbucket-pipelines.yml ファイル内で行います。
Maven プロジェクトのビルドとテスト
Docker で Maven のバージョンを指定する
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.
たとえば、Maven 3.3.9 (この記事の執筆時点での最新バージョン) を bitbucket-pipelines.yml ファイルの先頭で指定して使用できます。
image: maven:3.3.9
pipelines:
default:
- step:
script:
- mvn -versionアトラシアンのオンライン バリデーターでbitbucket-pipelines.ymlファイルを確認できます。
Java および JDK バージョン
既定では、Maven イメージには Java JDK の最新の公式バージョンが含まれますが、特定の Maven バージョンを使用することで別のバージョンを指定することもできます。以下は、JDK 1.7 を搭載した Maven 3.2.5 の Docker イメージの使用例です。
image: maven:3.2.5-jdk-7You can find a list of available Maven/JDK versions and corresponding image tags, refer to https://hub.docker.com/r/library/maven/.
Maven を使用したビルドとテストの実行
Docker イメージを選択したら、Pipelines の script セクションに適切な Maven コマンドを含めるだけでテストを実行できます。例を次に示します。
image: maven:3.3.9
pipelines:
default:
- step:
script:
- mvn -B verify # -B batch mode makes Maven less verboseMaven のプライベート リポジトリ
Maven のプライベート リポジトリにアクセスするには、Maven の既定の settings.xml (Docker イメージの /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:
main:
- step:
script:
- bash configure-maven.sh
- mvn -B verifyconfigure-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.xmlMaven の settings.xml のカスタマイズ
sed を使用して既定の settings.xml にさらに高度な変更を加えることは非常に難しいため、独自の settings.xml を Bitbucket リポジトリにコミットすることをお勧めします。その後、Maven の -s フラグを使用してそれを参照できます。
image: maven:3.3.9
pipelines:
default:
- step:
script:
- mvn -s settings.xml # e.g. with settings.xml in the root of the projectGradle プロジェクトのビルドとテスト
リポジトリに Gradle Wrapper を追加する
Our recommended approach (and Gradle's) to building Gradle projects is to commit the Gradle Wrapper to your repository.
Gradle Wrapper は、次の 2 つを保証します。
プロジェクトをビルドするための、ビルド環境への Gradle の手動インストールは不要
プロジェクトは常に同じ Gradle バージョンでビルドされる
wrapper スクリプトは、リポジトリにコミットする前に実行可能にしておくことをおすすめします。
chmod +x gradlewその後、wrapper を使用して Gradle プロジェクトをビルドできます。
bitbucket-pipelines.yml
image: openjdk:8
pipelines:
default:
- step:
script:
- bash ./gradlew buildbitbucket-pipelines.ymlファイルはアトラシアンのオンライン バリデーターで確認できることを覚えておいてください。
この内容はお役に立ちましたか?