我正在努力改进我的团队的代码风格,而 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-reporter或 one 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/