kdnakt blog

hello there.

Gradleはタスク名を省略してコマンドライン実行できる

先日いただいたTODA先生のありがたいお言葉に従ってgradle build --scanを試していたら発見したのでメモしておく。

 

 

[先頭の数文字を指定する]

Gradle CLIのドキュメントに、Name abbreviationという項がある。

docs.gradle.org

 

上のページの例では、gradle checkというコマンドのかわりにgradle cheと入力する例が紹介されている。

 

実際に自分のプロジェクトで試してみる。

github.com

 

サブプロジェクトを一覧表示してくれるprojectsというタスクがあるので、これを./gradlew projで実行してみる。

$ ./gradlew proj

> Configure project :
Kotlin Multiplatform Projects are an Alpha feature. See: https://kotlinlang.org/docs/reference/evolution/components-stability.html. To hide this message, add 'kotlin.mpp.stability.nowarn=true' to the Gradle properties.


> Task :projects

------------------------------------------------------------
Root project
------------------------------------------------------------

Root project 'kmonkey'
No sub-projects

To see a list of the tasks of a project, run gradlew <project-path>:tasks
For example, try running gradlew :tasks

BUILD SUCCESSFUL in 1s
1 actionable task: 1 executed

 

問題なくタスクを実行できた。

1文字でも大丈夫だろうかと思い./gradlew buildのかわりに./gradlew bを試してみたが問題なさそう(わざとコンパイルエラーにしてみたときのビルドログ)。

$ ./gradlew b

> Configure project :
Kotlin Multiplatform Projects are an Alpha feature. See: https://kotlinlang.org/docs/reference/evolution/components-stability.html. To hide this message, add 'kotlin.mpp.stability.nowarn=true' to the Gradle properties.


> Task :compileKotlinNative FAILED
e: /Users/akito/Develop/kdnakt/kmonkey/src/nativeMain/kotlin/repl/repl.kt: (27, 33): Expecting ')'

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileKotlinNative'.
> Compilation finished with errors

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 5s
3 actionable tasks: 1 executed, 2 up-to-date

 

[キャメルケースの先頭1文字を利用する]

Gradleにはビルド時に何がなぜ起きたのかを確認できる機能(ビルドスキャン)がある。

scans.gradle.com

 

ビルドスキャンはGradle Enterpriseプラグインで提供されている。

docs.gradle.com

 

./gradlew build --scanで実行されたビルドスキャンの結果は、https://scans.gradle.com/上で一意のURLを振られて公開される。

--scanオプションを利用せずにビルドした場合でも、後からスキャン結果を公開することができる。その場合のタスク名はbuildScanPublishPreviousと非常に長い。

こうした複雑なタスク名の場合には、キャメルケース(ローワーキャメルケース)の先頭1文字だけを利用して、タスク名を省略できる。

// 通常
$ ./gradlew buildScanPublishPrevious

// 省略版
$ ./gradlew bSPP

 

Gradleのタスク名はこうした長いものがときおり存在しており、コマンドラインからの実行時に面倒だなと感じていた。しかし、このようにタスク名を省略できるのなら話は別だ。Gradleと仲良くなれそうな気がする。

 

省略前がケバブケースの場合でも、キャメルケースのように扱ってタスク名を省略できる。

// 通常
$ ./gradlew my-awesome-library:compileKotlin

// 省略版
$ ./gradlew mAL:cK

 

[省略タスク名が重複する場合]

先ほどの自分のプロジェクトにはprojectspropertiesとpではじまるタスクが2つ存在している。

./gradlew pを実行したら、どちらのタスクが実行されるのだろう?と思い試してみると、タスク名が特定できずエラーとなってしまった。

$ ./gradlew p

> Configure project :
Kotlin Multiplatform Projects are an Alpha feature. See: https://kotlinlang.org/docs/reference/evolution/components-stability.html. To hide this message, add 'kotlin.mpp.stability.nowarn=true' to the Gradle properties.


FAILURE: Build failed with an exception.

* What went wrong:
Task 'p' is ambiguous in root project 'kmonkey'. Candidates are: 'projects', 'properties'.

* Try:
Run gradlew tasks to get a list of available tasks. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

 

[まとめ]

  • Gradle CLIではタスク名を先頭の数文字に省略できる
  • 複雑なタスク名の場合はキャメルケースの先頭1文字を利用して省略できる
  • タスク名を省略しすぎるとエラーになるので注意