Skip to content

Commit

Permalink
Swap to ktfmt for kotlin code, but keep ktlint for gradle scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
will-molloy committed Jan 6, 2025
1 parent 657decc commit 078f74c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ template repo for Java/Kotlin Gradle projects
- [GitHub Actions](https://github.com/features/actions) CI/CD
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless)
- Java: [`google-java-format`](https://github.com/google/google-java-format)
- Kotlin: [`ktlint`](https://github.com/pinterest/ktlint) and [`ktfmt`](https://github.com/facebook/ktfmt)
- Kotlin: [`ktfmt`](https://github.com/facebook/ktfmt)
- Kotlin Gradle: [`ktlint`](https://github.com/pinterest/ktlint)
- Code style analysis via [Checkstyle](https://github.com/checkstyle/checkstyle)
- Static analysis via [SpotBugs](https://spotbugs.github.io/)
- Unit and integration test support via [JUnit 5](https://junit.org/junit5/) and [TestSets plugin](https://github.com/unbroken-dome/gradle-testsets-plugin)
Expand Down
36 changes: 26 additions & 10 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension

logger.quiet("Java version: ${JavaVersion.current()}")

logger.quiet("Gradle version: ${gradle.gradleVersion}")

plugins {
Expand All @@ -21,7 +20,9 @@ plugins {

allprojects {
group = "com.willmolloy"
repositories { mavenCentral() }
repositories {
mavenCentral()
}

apply(plugin = "java")
configure<JavaPluginExtension> {
Expand All @@ -30,7 +31,9 @@ allprojects {
}

apply(plugin = "kotlin")
configure<KotlinJvmProjectExtension> { jvmToolchain(21) }
configure<KotlinJvmProjectExtension> {
jvmToolchain(21)
}

apply(plugin = "com.diffplug.spotless")
configure<SpotlessExtension> {
Expand All @@ -42,15 +45,16 @@ allprojects {
endWithNewline()
}
// https://github.com/diffplug/spotless/tree/main/plugin-gradle#kotlin
// ktfmt seems better than ktlint (more deterministic/consistent output).
// Furthermore, using spotless more for formatting than linting.
// However, ktfmt has some weird output with Gradle scripts, so using ktlint for that.
kotlin {
ktlint()
ktfmt().googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
kotlinGradle {
ktlint().editorConfigOverride(mapOf("ktlint_standard_no-empty-file" to "disabled"))
ktfmt().googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
Expand All @@ -74,7 +78,9 @@ allprojects {
ignoreFailures.set(false)
excludeFilter.set(rootProject.file("./spotbugs-exclude.xml"))
}
tasks.withType<SpotBugsTask> { reports.create("html").required.set(true) }
tasks.withType<SpotBugsTask> {
reports.create("html").required.set(true)
}

tasks.withType<Test> {
maxParallelForks = Runtime.getRuntime().availableProcessors()
Expand Down Expand Up @@ -103,12 +109,22 @@ allprojects {
}

apply(plugin = "jacoco")
tasks.withType<JacocoReport> { reports { xml.required.set(true) } }
tasks.withType<JacocoReport> {
reports {
xml.required.set(true)
}
}

val previewFeatures = emptyList<String>()
tasks.withType<JavaCompile> { options.compilerArgs = previewFeatures }
tasks.withType<Test> { jvmArgs = previewFeatures }
tasks.withType<JavaExec> { jvmArgs = previewFeatures }
tasks.withType<JavaCompile> {
options.compilerArgs = previewFeatures
}
tasks.withType<Test> {
jvmArgs = previewFeatures
}
tasks.withType<JavaExec> {
jvmArgs = previewFeatures
}

dependencies {
implementation(rootProject.libs.log4j.core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package com.willmolloy
* @author <a href=https://willmolloy.com>Will Molloy</a>
*/
class HelloWorld {

fun hello(text: String): String {
return "Hello $text!"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test

/** Unit tests for [HelloWorld]. */
class HelloWorldTest {

@Test
fun `test hello`() {
assertThat(HelloWorld().hello("world")).isEqualTo("Hello world!")
Expand Down
2 changes: 0 additions & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
rootProject.name = "java-template"

include("example-java")

include("example-kotlin")

0 comments on commit 078f74c

Please sign in to comment.