Skip to content

Commit

Permalink
move common logic to buildSrc/ and split build to files per project
Browse files Browse the repository at this point in the history
Note: smaller commits are not possible because I couldn't find out how
to apply a bulidSrc/*/*.gradle.kts script to subprojects {} ...
  • Loading branch information
aaschmid committed Jan 5, 2020
1 parent 660e901 commit 33cf594
Show file tree
Hide file tree
Showing 9 changed files with 489 additions and 457 deletions.
498 changes: 42 additions & 456 deletions build.gradle.kts

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
plugins {
kotlin("jvm") version "1.3.50"
`kotlin-dsl`
}

repositories {
mavenCentral()
maven {
url = uri("https://plugins.gradle.org/m2/")
}
}

dependencies {
// external plugins
implementation("com.github.spotbugs:spotbugs-gradle-plugin:3.0.0")
implementation("biz.aQute.bnd:biz.aQute.bnd.gradle:4.3.1")
}
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Dependencies.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Dependencies(private val junit4Version: String, private val junitJupiterVe
val junitJupiterEngine = "org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion"
val junitJupiterParams = "org.junit.jupiter:junit-jupiter-params:$junitJupiterVersion"

val assertJ6 = "org.assertj:assertj-core:1.7.1"
val assertJ6 = "org.assertj:assertj-core:1.6.0"
val mockito6 = "org.mockito:mockito-core:2.28.2"
val assertJ8 = "org.assertj:assertj-core:3.14.0"
val mockito8 = "org.mockito:mockito-core:3.2.4"
Expand Down
231 changes: 231 additions & 0 deletions buildSrc/src/main/kotlin/dataprovider-library.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
import com.github.spotbugs.SpotBugsExtension
import org.w3c.dom.Element
import org.w3c.dom.NodeList
import java.time.LocalDate
import java.time.format.DateTimeFormatter

plugins {
`java-library`
jacoco

id("com.github.spotbugs")
id("biz.aQute.bnd.builder")

`maven-publish`
signing
}

val isBuildOnJenkins: Boolean by rootProject.extra
val skipSpotBugs: Boolean by rootProject.extra
val deps: Dependencies by rootProject.extra

repositories {
mavenCentral()
}

group = rootProject.group
version = rootProject.version

dependencies {
"compileOnly"(deps.spotBugsAnnotations)
"testImplementation"(deps.spotBugsAnnotations)
}

configure<JavaPluginExtension> {
withJavadocJar()
withSourcesJar()
}

// configure after properties are set and integration tests are added
configure<JacocoPluginExtension> {
toolVersion = "0.8.3"
}

configure<SpotBugsExtension> {
toolVersion = "3.1.12"
}

tasks {
withType<JavaCompile> {
options.encoding = "UTF-8"
options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror"))
}

withType<Jar> {
from(rootProject.rootDir) {
include("LICENSE", "NOTICE")
into("META-INF")
}
}

named<Javadoc>("javadoc") {
if (JavaVersion.current().isJava9Compatible) {
(options as StandardJavadocDocletOptions).addBooleanOption("html5", true)
}
}

named<Jar>("jar") {
manifest {
val now = LocalDate.now()

val title = project.the<BasePluginConvention>().archivesBaseName
val company = "TNG Technology Consulting GmbH"
val today = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
val copyright = "${now.year} $company"

attributes(
"Built-By" to "Gradle ${gradle.gradleVersion}",
"Built-Date" to today, // using now would destroy incremental build feature
"Specification-Title" to title,
"Specification-Version" to archiveVersion,
"Specification-Vendor" to company,
"Implementation-Title" to title,
"Implementation-Version" to archiveVersion,
"Implementation-Vendor" to company,
"Issue-Tracker" to "https://github.com/TNG/junit-dataprovider/issues",
"Documentation-URL" to "https://github.com/TNG/junit-dataprovider/wiki",
"Copyright" to copyright,
"License" to "Apache License v2.0, January 2004",

// OSGi / p2 plugin information
"Bundle-Copyright" to copyright,
"Bundle-Name" to title,
"Bundle-SymbolicName" to "${project.group}.$title",
"Bundle-Vendor" to company,
"Export-Package" to "com.tngtech.junit.dataprovider.*",

// Bnd plugin instructions -- remove field because it breaks caching builds with included linux timestamp
"-removeheaders" to "Bnd-LastModified"
)
}
}

withType<com.github.spotbugs.SpotBugsTask> {
enabled = !skipSpotBugs
reports {
html.isEnabled = true
xml.isEnabled = false
}
}

val test = named<Test>("test") {
ignoreFailures = isBuildOnJenkins
}

val touchTestResultsForJenkins = register<TouchTestResults>("touchTestResultsForJenkins") {
tasks(test)
enabled = isBuildOnJenkins
}
named("build") {
dependsOn(touchTestResultsForJenkins)
}
}

// -- sign and publish artifacts -------------------------------------------------------------------------------------
val isReleaseVersion by extra(!project.version.toString().endsWith("-SNAPSHOT"))

// username and password from gradle.properties otherwise empty
val sonatypeUsername by extra(findProperty("sonatypeUsername")?.toString() ?: "")
val sonatypePassword by extra(findProperty("sonatypePassword")?.toString() ?: "")

tasks.withType<GenerateModuleMetadata> {
enabled = isReleaseVersion // signing of these artifacts causes failure for snapshot versions
}

configure<PublishingExtension> {
publications {
register<MavenPublication>("mavenJava") {
val archivesBaseName = project.the<BasePluginConvention>().archivesBaseName
artifactId = archivesBaseName
from(components["java"])
pom {
packaging = "jar"

name.set(archivesBaseName)
description.set(project.description)
url.set("https://github.com/TNG/junit-dataprovider")

developers {
developer {
id.set("aaschmid")
name.set("Andreas Schmid")
email.set("service@aaschmid.de")
}
}

licenses {
license {
name.set("The Apache Software License, Version 2.0")
url.set("http://www.apache.org/licenses/LICENSE-2.0.txt")
distribution.set("repo")
}
}

scm {
connection.set("scm:git@github.com:TNG/junit-dataprovider.git")
developerConnection.set("scm:git@github.com:TNG/junit-dataprovider.git")
url.set("scm:git@github.com:TNG/junit-dataprovider.git")
}

withXml {
fun NodeList.asElementList() = (0 until length).map { this::item }.filterIsInstance<Element>()
fun NodeList.onlyElement() = if (length == 1) item(0) else throw IllegalStateException("Expected only one element but got $length.")

asElement()
.getElementsByTagName("dependencies")
.asElementList()
.flatMap { it.childNodes.asElementList() }
.forEach { dep ->
val groupId = dep.getElementsByTagName("groupId").onlyElement()
val artifactId = dep.getElementsByTagName("artifactId").onlyElement()

// JUnit4
if (groupId.textContent == "junit" && artifactId.textContent == "junit") {
dep.getElementsByTagName("version").onlyElement().textContent = "[4.10,4.12]"
dep.getElementsByTagName("scope").onlyElement().textContent = "provided"
}

// JUnit5
if ((groupId.textContent == "org.junit.jupiter" && artifactId.textContent == "junit-jupiter-engine") ||
(groupId.textContent == "org.junit.jupiter" && artifactId.textContent == "junit-jupiter-params")) {
dep.getElementsByTagName("version").onlyElement().textContent = "[5.5.0-M6,6.0.0)"
dep.getElementsByTagName("scope").onlyElement().textContent = "provided"
}
}
}
}
// finally call `asNode` to get rid of excessive newlines caused by use of asElement
// see also https://github.com/gradle/gradle/issues/7529
afterEvaluate {
pom {
withXml {
asNode()
}
}
}
}
}

repositories {
maven {
val releasesRepoUrl = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
val snapshotRepoUrl = uri("https://oss.sonatype.org/content/repositories/snapshots/")
url = if (isReleaseVersion) releasesRepoUrl else snapshotRepoUrl

credentials {
username = sonatypeUsername
password = sonatypePassword
}

metadataSources {
gradleMetadata()
}
}
}
}

// requires gradle.properties, see http://www.gradle.org/docs/current/userguide/signing_plugin.html
configure<SigningExtension> {
setRequired({ isReleaseVersion && gradle.taskGraph.hasTask("publish") })
sign(the<PublishingExtension>().publications["mavenJava"])
}
46 changes: 46 additions & 0 deletions buildSrc/src/main/kotlin/integration-test.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
plugins {
groovy
}

val isBuildOnJenkins: Boolean by rootProject.extra
val deps: Dependencies by rootProject.extra

sourceSets {
register("integTest") {
compileClasspath += named("main").get().output
runtimeClasspath += named("main").get().output
}
}

configurations {
"integTestImplementation" {
extendsFrom(configurations["testImplementation"])
}
}

dependencies {
"integTestImplementation"(deps.groovy)
}

tasks {
val integTest = register<Test>("integTest") {
group = "verification"
description = "Runs all integration tests."

ignoreFailures = isBuildOnJenkins

classpath = sourceSets["integTest"].runtimeClasspath
testClassesDirs = sourceSets["integTest"].output.classesDirs

dependsOn(named("integTestClasses"))
}

val touchIntegTestResultsForJenkins = register<TouchTestResults>("touchIntegTestResultsForJenkins") {
tasks(integTest)
enabled = isBuildOnJenkins
}

named("build") {
dependsOn(touchIntegTestResultsForJenkins)
}
}
36 changes: 36 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
plugins {
`dataprovider-library`
}

val deps: Dependencies by rootProject.extra

base {
archivesBaseName = "junit-dataprovider-core"
description = "The common core for a TestNG like dataprovider runner for JUnit."
}

java {
sourceCompatibility = JavaVersion.VERSION_1_6
targetCompatibility = JavaVersion.VERSION_1_6
}

dependencies {
"testImplementation"(deps.junit4)

"testImplementation"(deps.assertJ6)
"testImplementation"(deps.mockito6)
}

tasks {
withType<JavaCompile> {
options.compilerArgs.addAll(listOf("-Xlint:-options"))
}

jar {
manifest {
attributes(
"Automatic-Module-Name" to "com.tngtech.junit.dataprovider.core"
)
}
}
}
43 changes: 43 additions & 0 deletions junit-jupiter-params/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
plugins {
`dataprovider-library`
`integration-test`
}

val deps: Dependencies by rootProject.extra

base {
archivesBaseName = "junit-jupiter-params-dataprovider"
description = "A TestNG like dataprovider runner for JUnit Jupiter Parameterized Tests which is largely compatible to JUnit4 dataprovider."
}

java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}

dependencies {
"api"(project(":core"))
"api"(deps.junitJupiterEngine)
"api"(deps.junitJupiterParams)

"testImplementation"(deps.assertJ8)
"testImplementation"(deps.mockito8)
}

tasks {
withType<JavaCompile> {
options.compilerArgs.addAll(listOf("-parameters"))
}

jar {
manifest {
attributes(
"Automatic-Module-Name" to "com.tngtech.junit.dataprovider.jupiter.params"
)
}
}

test {
useJUnitPlatform()
}
}
Loading

0 comments on commit 33cf594

Please sign in to comment.