Tambourine作業メモ

主にスキル習得のためにやった作業のメモ。他人には基本的に無用のものです。

Gradleでjarを作る

ひとつずつやってみよう。まずは、サブプロジェクトの位置に直接build.gradleを置いて、jarを作ってみることである。プロジェクトにはsrcディレクトリだけがあり、その下には長い長いパッケージ名の彼方に20ぐらいのjavaコードがある。src/jp/co/hoge/fuga/...といった次第である。

とりあえず、中身はこれだけで始めてみよう

> cat build.gradle 
apply plugin: 'java'

タスクのリストを表示してみる

> gradle tasks

> Task :tasks

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

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 main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.

Build Setup tasks
-----------------
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.

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

Help tasks
----------
buildEnvironment - Displays all buildscript dependencies declared in root project 'BBusiness'.
components - Displays the components produced by root project 'BBusiness'. [incubating]
dependencies - Displays all dependencies declared in root project 'BBusiness'.
dependencyInsight - Displays the insight into a specific dependency in root project 'BBusiness'.
dependentComponents - Displays the dependent components of components in root project 'BBusiness'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'BBusiness'. [incubating]
projects - Displays the sub-projects of root project 'BBusiness'.
properties - Displays the properties of root project 'BBusiness'.
tasks - Displays the tasks runnable from root project 'BBusiness'.

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

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.

To see all tasks and more detail, run gradle tasks --all

To see more detail about a task, run gradle help --task <task>


BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed          
> 

ちなみにBBusinessは、今試しているディレクトリの名前だ。なぜこんな名前なのかは気にしないで欲しい。

一番上にあるタスクを実行してみようと思う。

> gradle assemble

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

ものすごく一瞬で終わった。怪しい。buildディレクトリが出来ているので、lsしてみる

> ls -lR
total 0
drwxr-xr-x  3 tambara  staff  102  9 11 18:28 libs
drwxr-xr-x  3 tambara  staff  102  9 11 18:28 tmp

./libs:
total 8
-rw-r--r--  1 tambara  staff  261  9 11 18:28 BBusiness.jar

./tmp:
total 0
drwxr-xr-x  3 tambara  staff  102  9 11 18:28 jar

./tmp/jar:
total 8
-rw-r--r--  1 tambara  staff  25  9 11 18:28 MANIFEST.MF

261バイト。いくらなんでも私のプロジェクトはそんなショボくない。

私は、まずやってみてから取説を読むタイプである。

http://gradle.monochromeroad.com/docs/userguide/tutorial_java_projects.html

Gradleは、製品のソースコードがsrc/main/javaに、テストのソースコードがsrc/test/javaにあることを想定しています。

ソースコードの位置を指定してやる必要があるようだ。

その方法は

http://gradle.monochromeroad.com/docs/userguide/java_plugin.html

に書いてある。参考にして、build.gradleを書き換えよう

apply plugin: 'java'

sourceSets {
  main {
    java {
      srcDir 'src'
    }
  }
}

やってみよう

> gradle assemble

> Task :compileJava
/BBusiness/src/jp/co/hoge/b/bp/BCommonBP.java:5: エラー: パッケージjp.co.hoge.z.common.exceptionは存在しません
import jp.co.hoge.z.common.exception.ZCAApplException;
(以下、たくさんのコンパイルエラー)

おなじみ、CLASSPATHの問題である。