android - Ktlint 自定义记者

标签 android gradle kotlin checkstyle

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

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

In short, all you need to do is to implement a Reporter and make it available by registering a custom ReporterProvider using META-INF/services/com.github.shyiko.ktlint.core.ReporterProvider. Pack all of that into a JAR and you're done.

但下面是一个简单的例子 here但我不知道将这些文件放在哪里,也不知道将 ktlint 推荐的这个“ jar ”放在哪里,或者它说我的自定义记者还没有找到。

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

最佳答案

看看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"
    }
    

关于android - Ktlint 自定义记者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51368558/

相关文章:

android - 工具栏的 Logo 图标是否可点击?

gradle - 在 gradle 中添加外部项目依赖

java - 如何修复此错误 : java. lang.NoSuchMethodError: 'java.lang.AutoCloseable org.mockito.MockitoAnnotations.openMocks(java.lang.Object)'

java - 在 gradle 的 eclipse 构建中拆分 main 和 test

kotlin - 使用挂起函数混淆内部类后的 ClassCastException

android - 将 ColorFilter 应用于 LayerDrawable 中的 Drawable

android - Proguard删除方法参数

android - 如何使用选择器为 ImageView 着色?

performance - Kotlin:避免创建FqNameUnsafe和FqName

Kotlin 高阶函数参数 : Passing subtypes