LoginSignup
2
1

More than 5 years have passed since last update.

Gradleで初心者が気付いて気持ちよかったことを列挙する #morning_kotlin

Last updated at Posted at 2019-01-26

もくもく朝活Kotlin でKtorのアプリをHerokuにデプロイするにあたり、初心者なりにGradleを学んだ結果をメモしていきます。

全般

./gradlew および ./gradlew.bat は、gradleを自動的にダウンロードするスクリプトに過ぎない

当初私は、 ./gradlewbuild.gradle をシェルで解釈して実行するのか!すごい!と思っていました。
もちろんそんなわけはなく、内部で./gradle/wrapper/gradle-wrapper.jarを呼んでいます。

タスクの実行は gradle より ./gradlew コマンドが推奨されているが普通に打つのが面倒臭い

第62章 Gradleラッパーより、
これらのファイルがプロジェクトに追加されたら、以後のビルドはこのgradlewコマンドで行ってください。 とあります。しかし、単にタイプ数が増えるしgradleディレクトリと名前が競合してtab補完が効かないしめんどくさい...

なんかいい方法ないですか?エイリアス?

タスク

ユーザー定義タスクの実行時、npm run *** とは異なり run が不要

gradleコマンドのサブコマンドに直接ユーザー定義コマンド指定できます。

npm run hello
pipenv run hello

gradle hello # シンプル!

gradleはタスクを複数引数に取れる

gradle hi hello

> Task :hi
Hi, world!

> Task :hello
Hello, world!

BUILD SUCCESSFUL in 1s
2 actionable tasks: 2 executed

タスクはプラグインで足す。buildでさえ例外ではない

例えば、プラグインなしのgradle.buildがルート直下にある場合のgradle tasks は以下のようになっています。
そもそも Build tasks がありません。

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

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

一方、Kotlinプラグイン & Applicationプラグインがある場合は以下の通り。
※他にもプラグイン入ってます。

------------------------------------------------------------
Tasks runnable from root project
------------------------------------------------------------

Application tasks
-----------------
run - Runs this project as a JVM application
runShadow - Runs this project as a JVM application using the shadow jar
startShadowScripts - Creates OS specific scripts to run the project as a JVM application using the shadow jar

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.
...

その他

herokuはpush時デフォルトで ./gradlew build を実行する

ちなみに、stage というタスクがある場合はそっちが優先みたいです。
Deploying Gradle Apps on Heroku

remote: -----> Gradle app detected
remote: -----> Installing JDK 1.8... done
remote: -----> Building Gradle app...
remote: -----> executing ./gradlew build
remote:        > Task :discoverMainScriptsExtensions
...

herokuはpush時にbuildするのであって、Procfile内でbuildを指示するとメモリ不足でタスクが失敗する。

# Procfile
# 間違い
web: ./gradlew build && java -server -jar ./build/libs/app-0.0.1-all.jar
# これが正しい
web: java -server -jar ./build/libs/app-0.0.1-all.jar

まとめ

Gradle、全体的にとても親切ですね。勉強することも多いですけど、慣れると手放せなくなりそうな気がします。

2
1
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
2
1