Skip to content

Commit 95e5c37

Browse files
authored
Kotlin support and libs.versions (#244)
1 parent 458d57b commit 95e5c37

File tree

12 files changed

+111
-34
lines changed

12 files changed

+111
-34
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
[![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)
44
[![codecov](https://codecov.io/gh/will-molloy/java-template/branch/main/graph/badge.svg)](https://codecov.io/gh/will-molloy/java-template)
55

6-
template repo for Java projects using Gradle
6+
template repo for Java/Kotlin projects using Gradle
77

88
## Features
99

1010
- JDK 21 ([Amazon Corretto](https://aws.amazon.com/corretto/))
11-
- Gradle 8
11+
- [Gradle 8](https://github.com/gradle/gradle) with Kotlin DSL
1212
- [GitHub Actions](https://github.com/features/actions) CI/CD
13-
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless)
13+
- 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))
1414
- Code style analysis via [Checkstyle](https://github.com/checkstyle/checkstyle)
1515
- Static analysis via [SpotBugs](https://spotbugs.github.io/)
1616
- Unit and integration test support via [JUnit 5](https://junit.org/junit5/) and [TestSets plugin](https://github.com/unbroken-dome/gradle-testsets-plugin)

build.gradle.kts

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,57 @@ import com.github.spotbugs.snom.SpotBugsExtension
55
import com.github.spotbugs.snom.SpotBugsTask
66
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
77
import org.gradle.api.tasks.testing.logging.TestLogEvent
8+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
89

910
logger.quiet("Java version: ${JavaVersion.current()}")
1011
logger.quiet("Gradle version: ${gradle.gradleVersion}")
1112

1213
plugins {
1314
id("java-library")
14-
id("com.diffplug.gradle.spotless") version "6.25.0" apply (false)
15-
id("com.github.spotbugs") version "6.0.15" apply (false)
16-
id("com.asarkar.gradle.build-time-tracker") version "4.3.0"
15+
kotlin("jvm") version libs.versions.kotlin
16+
alias(libs.plugins.spotless)
17+
alias(libs.plugins.spotbugs)
18+
alias(libs.plugins.buildtimetracker)
1719
}
1820

1921
allprojects {
2022
group = "com.willmolloy"
2123
repositories {
2224
mavenCentral()
2325
}
24-
}
2526

26-
subprojects {
2727
apply(plugin = "java")
2828
configure<JavaPluginExtension> {
2929
sourceCompatibility = JavaVersion.VERSION_21
3030
targetCompatibility = JavaVersion.VERSION_21
3131
}
3232

33+
apply(plugin = "kotlin")
34+
configure<KotlinJvmProjectExtension> {
35+
jvmToolchain(21)
36+
}
37+
3338
apply(plugin = "com.diffplug.spotless")
3439
configure<SpotlessExtension> {
3540
java {
3641
removeUnusedImports()
3742
googleJavaFormat()
43+
trimTrailingWhitespace()
44+
endWithNewline()
45+
}
46+
kotlin {
47+
ktlint()
48+
trimTrailingWhitespace()
49+
endWithNewline()
50+
}
51+
kotlinGradle {
52+
ktlint().editorConfigOverride(mapOf("ktlint_standard_no-empty-file" to "disabled"))
53+
trimTrailingWhitespace()
54+
endWithNewline()
3855
}
3956
}
4057

58+
// TODO this doesn't work on Kotlin, look into Detekt?
4159
apply(plugin = "checkstyle")
4260
configure<CheckstyleExtension> {
4361
toolVersion = "10.12.0"
@@ -68,17 +86,19 @@ subprojects {
6886
showExceptions = true
6987
showCauses = true
7088
showStackTraces = true
71-
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
72-
if (desc.parent == null) {
73-
println(
74-
"Results: ${result.resultType} " +
89+
afterSuite(
90+
KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
91+
if (desc.parent == null) {
92+
println(
93+
"Results: ${result.resultType} " +
7594
"(${result.testCount} test${if (result.testCount > 1) "s" else ""}, " +
7695
"${result.successfulTestCount} passed, " +
7796
"${result.failedTestCount} failed, " +
78-
"${result.skippedTestCount} skipped)"
79-
)
80-
}
81-
}))
97+
"${result.skippedTestCount} skipped)",
98+
)
99+
}
100+
}),
101+
)
82102
}
83103
finalizedBy(tasks.withType<JacocoReport>())
84104
}
@@ -102,20 +122,23 @@ subprojects {
102122
}
103123

104124
dependencies {
105-
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
106-
implementation("org.apache.logging.log4j:log4j-api:2.23.1")
107-
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.23.1")
108-
implementation("com.github.spotbugs:spotbugs-annotations:4.8.5")
109-
implementation("com.google.guava:guava:33.2.0-jre")
125+
implementation(rootProject.libs.log4j.core)
126+
implementation(rootProject.libs.log4j.api)
127+
implementation(rootProject.libs.log4j.slf4j2)
128+
implementation(rootProject.libs.spotbugs.annotations)
129+
implementation(rootProject.libs.guava)
110130

111-
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
112-
testImplementation("com.google.truth:truth:1.4.2")
113-
testImplementation("org.mockito:mockito-core:5.12.0")
114-
testImplementation("org.mockito:mockito-junit-jupiter:5.12.0")
131+
testImplementation(rootProject.libs.junit)
132+
testImplementation(rootProject.libs.truth)
133+
testImplementation(rootProject.libs.mockito.core)
134+
testImplementation(rootProject.libs.mockito.junit)
115135

116136
configurations.all {
117-
exclude("org.assertj")
118-
exclude("junit")
137+
exclude(group = "org.assertj")
138+
exclude(group = "junit")
139+
resolutionStrategy {
140+
force("com.google.guava:guava:${rootProject.libs.versions.guava.get()}") // exclude android version
141+
}
119142
}
120143
}
121144
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
2-
id("org.unbroken-dome.test-sets") version "4.1.0"
2+
alias(libs.plugins.testsets)
33
}
44

55
testSets {

hello-world/src/integrationTest/java/com/willmolloy/HelloWorldIntegrationTest.java renamed to example-java/src/integrationTest/java/com/willmolloy/HelloWorldIntegrationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class HelloWorldIntegrationTest {
1313

1414
@Test
15-
void test() {
15+
void test_hello() {
1616
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
1717
}
1818
}

hello-world/src/main/java/com/willmolloy/HelloWorld.java renamed to example-java/src/main/java/com/willmolloy/HelloWorld.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
* @author <a href=https://willmolloy.com>Will Molloy</a>
1010
*/
1111
class HelloWorld {
12-
13-
private final Logger log = LogManager.getLogger();
12+
private static final Logger log = LogManager.getLogger();
1413

1514
String hello(String text) {
1615
log.debug("Hello {}!", text);

hello-world/src/main/resources/log4j2.xml renamed to example-java/src/main/resources/log4j2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313

1414
<Logger name="com.willmolloy" level="debug"/>
1515
</Loggers>
16-
</Configuration>
16+
</Configuration>

hello-world/src/test/java/com/willmolloy/HelloWorldTest.java renamed to example-java/src/test/java/com/willmolloy/HelloWorldTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
class HelloWorldTest {
1313

1414
@Test
15-
void test() {
15+
void test_hello() {
1616
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
1717
}
1818
}

example-kotlin/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.willmolloy
2+
3+
/**
4+
* Example main src.
5+
*
6+
* @author <a href=https://willmolloy.com>Will Molloy</a>
7+
*/
8+
class HelloWorld {
9+
fun hello(text: String): String {
10+
return "Hello $text!"
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.willmolloy
2+
3+
import com.google.common.truth.Truth.assertThat
4+
import org.junit.jupiter.api.Test
5+
6+
/**
7+
* Unit tests for [HelloWorld].
8+
*/
9+
class HelloWorldTest {
10+
@Test
11+
fun `test hello`() {
12+
assertThat(HelloWorld().hello("world")).isEqualTo("Hello world!")
13+
}
14+
}

0 commit comments

Comments
 (0)