Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kotlin support and libs.versions #244

Merged
merged 5 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
[![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 projects using Gradle
template repo for Java/Kotlin projects using Gradle

## Features

- JDK 21 ([Amazon Corretto](https://aws.amazon.com/corretto/))
- Gradle 8
- [Gradle 8](https://github.com/gradle/gradle) with Kotlin DSL
- [GitHub Actions](https://github.com/features/actions) CI/CD
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless)
- 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))
- 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
71 changes: 47 additions & 24 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,57 @@ import com.github.spotbugs.snom.SpotBugsExtension
import com.github.spotbugs.snom.SpotBugsTask
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
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 {
id("java-library")
id("com.diffplug.gradle.spotless") version "6.25.0" apply (false)
id("com.github.spotbugs") version "6.0.15" apply (false)
id("com.asarkar.gradle.build-time-tracker") version "4.3.0"
kotlin("jvm") version libs.versions.kotlin
alias(libs.plugins.spotless)
alias(libs.plugins.spotbugs)
alias(libs.plugins.buildtimetracker)
}

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

subprojects {
apply(plugin = "java")
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
}

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

apply(plugin = "com.diffplug.spotless")
configure<SpotlessExtension> {
java {
removeUnusedImports()
googleJavaFormat()
trimTrailingWhitespace()
endWithNewline()
}
kotlin {
ktlint()
trimTrailingWhitespace()
endWithNewline()
}
kotlinGradle {
ktlint().editorConfigOverride(mapOf("ktlint_standard_no-empty-file" to "disabled"))
trimTrailingWhitespace()
endWithNewline()
}
}

// TODO this doesn't work on Kotlin, look into Detekt?
apply(plugin = "checkstyle")
configure<CheckstyleExtension> {
toolVersion = "10.12.0"
Expand Down Expand Up @@ -68,17 +86,19 @@ subprojects {
showExceptions = true
showCauses = true
showStackTraces = true
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) {
println(
"Results: ${result.resultType} " +
afterSuite(
KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) {
println(
"Results: ${result.resultType} " +
"(${result.testCount} test${if (result.testCount > 1) "s" else ""}, " +
"${result.successfulTestCount} passed, " +
"${result.failedTestCount} failed, " +
"${result.skippedTestCount} skipped)"
)
}
}))
"${result.skippedTestCount} skipped)",
)
}
}),
)
}
finalizedBy(tasks.withType<JacocoReport>())
}
Expand All @@ -102,20 +122,23 @@ subprojects {
}

dependencies {
implementation("org.apache.logging.log4j:log4j-core:2.23.1")
implementation("org.apache.logging.log4j:log4j-api:2.23.1")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:2.23.1")
implementation("com.github.spotbugs:spotbugs-annotations:4.8.5")
implementation("com.google.guava:guava:33.2.0-jre")
implementation(rootProject.libs.log4j.core)
implementation(rootProject.libs.log4j.api)
implementation(rootProject.libs.log4j.slf4j2)
implementation(rootProject.libs.spotbugs.annotations)
implementation(rootProject.libs.guava)

testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testImplementation("com.google.truth:truth:1.4.2")
testImplementation("org.mockito:mockito-core:5.12.0")
testImplementation("org.mockito:mockito-junit-jupiter:5.12.0")
testImplementation(rootProject.libs.junit)
testImplementation(rootProject.libs.truth)
testImplementation(rootProject.libs.mockito.core)
testImplementation(rootProject.libs.mockito.junit)

configurations.all {
exclude("org.assertj")
exclude("junit")
exclude(group = "org.assertj")
exclude(group = "junit")
resolutionStrategy {
force("com.google.guava:guava:${rootProject.libs.versions.guava.get()}") // exclude android version
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id("org.unbroken-dome.test-sets") version "4.1.0"
alias(libs.plugins.testsets)
}

testSets {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class HelloWorldIntegrationTest {

@Test
void test() {
void test_hello() {
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
* @author <a href=https://willmolloy.com>Will Molloy</a>
*/
class HelloWorld {

private final Logger log = LogManager.getLogger();
private static final Logger log = LogManager.getLogger();

String hello(String text) {
log.debug("Hello {}!", text);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@

<Logger name="com.willmolloy" level="debug"/>
</Loggers>
</Configuration>
</Configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class HelloWorldTest {

@Test
void test() {
void test_hello() {
assertThat(new HelloWorld().hello("world")).isEqualTo("Hello world!");
}
}
1 change: 1 addition & 0 deletions example-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

12 changes: 12 additions & 0 deletions example-kotlin/src/main/kotlin/com/willmolloy/HelloWorld.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.willmolloy

/**
* Example main src.
*
* @author <a href=https://willmolloy.com>Will Molloy</a>
*/
class HelloWorld {
fun hello(text: String): String {
return "Hello $text!"
}
}
14 changes: 14 additions & 0 deletions example-kotlin/src/test/kotlin/com/willmolloy/HelloWorldTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.willmolloy

import com.google.common.truth.Truth.assertThat
import org.junit.jupiter.api.Test

/**
* Unit tests for [HelloWorld].
*/
class HelloWorldTest {
@Test
fun `test hello`() {
assertThat(HelloWorld().hello("world")).isEqualTo("Hello world!")
}
}
27 changes: 27 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[plugins]
spotless = { id = "com.diffplug.spotless", version = "6.25.0" }
spotbugs = { id = "com.github.spotbugs", version = "6.0.27" }
buildtimetracker = { id = "com.asarkar.gradle.build-time-tracker", version = "4.3.0" }
testsets = { id = "org.unbroken-dome.test-sets", version = "4.1.0" }

[versions]
kotlin = "2.1.0"
log4j = "2.24.3"
spotbugs = "4.8.6"
guava = "33.4.0-jre"
# test libs
junit = "5.11.4"
truth = "1.4.4"
mockito = "5.14.2"

[libraries]
spotbugs-annotations = { module = "com.github.spotbugs:spotbugs-annotations", version.ref = "spotbugs" }
log4j-api = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j" }
log4j-core = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j" }
log4j-slf4j2 = { module = "org.apache.logging.log4j:log4j-slf4j2-impl", version.ref = "log4j" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
# test libs
junit = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit" }
truth = { module = "com.google.truth:truth", version.ref = "truth" }
mockito-core = { module = "org.mockito:mockito-core", version.ref = "mockito" }
mockito-junit = { module = "org.mockito:mockito-junit-jupiter", version.ref = "mockito" }
3 changes: 2 additions & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rootProject.name = "java-template"
include("hello-world")
include("example-java")
include("example-kotlin")
Loading