From 6670346890c751429f1f0ea1cbfe2e6239597122 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Wed, 31 May 2023 22:48:49 +0200 Subject: [PATCH] tweaks and fixes - introduce kotlin-jvm convention - tidy up code-quality build config --- .../buildsrc/conventions/base.gradle.kts | 25 ++++++-- ...ube.gradle.kts => code-quality.gradle.kts} | 26 +++++--- .../conventions/lang/kotlin-jvm.gradle.kts | 42 +++++++++++++ .../buildsrc/utils/Rife2TestListener.kt | 63 ++++++++++--------- lib/build.gradle.kts | 52 +++------------ 5 files changed, 119 insertions(+), 89 deletions(-) rename buildSrc/src/main/kotlin/buildsrc/conventions/{sonarqube.gradle.kts => code-quality.gradle.kts} (52%) create mode 100644 buildSrc/src/main/kotlin/buildsrc/conventions/lang/kotlin-jvm.gradle.kts diff --git a/buildSrc/src/main/kotlin/buildsrc/conventions/base.gradle.kts b/buildSrc/src/main/kotlin/buildsrc/conventions/base.gradle.kts index 0ef8540..7404066 100644 --- a/buildSrc/src/main/kotlin/buildsrc/conventions/base.gradle.kts +++ b/buildSrc/src/main/kotlin/buildsrc/conventions/base.gradle.kts @@ -1,18 +1,31 @@ package buildsrc.conventions +import buildsrc.utils.Rife2TestListener +import org.gradle.api.tasks.testing.logging.TestExceptionFormat +import org.gradle.api.tasks.testing.logging.TestLogEvent + /** common config for all subprojects */ plugins { - base + base } if (project != rootProject) { - project.version = rootProject.version - project.group = rootProject.group + project.version = rootProject.version + project.group = rootProject.group } tasks.withType().configureEach { - // https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives - isPreserveFileTimestamps = false - isReproducibleFileOrder = true + // https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives + isPreserveFileTimestamps = false + isReproducibleFileOrder = true +} + +tasks.withType().configureEach { + val testsBadgeApiKey = providers.gradleProperty("testsBadgeApiKey") + addTestListener(Rife2TestListener(testsBadgeApiKey)) + testLogging { + exceptionFormat = TestExceptionFormat.FULL + events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) + } } diff --git a/buildSrc/src/main/kotlin/buildsrc/conventions/sonarqube.gradle.kts b/buildSrc/src/main/kotlin/buildsrc/conventions/code-quality.gradle.kts similarity index 52% rename from buildSrc/src/main/kotlin/buildsrc/conventions/sonarqube.gradle.kts rename to buildSrc/src/main/kotlin/buildsrc/conventions/code-quality.gradle.kts index 80ce521..a81b982 100644 --- a/buildSrc/src/main/kotlin/buildsrc/conventions/sonarqube.gradle.kts +++ b/buildSrc/src/main/kotlin/buildsrc/conventions/code-quality.gradle.kts @@ -17,17 +17,25 @@ package buildsrc.conventions +import org.sonarqube.gradle.SonarTask + plugins { - id("org.sonarqube") + id("org.sonarqube") + id("io.gitlab.arturbosch.detekt") + id("org.jetbrains.kotlinx.kover") } sonarqube { - properties { - property("sonar.projectName", rootProject.name) - property("sonar.projectKey", "ethauvin_${rootProject.name}") - property("sonar.organization", "ethauvin-github") - property("sonar.host.url", "https://sonarcloud.io") - property("sonar.sourceEncoding", "UTF-8") - property("sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/kover/xml/report.xml") - } + properties { + property("sonar.projectName", rootProject.name) + property("sonar.projectKey", "ethauvin_${rootProject.name}") + property("sonar.organization", "ethauvin-github") + property("sonar.host.url", "https://sonarcloud.io") + property("sonar.sourceEncoding", "UTF-8") + property("sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/kover/report.xml") + } +} + +tasks.withType().configureEach { + dependsOn(tasks.matching { it.name == "koverXmlReport" }) } diff --git a/buildSrc/src/main/kotlin/buildsrc/conventions/lang/kotlin-jvm.gradle.kts b/buildSrc/src/main/kotlin/buildsrc/conventions/lang/kotlin-jvm.gradle.kts new file mode 100644 index 0000000..790e694 --- /dev/null +++ b/buildSrc/src/main/kotlin/buildsrc/conventions/lang/kotlin-jvm.gradle.kts @@ -0,0 +1,42 @@ +package buildsrc.conventions.lang + +import buildsrc.utils.Rife2TestListener +import org.gradle.api.JavaVersion +import org.gradle.api.tasks.testing.Test +import org.gradle.api.tasks.testing.logging.TestExceptionFormat +import org.gradle.api.tasks.testing.logging.TestLogEvent +import org.gradle.kotlin.dsl.withType +import org.jetbrains.kotlin.gradle.dsl.JvmTarget +import org.jetbrains.kotlin.gradle.targets.jvm.KotlinJvmTarget +import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import org.sonarqube.gradle.SonarTask + +/** + * Common configuration for Kotlin/JVM projects + * + * (this can be removed after Kotlin Multiplatform migration) + */ + +plugins { + id("buildsrc.conventions.base") + kotlin("jvm") + id("buildsrc.conventions.code-quality") +} + +java { + withSourcesJar() +} + +kotlin { + jvmToolchain(11) +} + +tasks.withType().configureEach { + compilerOptions { + jvmTarget.set(JvmTarget.JVM_11) + } +} + +tasks.withType().configureEach { + useJUnitPlatform() +} diff --git a/buildSrc/src/main/kotlin/buildsrc/utils/Rife2TestListener.kt b/buildSrc/src/main/kotlin/buildsrc/utils/Rife2TestListener.kt index 2fe062f..748b064 100644 --- a/buildSrc/src/main/kotlin/buildsrc/utils/Rife2TestListener.kt +++ b/buildSrc/src/main/kotlin/buildsrc/utils/Rife2TestListener.kt @@ -17,6 +17,7 @@ package buildsrc.utils +import org.gradle.api.provider.Provider import org.gradle.api.tasks.testing.TestDescriptor import org.gradle.api.tasks.testing.TestListener import org.gradle.api.tasks.testing.TestResult @@ -26,37 +27,39 @@ import java.net.http.HttpRequest import java.net.http.HttpResponse class Rife2TestListener( - private val testBadgeApiKey: String? + private val testBadgeApiKey: Provider ) : TestListener { - override fun beforeTest(p0: TestDescriptor?) = Unit - override fun beforeSuite(p0: TestDescriptor?) = Unit - override fun afterTest(desc: TestDescriptor, result: TestResult) = Unit - override fun afterSuite(desc: TestDescriptor, result: TestResult) { - if (desc.parent == null) { - val passed = result.successfulTestCount - val failed = result.failedTestCount - val skipped = result.skippedTestCount + override fun beforeTest(p0: TestDescriptor?) = Unit + override fun beforeSuite(p0: TestDescriptor?) = Unit + override fun afterTest(desc: TestDescriptor, result: TestResult) = Unit + override fun afterSuite(desc: TestDescriptor, result: TestResult) { + if (desc.parent == null) { + val passed = result.successfulTestCount + val failed = result.failedTestCount + val skipped = result.skippedTestCount - if (testBadgeApiKey != null) { - println(testBadgeApiKey) - val response: HttpResponse = HttpClient.newHttpClient() - .send( - HttpRequest.newBuilder() - .uri( - URI( - "https://rife2.com/tests-badge/update/net.thauvin.erik/urlencoder?" + - "apiKey=$testBadgeApiKey&" + - "passed=$passed&" + - "failed=$failed&" + - "skipped=$skipped" - ) - ) - .POST(HttpRequest.BodyPublishers.noBody()) - .build(), HttpResponse.BodyHandlers.ofString() - ) - println("RESPONSE: ${response.statusCode()}") - println(response.body()) - } + val apiKey = testBadgeApiKey.orNull + + if (apiKey != null) { + println(apiKey) + val response: HttpResponse = HttpClient.newHttpClient() + .send( + HttpRequest.newBuilder() + .uri( + URI( + "https://rife2.com/tests-badge/update/net.thauvin.erik/urlencoder?" + + "apiKey=$apiKey&" + + "passed=$passed&" + + "failed=$failed&" + + "skipped=$skipped" + ) + ) + .POST(HttpRequest.BodyPublishers.noBody()) + .build(), HttpResponse.BodyHandlers.ofString() + ) + println("RESPONSE: ${response.statusCode()}") + println(response.body()) + } + } } - } } diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 6ebdf1d..eb0edae 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -1,27 +1,17 @@ -import buildsrc.utils.Rife2TestListener -import org.gradle.api.tasks.testing.logging.TestExceptionFormat -import org.gradle.api.tasks.testing.logging.TestLogEvent import org.jetbrains.dokka.gradle.DokkaTask -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { + buildsrc.conventions.lang.`kotlin-jvm` + buildsrc.conventions.publishing id("application") id("com.github.ben-manes.versions") - id("io.gitlab.arturbosch.detekt") - id("java-library") - id("maven-publish") - id("org.jetbrains.kotlin.jvm") - id("org.jetbrains.kotlinx.kover") - - buildsrc.conventions.publishing - buildsrc.conventions.sonarqube } description = "A simple defensive library to encode/decode URL components" val deployDir = project.layout.projectDirectory.dir("deploy") -val mainClassName = "net.thauvin.erik.urlencoder.UrlEncoder" +val urlEncoderMainClass = "net.thauvin.erik.urlencoder.UrlEncoder" dependencies { // testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25") @@ -32,20 +22,14 @@ base { archivesName.set(rootProject.name) } -java { - sourceCompatibility = JavaVersion.VERSION_11 - targetCompatibility = JavaVersion.VERSION_11 - withSourcesJar() -} - application { - mainClass.set(mainClassName) + mainClass.set(urlEncoderMainClass) } tasks { jar { manifest { - attributes["Main-Class"] = mainClassName + attributes["Main-Class"] = urlEncoderMainClass } } @@ -65,30 +49,10 @@ tasks { dependsOn(fatJar) } - withType().configureEach { - kotlinOptions.jvmTarget = java.targetCompatibility.toString() - } - - test { - addTestListener(Rife2TestListener(project.properties["testsBadgeApiKey"]?.toString())) - } - - withType().configureEach { - useJUnitPlatform() - testLogging { - exceptionFormat = TestExceptionFormat.FULL - events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED) - } - } - withType().configureEach { destination = file("$projectDir/pom.xml") } - clean { - delete(deployDir) - } - withType().configureEach { dokkaSourceSets { named("main") { @@ -113,8 +77,8 @@ tasks { dependsOn(build, copyToDeploy) } - "sonar" { - dependsOn(koverReport) + clean { + delete(deployDir) } } @@ -122,7 +86,7 @@ publishing { publications { create("mavenJava") { from(components["java"]) - artifactId = "${rootProject.name}-lib" + artifactId = rootProject.name artifact(tasks.javadocJar) } }