Skip to content

Instantly share code, notes, and snippets.

@ditn
Created December 7, 2018 12:53
Show Gist options
  • Save ditn/a5152308e113c7cdbe6221fd030c5060 to your computer and use it in GitHub Desktop.
Save ditn/a5152308e113c7cdbe6221fd030c5060 to your computer and use it in GitHub Desktop.
ktlint with Gradle Kotlin DSL
val ktlint by configurations.creating
dependencies {
ktlint(Libraries.ktlint)
}
tasks.register<JavaExec>("ktlint") {
group = "verification"
description = "Check Kotlin code style."
classpath = ktlint
main = "com.github.shyiko.ktlint.Main"
args("--android", "src/**/*.kt")
}
tasks.named("check") {
dependsOn(ktlint)
}
tasks.register<JavaExec>("ktlintFormat") {
group = "formatting"
description = "Fix Kotlin code style deviations."
classpath = ktlint
main = "com.github.shyiko.ktlint.Main"
args("--android", "-F", "src/**/*.kt")
}
@kikin81
Copy link

kikin81 commented Jul 14, 2019

I cannot import Libraries.

@ditn
Copy link
Author

ditn commented Jul 14, 2019

Either declare using buildSrc, or just replace Libraries.ktlint with "com.github.shyiko:ktlint:0.29.0".

object Versions {
    const val ktlint = "0.29.0"
}

object Libraries {
    const val ktlint = "com.github.shyiko:ktlint:${Versions.ktlint}"
}

@KrisOch
Copy link

KrisOch commented Oct 22, 2020

If the dependence of tasks do not work, try this:

tasks.named("check") {
  dependsOn(tasks.named("ktlint"))
}

@tushar-majestyk
Copy link

awesome, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment