Skip to content

Commit

Permalink
Add ktfmt
Browse files Browse the repository at this point in the history
Have been finding inconsistent results with `ktlint`. I.e. it preserves
custom formatting, which you do not want. It should have deterministic
output.

`ktfmt` was made to solve that: https://github.com/facebook/ktfmt?tab=readme-ov-file#faq

I think they should work together? Need to use for a bit to make sure.
Still want `ktlint` as it handles some bits better like space between
methods.
  • Loading branch information
will-molloy committed Jan 4, 2025
1 parent a37cd10 commit 657decc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 37 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
[![build](https://github.com/will-molloy/java-template/actions/workflows/build.yml/badge.svg?branch=main&event=push)](https://github.com/will-molloy/java-template/actions/workflows/build.yml)
[![codecov](https://codecov.io/gh/will-molloy/java-template/branch/main/graph/badge.svg)](https://codecov.io/gh/will-molloy/java-template)

template repo for Java/Kotlin projects using Gradle
template repo for Java/Kotlin Gradle projects

## Features

- JDK 21 ([Amazon Corretto](https://aws.amazon.com/corretto/))
- [Gradle 8](https://github.com/gradle/gradle) with Kotlin DSL
- [Gradle 8](https://github.com/gradle/gradle) (Kotlin DSL)
- [GitHub Actions](https://github.com/features/actions) CI/CD
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless) ([`google-java-format`](https://github.com/google/google-java-format) and [`ktlint`](https://github.com/pinterest/ktlint))
- 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)
- 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
40 changes: 15 additions & 25 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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 @@ -20,9 +21,7 @@ plugins {

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

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

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

apply(plugin = "com.diffplug.spotless")
configure<SpotlessExtension> {
// https://github.com/diffplug/spotless/tree/main/plugin-gradle#java
java {
removeUnusedImports()
googleJavaFormat()
trimTrailingWhitespace()
endWithNewline()
}
// https://github.com/diffplug/spotless/tree/main/plugin-gradle#kotlin
kotlin {
ktlint()
ktfmt().googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
kotlinGradle {
ktlint().editorConfigOverride(mapOf("ktlint_standard_no-empty-file" to "disabled"))
ktfmt().googleStyle()
trimTrailingWhitespace()
endWithNewline()
}
}

// TODO this doesn't work on Kotlin, look into Detekt?
// TODO Kotlin alternative?
apply(plugin = "checkstyle")
configure<CheckstyleExtension> {
toolVersion = "10.12.0"
Expand All @@ -73,9 +74,7 @@ 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 @@ -104,22 +103,12 @@ 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 All @@ -137,7 +126,8 @@ allprojects {
exclude(group = "org.assertj")
exclude(group = "junit")
resolutionStrategy {
force("com.google.guava:guava:${rootProject.libs.versions.guava.get()}") // exclude android version
// exclude android version
force("com.google.guava:guava:${rootProject.libs.versions.guava.get()}")
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions example-java/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
plugins {
alias(libs.plugins.testsets)
}
plugins { alias(libs.plugins.testsets) }

testSets {
create("integrationTest")
}
testSets { create("integrationTest") }
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package com.willmolloy
import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

/**
* Unit tests for [HelloWorld].
*/
/** Unit tests for [HelloWorld]. */
class HelloWorldTest {
@Test
fun `test hello`() {
Expand Down
2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
rootProject.name = "java-template"

include("example-java")

include("example-kotlin")

0 comments on commit 657decc

Please sign in to comment.