【问题标题】:Ktlint Custom ReporterKtlint 自定义记者
【发布时间】:2018-12-24 09:34:14
【问题描述】:

我正在努力改进我团队的代码风格,ktlint 似乎是我们正在引入的 Kotlin 的完美解决方案。

我的问题是找到一个完整的示例来创建客户报告器,以便在运行 ktlint gradle 任务时允许自定义输出。 Ktlint 的文档说:

简而言之,您需要做的就是实现一个 Reporter 并通过使用 META-INF/services/com.github.shyiko.ktlint.core.ReporterProvider 注册一个自定义 ReporterProvider 来使其可用。将所有这些打包到一个 JAR 中,就完成了。

但遵循一个简单的示例here,但我不知道在哪里放置这些文件或在哪里放置 ktlint 推荐的这个“jar”,或者它说我的自定义报告器尚未找到。

有没有人举例说明这是如何工作的?谢谢。

【问题讨论】:

    标签: android gradle kotlin checkstyle


    【解决方案1】:

    看看mcassiano/ktlint-html-reporterone of the ktlint's built-in reporters

    简而言之,每个报告器都包含一个 Reporter、ReporterProvider 和一个服务定义(其中包含 ReporterProvider 实现类名):

    $ cat src/main/kotlin/your/pkg/CustomReporter.kt
    package your.pkg
    import com.github.shyiko.ktlint.core.Reporter
    class CustomReporter : Reporter {
    ...    
    
    $ cat src/main/kotlin/your/pkg/CustomReporterProvider.kt
    package your.pkg
    import com.github.shyiko.ktlint.core.ReporterProvider
    class CustomReporterProvider : CustomReporter {
    ...
    
    $ cat src/main/resources/META-INF/services/com.github.shyiko.ktlint.core.ReporterProvider
    your.pkg.CustomReporterProvider
    

    您需要将其打包到 JAR 中。
    有了 JAR 后,ktlint 可以通过以下方式之一加载它:

    • ktlint --reporter=custom,artifact=your.pkg:custom-reporter:0.1.0,output=target/output.html(假设 your.pkg:custom-reporter:0.1.0 在 Maven Central / JCenter / JitPack 中可用)
    • ktlint --reporter=custom,artifact=~/path/to/custom-reporter.jar(来自 fs)
    • 来自类路径(如果您计划通过 Gradle/Maven/etc 使用 ktlint),例如

      dependencies {
          ktlint "com.github.shyiko:ktlint:$ktlintVersion"
          ktlint "your.pkg:custom-reporter:0.1.0"
      }
      
      task ktlint(type: JavaExec, group: "verification") {
          classpath = configurations.ktlint
          main = "com.github.shyiko.ktlint.Main"
          args "--reporter=custom", "src/**/*.kt"
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-13
      • 2017-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-30
      • 2019-05-11
      • 1970-01-01
      相关资源
      最近更新 更多