tweaks and fixes

- introduce kotlin-jvm convention
- tidy up code-quality build config
This commit is contained in:
Adam 2023-05-31 22:48:49 +02:00
parent b8a394c9ad
commit 6670346890
5 changed files with 119 additions and 89 deletions

View file

@ -1,18 +1,31 @@
package buildsrc.conventions 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 */ /** common config for all subprojects */
plugins { plugins {
base base
} }
if (project != rootProject) { if (project != rootProject) {
project.version = rootProject.version project.version = rootProject.version
project.group = rootProject.group project.group = rootProject.group
} }
tasks.withType<AbstractArchiveTask>().configureEach { tasks.withType<AbstractArchiveTask>().configureEach {
// https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives // https://docs.gradle.org/current/userguide/working_with_files.html#sec:reproducible_archives
isPreserveFileTimestamps = false isPreserveFileTimestamps = false
isReproducibleFileOrder = true isReproducibleFileOrder = true
}
tasks.withType<Test>().configureEach {
val testsBadgeApiKey = providers.gradleProperty("testsBadgeApiKey")
addTestListener(Rife2TestListener(testsBadgeApiKey))
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
} }

View file

@ -17,17 +17,25 @@
package buildsrc.conventions package buildsrc.conventions
import org.sonarqube.gradle.SonarTask
plugins { plugins {
id("org.sonarqube") id("org.sonarqube")
id("io.gitlab.arturbosch.detekt")
id("org.jetbrains.kotlinx.kover")
} }
sonarqube { sonarqube {
properties { properties {
property("sonar.projectName", rootProject.name) property("sonar.projectName", rootProject.name)
property("sonar.projectKey", "ethauvin_${rootProject.name}") property("sonar.projectKey", "ethauvin_${rootProject.name}")
property("sonar.organization", "ethauvin-github") property("sonar.organization", "ethauvin-github")
property("sonar.host.url", "https://sonarcloud.io") property("sonar.host.url", "https://sonarcloud.io")
property("sonar.sourceEncoding", "UTF-8") property("sonar.sourceEncoding", "UTF-8")
property("sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/kover/xml/report.xml") property("sonar.coverage.jacoco.xmlReportPaths", "${project.buildDir}/reports/kover/report.xml")
} }
}
tasks.withType<SonarTask>().configureEach {
dependsOn(tasks.matching { it.name == "koverXmlReport" })
} }

View file

@ -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<KotlinCompile>().configureEach {
compilerOptions {
jvmTarget.set(JvmTarget.JVM_11)
}
}
tasks.withType<Test>().configureEach {
useJUnitPlatform()
}

View file

@ -17,6 +17,7 @@
package buildsrc.utils package buildsrc.utils
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.testing.TestDescriptor import org.gradle.api.tasks.testing.TestDescriptor
import org.gradle.api.tasks.testing.TestListener import org.gradle.api.tasks.testing.TestListener
import org.gradle.api.tasks.testing.TestResult import org.gradle.api.tasks.testing.TestResult
@ -26,37 +27,39 @@ import java.net.http.HttpRequest
import java.net.http.HttpResponse import java.net.http.HttpResponse
class Rife2TestListener( class Rife2TestListener(
private val testBadgeApiKey: String? private val testBadgeApiKey: Provider<String>
) : TestListener { ) : TestListener {
override fun beforeTest(p0: TestDescriptor?) = Unit override fun beforeTest(p0: TestDescriptor?) = Unit
override fun beforeSuite(p0: TestDescriptor?) = Unit override fun beforeSuite(p0: TestDescriptor?) = Unit
override fun afterTest(desc: TestDescriptor, result: TestResult) = Unit override fun afterTest(desc: TestDescriptor, result: TestResult) = Unit
override fun afterSuite(desc: TestDescriptor, result: TestResult) { override fun afterSuite(desc: TestDescriptor, result: TestResult) {
if (desc.parent == null) { if (desc.parent == null) {
val passed = result.successfulTestCount val passed = result.successfulTestCount
val failed = result.failedTestCount val failed = result.failedTestCount
val skipped = result.skippedTestCount val skipped = result.skippedTestCount
if (testBadgeApiKey != null) { val apiKey = testBadgeApiKey.orNull
println(testBadgeApiKey)
val response: HttpResponse<String> = HttpClient.newHttpClient() if (apiKey != null) {
.send( println(apiKey)
HttpRequest.newBuilder() val response: HttpResponse<String> = HttpClient.newHttpClient()
.uri( .send(
URI( HttpRequest.newBuilder()
"https://rife2.com/tests-badge/update/net.thauvin.erik/urlencoder?" + .uri(
"apiKey=$testBadgeApiKey&" + URI(
"passed=$passed&" + "https://rife2.com/tests-badge/update/net.thauvin.erik/urlencoder?" +
"failed=$failed&" + "apiKey=$apiKey&" +
"skipped=$skipped" "passed=$passed&" +
) "failed=$failed&" +
) "skipped=$skipped"
.POST(HttpRequest.BodyPublishers.noBody()) )
.build(), HttpResponse.BodyHandlers.ofString() )
) .POST(HttpRequest.BodyPublishers.noBody())
println("RESPONSE: ${response.statusCode()}") .build(), HttpResponse.BodyHandlers.ofString()
println(response.body()) )
} println("RESPONSE: ${response.statusCode()}")
println(response.body())
}
}
} }
}
} }

View file

@ -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.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
buildsrc.conventions.lang.`kotlin-jvm`
buildsrc.conventions.publishing
id("application") id("application")
id("com.github.ben-manes.versions") 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" description = "A simple defensive library to encode/decode URL components"
val deployDir = project.layout.projectDirectory.dir("deploy") val deployDir = project.layout.projectDirectory.dir("deploy")
val mainClassName = "net.thauvin.erik.urlencoder.UrlEncoder" val urlEncoderMainClass = "net.thauvin.erik.urlencoder.UrlEncoder"
dependencies { dependencies {
// testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25") // testImplementation("com.willowtreeapps.assertk:assertk-jvm:0.25")
@ -32,20 +22,14 @@ base {
archivesName.set(rootProject.name) archivesName.set(rootProject.name)
} }
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
withSourcesJar()
}
application { application {
mainClass.set(mainClassName) mainClass.set(urlEncoderMainClass)
} }
tasks { tasks {
jar { jar {
manifest { manifest {
attributes["Main-Class"] = mainClassName attributes["Main-Class"] = urlEncoderMainClass
} }
} }
@ -65,30 +49,10 @@ tasks {
dependsOn(fatJar) dependsOn(fatJar)
} }
withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = java.targetCompatibility.toString()
}
test {
addTestListener(Rife2TestListener(project.properties["testsBadgeApiKey"]?.toString()))
}
withType<Test>().configureEach {
useJUnitPlatform()
testLogging {
exceptionFormat = TestExceptionFormat.FULL
events = setOf(TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED)
}
}
withType<GenerateMavenPom>().configureEach { withType<GenerateMavenPom>().configureEach {
destination = file("$projectDir/pom.xml") destination = file("$projectDir/pom.xml")
} }
clean {
delete(deployDir)
}
withType<DokkaTask>().configureEach { withType<DokkaTask>().configureEach {
dokkaSourceSets { dokkaSourceSets {
named("main") { named("main") {
@ -113,8 +77,8 @@ tasks {
dependsOn(build, copyToDeploy) dependsOn(build, copyToDeploy)
} }
"sonar" { clean {
dependsOn(koverReport) delete(deployDir)
} }
} }
@ -122,7 +86,7 @@ publishing {
publications { publications {
create<MavenPublication>("mavenJava") { create<MavenPublication>("mavenJava") {
from(components["java"]) from(components["java"])
artifactId = "${rootProject.name}-lib" artifactId = rootProject.name
artifact(tasks.javadocJar) artifact(tasks.javadocJar)
} }
} }