BazelでJavaのプロジェクトをビルドする
概要
- Bazelを実際に使って見るためにインストールとサンプルプロジェクトのビルドを試してみる。
Bazel?
BazelはGoogleで使われているビルドツールBlazeのOSS版。巨大なソースコードのビルド、テストおよびリリースの要件を満たすように設計されたビルドツールで、迅速かつ確実に巨大なソースコードをビルドすることが可能ならしい。現在α版。
Bazelの特徴
- Fast
- キャッシュと並列処理でビルドを高速化
- Reproducible
- ビルドサーバ、開発機問わずbitwise-identicalなバイナリをビルド
- Flexible
- 他言語サポート、同じコードベースでサーバ、クライアント両方のビルドに対応
なぜBazelを試してみるのか
- AndroidでのGradleの次のビルドシステム(と勝手に思っている)
- Gradleチームの見解
- ロードマップを見ると2015/6月にiOS、Androidのビルドをサポートする
- 未確定だがAndroid Studio互換がロードマップに記載されている
- Goのサポートも未確定だが予定されている
- Androidのビルドが高速化される可能性
- AndroidのビルドがBazelを使うことで高速になるかもしれない
- もし高速化されれば開発効率が大幅にアップ
- Jakeが言及していた。
I can finally talk about Bazel! Check it out at http://t.co/dd6LMJxS8Z
— Jake Wharton (@JakeWharton) March 24, 2015
Bazel実行環境の用意
- http://bazel.io/docs/install.htmlを参考に進める
- Macで実行可能だが、ローカルを汚したくないのでDockerでUbuntu環境を構築する
Ubuntu環境の構築
MacでDockerを利用するための環境を構築
boot2dockerのインストール
- homebrewでもインストールできるようだが公式サイトのやり方でインストールする
- osx-installerを開く
Boot2Docker-1.6.0.pkgを実行しインストール
- Boot2Docker VMの作成
$ boot2docker init
- boot2docker VMの起動
$ boot2docker start
- Dockerクライアント用の環境変数が表示される
$ boot2docker start Waiting for VM and Docker daemon to start... ...........................oooooooooooooooo Started. Writing /Users/horie1024/.boot2docker/certs/boot2docker-vm/ca.pem Writing /Users/horie1024/.boot2docker/certs/boot2docker-vm/cert.pem Writing /Users/horie1024/.boot2docker/certs/boot2docker-vm/key.pem To connect the Docker client to the Docker daemon, please set: export DOCKER_HOST=tcp://192.168.59.103:2376 export DOCKER_CERT_PATH=/Users/horie1024/.boot2docker/certs/boot2docker-vm export DOCKER_TLS_VERIFY=1
- shellの設定ファイルに以下を記述。手動で
export
しても問題は無い。
eval "$(boot2docker shellinit)"
hello-world
コンテナを起動
$ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from hello-world ・ ・ ・ Hello from Docker. This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (Assuming it was not already locally available.) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash For more examples and ideas, visit: http://docs.docker.com/userguide/
$ boot2docker ssh ## . ## ## ## == ## ## ## ## ## === /"""""""""""""""""\___/ === ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~ \______ o __/ \ \ __/ \____\_______/ _ _ ____ _ _ | |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __ | '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__| | |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ | |_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_| Boot2Docker version 1.6.0, build master : a270c71 - Thu Apr 16 19:50:36 UTC 2015 Docker version 1.6.0, build 4749651
Ubuntuコンテナを起動してみる
- dockerコマンドで起動
$ docker run -it ubuntu bash
- 起動できた
$ docker run -it ubuntu bash Unable to find image 'ubuntu:latest' locally Pulling repository ubuntu b7cf8f0d9e82: Download complete 706766fe1019: Download complete a62a42e77c9c: Download complete 2c014f14d3d9: Download complete Status: Downloaded newer image for ubuntu:latest root@3kdi85o3d:/#
- Ubuntuのバージョンを確認
root@3kdi85o3d:/# cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=14.04 DISTRIB_CODENAME=trusty DISTRIB_DESCRIPTION="Ubuntu 14.04.2 LTS"
Bazelのインストール
バイナリインストールのサポートはまだ無い(後日サポートするらしい)。http://bazel.io/docs/install.htmlを参考に進める
- aptリポジトリの追加
$ sudo add-apt-repository ppa:webupd8team/java sudo: add-apt-repository: command not found
add-apt-repositoryが無いと言われ失敗。
- software-properties-commonをインストールすると解決
$ sudo apt-get install software-properties-common
- apt-getをアップデート
$ sudo apt-get update
- Java8のインストール
$ sudo apt-get install oracle-java8-installer
- JAVA_HOMEの設定
$ export JAVA_HOME=/usr/lib/jvm/java-8-oracle
- 必要なパッケージのインストール
$ sudo apt-get install libarchive-dev pkg-config zip g++ zlib1g-dev
- Bazelをダウンロード
$ git clone https://github.com/google/bazel/
- Bazelのインストール
$ cd bazel $ ./compile.sh
- PATHを通す
$ export PATH="$PATH:$HOME/bazel/output"
- インストールできた
$ bazel Extracting Bazel installation... Apr 25, 2015 7:15:15 AM com.google.devtools.build.lib.runtime.BlazeRuntime batchMain INFO: Running Blaze in batch mode with startup args [--batch, --install_base=/root/.cache/bazel/_bazel_root/install/e3f3e466c8e7784303e1f835508f8561, --output_base=/root/.cache/bazel/_bazel_root/d41d8cd98f00b204e9800998ecf8427e, --workspace_directory=, --nofatal_event_bus_exceptions, --option_sources=] [bazel development version] Usage: bazel <command> <options> ... Available commands: analyze-profile Analyzes build profile data. build Builds the specified targets. canonicalize-flags Canonicalizes a list of bazel options. clean Removes output files and optionally stops the server. doc_ext Prints help for commands, or the index. help Prints help for commands, or the index. info Displays runtime info about the bazel server. query Executes a dependency graph query. run Runs the specified target. shutdown Stops the bazel server. test Builds and runs the specified test targets. version Prints version information for bazel. Getting more help: bazel help <command> Prints help and options for <command>. bazel help startup_options Options for the JVM hosting bazel. bazel help target-syntax Explains the syntax for specifying targets. bazel help info-keys Displays a list of keys used by the info command.
- versionをみてみる
$ bazel version Apr 25, 2015 7:20:22 AM com.google.devtools.build.lib.runtime.BlazeRuntime batchMain INFO: Running Blaze in batch mode with startup args [--batch, --install_base=/root/.cache/bazel/_bazel_root/install/e3f3e466c8e7784303e1f835508f8561, --output_base=/root/.cache/bazel/_bazel_root/d41d8cd98f00b204e9800998ecf8427e, --workspace_directory=, --nofatal_event_bus_exceptions, --option_sources=] ERROR: Version information not available.
ERRORでた・・
サンプルプロジェクトのビルド
インストールが正常に行われたか確認うるためにサンプルプロジェクトをビルドする。
- プロジェクトディレクトリの作成
$ mkdir my-project
- WORKSPACEファイルを作成
$ cd my-project $ touch WORKSPACE
- bazelインストール時にcloneしてきたディレクトリからexampleをコピー
$ cp -r $HOME/bazel/examples .
- ビルドの実行
初回実行時にbazel-binやbazel-outといったリンクが作られる。
$ bazel build examples/java-native/src/main/java/com/example/myproject:hello-world INFO: Found 1 target... Target //examples/java-native/src/main/java/com/example/myproject:hello-world up-to-date: bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world.jar bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world INFO: Elapsed time: 3.269s, Critical Path: 1.21s
- ビルド成果物の実行
ビルドによって作成されたバイナリはbazel-bin/以下に配置される。
$ bazel-bin/examples/java-native/src/main/java/com/example/myproject/hello-world Hello world
実行できた。Androidのビルドに対応したらまた試したい。
Docker Hubに登録
登録した。
https://registry.hub.docker.com/u/horie1024/bazel/registry.hub.docker.com