Asakusa Gradle Plugin に触れてみよう

この記事は Asakusa Framework Advent Calendar 2014 の 12/10 の記事です.

Asakusa Framework のことは名前と用途くらいは知ってるものとして, Asakusa Gradle Plugin について書いていきます.

基本的に Asakusa Gradle Plugin利用ガイド の抜粋みたいな文章なので, そちらも参考に読んでみてください.

Asakusa Gradle Plugin とは

コンパイルが必要な言語でのソフトウェアの開発の流れは、

  1. ファイルにプログラムを記述
  2. コンパイル
  3. テスト
  4. リリース用にビルド

となると思います.

Asakusa バッチアプリケーションを開発する上で, このリストの 2, 3, 4 番の項目に 1 つ加えた

  • 開発環境の準備
  • コンパイル
  • テスト
  • リリース用にビルド

の 4 つの作業をサポートするのが Asakusa Gradle Plugin (以下 Gradle Plugin) です.

そこで使っているビルドツール (正確にはビルドツールのための汎用フレームワーク?) は Gradle です. ここでは Gradle には深入りしないので, ユーザガイド や書籍に当たってみてください.

先程の 4 つの作業をどうサポートするかについて, これから (おそらく数記事に分けて) 解説していきます. CLI 編と GUI 編の解説が必要なのですが, まずは CLI 編から入っていきます.

まずは実行しよう

何はともあれ Gradle Plugin を触ってみましょう.

Asakusa を使ったサンプルプロジェクト asakusa-example-project-0.7.1.tar.gz が公開されているので, これに対して Gradle Plugin を使っていきます.

## java はインストールされてるものとします
$ java -version
java version "1.7.0_21"
Java(TM) SE Runtime Environment (build 1.7.0_21-b12)
Java HotSpot(TM) 64-Bit Server VM (build 23.21-b01, mixed mode)

## 環境変数 JAVA_HOME も設定しておいてください
$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_21.jdk/Contents/Home

## 開発用ディレクトリに移動
$ cd どこか適当なディレクトリ

## サンプルプロジェクトをダウンロード. 手動でも可.
$ wget http://www.asakusafw.com/download/gradle-plugin/asakusa-example-project-0.7.1.tar.gz
## もしくは
$ curl -O http://www.asakusafw.com/download/gradle-plugin/asakusa-example-project-0.7.1.tar.gz

## ファイルを展開
$ tar xvf asakusa-example-project-0.7.1.tar.gz

## プロジェクトディレクトリに移動
$ cd asakusa-example-project/

ここまで上手く行けば, Gradle Plugin を使う環境が整いました. さっそく次のコマンドを打って Gradle Plugin でできることを見ていきましょう.

$ ./gradlew tasks
Downloading http://services.gradle.org/distributions/gradle-2.1-bin.zip
..........................(中略)
:tasks

------------------------------------------------------------
All tasks runnable from root project
------------------------------------------------------------

Asakusa Framework Build tasks
-----------------------------
compileBatchapp - Compiles the Asakusa DSL java source with Asakusa DSL Compiler.
compileDMDL - Compiles the DMDL scripts with DMDL Compiler.
generateHiveDDL - Generates Hive DDL file from Data Models [Experimental].
generateTestbook - Generates the template Excel books for TestDriver.
generateThunderGateDataModel - Executes DDLs and generates ThunderGate data models.
jarBatchapp - Assembles a jar archive containing compiled batch applications.
summarizeYaessJob - Analyzes YAESS job execution from log file [Experimental].
testRunBatchapp - Executes Asakusa Batch Application [Experimental].

Asakusa Framework Organizer tasks
---------------------------------
assembleAsakusafw - Assembles a tarball containing framework files for deployment.
attachBatchapps - Attaches batch applications to assemblies.
attachComponentCore - Attaches framework core components to assemblies.
attachComponentDevelopment - Attaches development tools to assemblies.
attachComponentDirectIo - Attaches Direct I/O components to assemblies.
attachComponentExtension - Attaches framework extension components to assemblies.
attachComponentOperation - Attaches operation tools to assemblies.
attachComponentTesting - Attaches testing tools to assemblies.
attachComponentThunderGate - Attaches ThunderGate components to assemblies.
attachComponentWindGate - Attaches WindGate components to assemblies.
attachComponentWindGateSsh - Attaches WindGate SSH components to assemblies.
attachComponentYaess - Attaches YAESS components to assemblies.
attachComponentYaessHadoop - Attaches Yaess Hadoop bridge components to assemblies.
attachExtensionDirectIoHive - Attaches Direct I/O Hive extensions to assemblies.
attachExtensionWindGateRetryable - Attaches WindGate retryable extensions to assemblies.
attachExtensionYaessJobQueue - Attaches YAESS JobQueue client extensions to assemblies.
attachExtensionYaessTools - Attaches YAESS extra tools to assemblies.
cleanAssembleAsakusafw - Deletes the assembly files and directories.
installAsakusafw - Installs Asakusa Framework to $ASAKUSA_HOME using 'dev' profile.
updateAsakusafw - Updates Asakusa Framework on $ASAKUSA_HOME using 'dev' profile.

Build tasks
-----------
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles classes 'main'.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles classes 'test'.

Build Setup tasks
-----------------
init - Initializes a new Gradle build. [incubating]

Documentation tasks
-------------------
javadoc - Generates Javadoc API documentation for the main source code.

Help tasks
----------
components - Displays the components produced by root project 'asakusa-example-project'.
dependencies - Displays all dependencies declared in root project 'asakusa-example-project'.
dependencyInsight - Displays the insight into a specific dependency in root project 'asakusa-example-project'.
help - Displays a help message
projects - Displays the sub-projects of root project 'asakusa-example-project'.
properties - Displays the properties of root project 'asakusa-example-project'.
tasks - Displays the tasks runnable from root project 'asakusa-example-project'.

IDE tasks
---------
cleanEclipse - Cleans all Eclipse files.
eclipse - Generates all Eclipse files.

Verification tasks
------------------
check - Runs all checks.
test - Runs the unit tests.

Other tasks
-----------
wrapper

Rules
-----
Pattern: clean<TaskName>: Cleans the output files of a task.
Pattern: build<ConfigurationName>: Assembles the artifacts of a configuration.
Pattern: upload<ConfigurationName>: Assembles and uploads the artifacts belonging to a configuration.
Pattern: attachConf<Target>: Attaches asakusafw custom distribution files to assembly.

To see all tasks and more detail, run with --all.

BUILD SUCCESSFUL

Total time: 49.6 secs

こんな出力が出ていたら Gradle Plugin は無事動いています! 初めての Gradle Plugin の実行成功おめでとう!!

どうも上手く動いていないようなら, 環境変数 JAVA_HOME の設定や各コマンドが異常終了していないか echo $? で 1 つ 1 つ確かめてみてください.

出力の解説

如何せん出力が多いので, 必要なところから解説していきます.

この ./gradlew tasks というコマンドで出力したのはタスクの一覧です. タスクとはその名の通り「何らかの作業」です. ./gradlew タスク名 という形式で Gradle に何らかの作業を指示します. 先程のコマンドを例に出せば, tasks という名前のタスクはタスク一覧を出す処理を行うものでした.

Asakusa Framework Build tasks
-----------------------------

とある下に並んでいるのは Gradle Plugin が提供する Asakusa プロジェクト用のタスクです. これらのタスクは, 最初のリストで上げた作業のうち主に

  • コンパイル
  • テスト

に関わる作業をサポートします.

Asakusa Framework Organizer tasks
---------------------------------

とある下に並んでいるのも, 同じく Gradle Plugin が提供する Asakusa プロジェクト用のタスクです. こちらのタスクは主に

  • 開発環境の準備
  • リリース用にビルド

のサポートを受け持ちます.

他にもタスクはありますが, いっぺんに説明しても混乱すると思うので, まずはこのくらいにしておきます.

では

  • Asakusa Gradle Plugin とは何ぞや? 何のためにあるの?
  • Gradle Plugin の実行方法
  • Gradle には「タスク」というものがあり, Asakusa 用のタスクを使って Asakusa バッチアプリケーションを開発していく

あたりを伝えられたと思うので, 今日はこのくらいで.