8
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gradle 覚書

Last updated at Posted at 2017-10-15

Gradle を手っ取り早く使うための手順

少し使えるようになったら、以下を読むとぐっと理解が深まります(groovy知らない人は特に)

Groovyを知らない人のためのbuild.gradle読み書き入門

インストール

https://gradle.org/install/#manually からバイナリをダウンロードして展開、bin ディレクトリにパスを通して終わり

export GRADLE_HOME=・・・
export PATH="${GRADLE_HOME}/bin:${PATH}"

ディレクトリ構成

以下の通りディレクトリを作る。Maven のプラグイン開発に則ってるみたいだけど、深く考えない。トップディレクトリが jar の名前になる

[トップディレクトリ]
  +-- build.gradle
  +-- src/
      +-- main/
          +-- groovy/
          +-- java/
          +-- resources/
      +-- test/
          + main 配下と同じなので略

main 配下の役割は以下の通り

  • build.gradle: make の Makefile。ant の build.xml に相当するファイル
  • groovy: groovy ファイルを入れる
  • java: Java のソースコード
  • resources: property、画像(アイコン)などを入れる。gradle で run するときの classpath として自動的にはいる。この中のディレクトリファイルは本体の jar にも含まれるので、含めたくないものはここに置かないようにする

コンパイル

main/src/java に適当なコードをつっこんで、build.gradle ファイルを以下一行を記載

build.gradle
apply plugin: 'java'

gradle build を実行

トップディレクトリ配下に build ディレクトリが作成され、色々ファイルができてる。すごい。この時点で ant との決別を決意

build
|-- classes
|   `-- java
|       `-- main
|           `-- Test.class
|-- distributions
|   |-- sandbox.tar
|   `-- sandbox.zip
|-- libs
|   `-- sandbox.jar
|-- scripts
|   |-- sandbox
|   `-- sandbox.bat
`-- tmp
    |-- compileJava
    `-- jar
        `-- MANIFEST.MF

クラスパスの追加

dependencies {
  compile fileTree(dir: 'libs', include: '*.jar')
  runtime files('src/main/dist/lib/properties')
}
  • compile に指定したものは、一律配布物の lib ディレクトリに含めてくれる
  • runtime は含めない(含めたい場合は以下)

実行

build.gradle に以下二行を追加し、gradle run を実行するだけ

apply plugin: 'application'
mainClassName = '実行したいクラス名'

プロパティファイルなど、classpath 探索するものがあれば、resources ディレクトリに入れておく
配布物に影響があるので、dependencies を使って適宜設定
ただ動かすだけなら resources に突っ込んでおけば OK

実行時の classpath の追加は以下を build.gradle に追加(以下はディレクトリを追加した例)。ここは普通に filetree など、ご自由に

dependencies {
   runtime files('src/main/dist/lib/properties')
}

配布物

build/distributions/ 配下にある tar(zip) ファイルが必要なものがjar,起動シェルなど一式が入ったファイル。これを展開すればよい

Java のソースコードだけであれば、内容は以下の通り

# tar tvf build/distributions/sandbox.tar
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/lib/
-rw-r--r-- 0/0             885 2017-10-14 09:54 sandbox/lib/sandbox.jar
drwxr-xr-x 0/0               0 2017-10-14 09:54 sandbox/bin/
-rwxr-xr-x 0/0            5210 2017-10-14 09:54 sandbox/bin/sandbox
-rwxr-xr-x 0/0            2180 2017-10-14 09:54 sandbox/bin/sandbox.bat

この配布物に property ファイルを追加する場合の手順
結論は、配布物に以下構成で property ファイルを追加

+-- sandbox
    +-- lib/properties/~~.properties <-- 追加
    +-- bin/

lib ディレクトリ配下ってのが、うーんという感じだが、build.gradle をあまりいじらないでやるにはこれがいいっぽい

やりかた
property ファイルは以下の通り配置する

[トップディレクトリ]
  +-- build.gradle
  +-- src/
      +-- main/
          +-- dist/
              +-- lib/
                  +-- properties/
                      + ~~.properties

build.gradle に以下追加

build.gradle
startScripts {
    classpath += files('dist/lib/properties')
}

よくわからないけど、files('dist/properties') でもできる。
dist 配下は、dist/lib/~~~ にしないとダメ。lib ディレクトリぬかすと、配布物の直下ディレクトリに properties ディレクトリができちゃう

gradle build すれば、最初に書いた通り、lib/properties ディレクトリに含めてくれ且つ、起動 script の CLASSPATH にも追加してくれる

この起動 script への CLASSPATH の追加がネックで。$APP_HOME/lib/~~ になってしまうので、この構成にするしかない、、、

以前は dist と resources でシンボリックリンク案も書いたが、resources ディレクトリの物は本体の jar ファイルの中に含まれてしまうので、property を外出ししたい場合は NG
ただし、運用中にいじらない property なら resources ディレクトリに突っ込んで OK

8
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?