Skip to content

Commit

Permalink
Migrate to gradle.kts (Gradle Kotlin DSL) (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
will-molloy committed Jul 2, 2023
1 parent 3d6fb33 commit 84d30ec
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 154 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ template repository for Java projects using Gradle
- Automatic code formatting via [Spotless](https://github.com/diffplug/spotless)
- 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 and [TestSets plugin](https://github.com/unbroken-dome/gradle-testsets-plugin)
- Unit and integration test support via [JUnit 5](https://junit.org/junit5/) and [TestSets plugin](https://github.com/unbroken-dome/gradle-testsets-plugin)
- Code coverage reporting via [Codecov](https://codecov.io/)
- Dependency upgrades via [Renovate bot](https://renovatebot.com)

Expand Down
142 changes: 0 additions & 142 deletions build.gradle

This file was deleted.

127 changes: 127 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import com.diffplug.gradle.spotless.SpotlessExtension
import com.github.spotbugs.snom.Confidence
import com.github.spotbugs.snom.Effort
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

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

plugins {
id("java-library")
id("com.diffplug.gradle.spotless") version "6.19.0" apply (false)
id("com.github.spotbugs") version "5.0.14" apply (false)
id("com.asarkar.gradle.build-time-tracker") version "4.3.0"
}

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

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

apply(plugin = "com.diffplug.spotless")
configure<SpotlessExtension> {
java {
removeUnusedImports()
googleJavaFormat()
}
}

apply(plugin = "checkstyle")
configure<CheckstyleExtension> {
toolVersion = "10.12.0"
configFile = rootProject.file("./checkstyle.xml")
maxErrors = 0
maxWarnings = 0
isIgnoreFailures = false
}

apply(plugin = "com.github.spotbugs")
configure<SpotBugsExtension> {
effort.set(Effort.MAX)
reportLevel.set(Confidence.LOW)
ignoreFailures.set(false)
excludeFilter.set(rootProject.file("./spotbugs-exclude.xml"))
}
tasks.withType<SpotBugsTask> {
reports.create("html").required.set(true)
}

tasks.withType<Test> {
maxParallelForks = Runtime.getRuntime().availableProcessors()
useJUnitPlatform()
testLogging {
events = setOf(TestLogEvent.FAILED, TestLogEvent.SKIPPED)
exceptionFormat = TestExceptionFormat.FULL
showExceptions = true
showCauses = true
showStackTraces = true
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)"
)
}
}))
}
finalizedBy(tasks.withType<JacocoReport>())
}

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

val previewFeatures = listOf("--enable-preview")
tasks.withType<JavaCompile> {
options.compilerArgs = previewFeatures
}
tasks.withType<Test> {
jvmArgs = previewFeatures
}
tasks.withType<JavaExec> {
jvmArgs = previewFeatures
}

dependencies {
val log4jVersion = "2.20.0"
val guavaVersion = "32.0.1-jre"
implementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
implementation("com.github.spotbugs:spotbugs-annotations:4.7.3")
implementation("com.google.guava:guava:$guavaVersion")

val junitVersion = "5.9.3"
val truthVersion = "1.1.4"
val mockitoVersion = "5.3.1"
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
testImplementation("com.google.truth:truth:$truthVersion")
testImplementation("com.google.truth.extensions:truth-java8-extension:$truthVersion")
testImplementation("org.mockito:mockito-core:$mockitoVersion")
testImplementation("org.mockito:mockito-junit-jupiter:$mockitoVersion")

configurations.all {
exclude("org.assertj")
exclude("junit")
resolutionStrategy {
force("com.google.guava:guava:$guavaVersion") // exclude android version
}
}
}
}
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# https://github.com/google/google-java-format/releases/tag/v1.10.0
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
org.gradle.jvmargs=--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Empty file removed hello-world/build.gradle
Empty file.
7 changes: 7 additions & 0 deletions hello-world/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
plugins {
id("org.unbroken-dome.test-sets") version "4.0.0"
}

testSets {
create("integrationTest")
}
4 changes: 2 additions & 2 deletions hello-world/src/main/java/com/willmolloy/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
*
* @author <a href=https://willmolloy.com>Will Molloy</a>
*/
public class HelloWorld {
class HelloWorld {

private final Logger log = LogManager.getLogger();

public String hello(String text) {
String hello(String text) {
log.debug("Hello {}!", text);
return "Hello %s!".formatted(text);
}
Expand Down
2 changes: 0 additions & 2 deletions settings.gradle

This file was deleted.

2 changes: 2 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
rootProject.name = "java-gradle-template"
include("hello-world")
5 changes: 0 additions & 5 deletions spotbugs-exclude.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>

<!-- False positive with records -->
<Match>
<Bug pattern="EQ_UNUSUAL" />
</Match>

</FindBugsFilter>

0 comments on commit 84d30ec

Please sign in to comment.