0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

KotlinプロジェクトにSpotlessリンターを導入

Last updated at Posted at 2025-02-23

リンターが入っているとチームの中でスタンダートな書きっぷりができて 統一感が出て、レビューの中で表面的なことを見なくて 時間短縮になる。

本章では SpotlessとKtlinを導入する. 前回のQuarkusプロジェクト用に考えているが、多分どのKotlinプロジェクトでもできるでしょう。

1. Spotlessを導入

gradleファイルに spotlessプラグインを導入. 基本的に公式ガイドの通り

  plugins {
      kotlin("jvm") version "2.0.21"
      kotlin("plugin.allopen") version "2.0.21"
      id("io.quarkus")
+     id("com.diffplug.spotless") version "7.0.2"
  }

2. Spotless用の設定を追加

gradleファイルの最後ら辺に Spotlessの設定を追加。そして、buildタスクと同時に実行されるように設定を追加。 詳細は公式ガイドを参照

build.gradle.kts
  kotlin {
      compilerOptions {
          jvmTarget = org.jetbrains.kotlin.gradle.dsl.JvmTarget.JVM_17
          javaParameters = true
      }
  }
  
+ spotless {
+     kotlin {
+         target("src/**/*.kt")
+         ktlint("1.5.0")
+         toggleOffOn() // spotless:off / spotless:onで一時的にオン・オフ可能とする
+     }
+ }
+ // Spotless: buildタスクと同時に spotlessもapplyする
+ tasks.named("build") {
+     dependsOn("spotlessApply")
+ }

3. rulesを導入

gradleファイルにルールを書くこともできるが、できればlinter関連のことを最低限にして、アプリのビルド関連のことにしたい。spotless/ktlintもeditorconfigファイルに対応しているため 以下にファイルで別管理が可能。

プロジェクトのルートに導入。

.editorconfig
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.kt]
max_line_length = 160

また、必要に応じて、プロジェクトの別のフォルダーに.editorconfigを追加することもできる。root=falseにすれば良い

src/native-test/kotlin/com/ibm/jp/.editorconfig
root = false

# このフォルダー配下のルールを記入
rule = value

以上!

See you later!🐊

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?