Skip to content

Commit

Permalink
Refactor to use convention plugins for common configuration (#117)
Browse files Browse the repository at this point in the history
Refactor project to use buildSrc plugins for common configuration rather than looping through subprojects.
  • Loading branch information
hpmellema committed Jan 12, 2024
1 parent 190e9c4 commit c1128e9
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 208 deletions.
203 changes: 2 additions & 201 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,214 +13,20 @@
* permissions and limitations under the License.
*/

import com.github.spotbugs.snom.Effort
import com.github.spotbugs.snom.SpotBugsTask
import com.adarshr.gradle.testlogger.TestLoggerExtension
import org.jreleaser.model.Active

plugins {
`java-library`
jacoco
id("com.github.spotbugs") version "5.0.14"
id("com.adarshr.test-logger") version "3.2.0"
id("com.gradle.plugin-publish") version "1.2.1" apply false
base
id("org.jreleaser") version "1.9.0"
}

// The root project doesn't produce a JAR.
tasks["jar"].enabled = false

val pluginVersion = project.file("VERSION").readText().replace(System.lineSeparator(), "")
allprojects {
group = "software.amazon.smithy.gradle"
version = pluginVersion
}
println("Smithy Gradle version: '${pluginVersion}'")

// JReleaser publishes artifacts from a local staging repository, rather than maven local.
// https://jreleaser.org/guide/latest/examples/maven/staging-artifacts.html#_gradle
val stagingDirectory = "$buildDir/staging"

subprojects {
val subproject = this

if (subproject.name != "integ-test-utils") {
apply(plugin = "java-gradle-plugin")
apply(plugin = "com.gradle.plugin-publish")
} else {
apply(plugin = "java-library")
}

/*
* Java
* ====================================================
*/

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

// Use Junit5's test runner.
tasks.withType<Test> {
useJUnitPlatform()
}

// Suppress warnings in javadocs
tasks.withType<Javadoc> {
(options as StandardJavadocDocletOptions).addStringOption("Xdoclint:-html", "-quiet")
}

apply(plugin = "com.adarshr.test-logger")
configure<TestLoggerExtension> {
showExceptions = true
showStackTraces = true
showFullStackTraces = false
showCauses = true
showSummary = true
showPassed = true
showSkipped = true
showFailed = true
showOnlySlow = false
showStandardStreams = true
showPassedStandardStreams = false
showSkippedStandardStreams = false
showFailedStandardStreams = true
logLevel = LogLevel.LIFECYCLE
}

dependencies {
implementation("software.amazon.smithy:smithy-model:[1.0, 2.0[")
implementation("software.amazon.smithy:smithy-build:[1.0, 2.0[")
implementation("software.amazon.smithy:smithy-cli:[1.0, 2.0[")

testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.0")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.0")
testImplementation("org.junit.jupiter:junit-jupiter-params:5.4.0")
testImplementation("org.hamcrest:hamcrest:2.1")
testImplementation(project(":integ-test-utils"))
}

// Reusable license copySpec
val licenseSpec = copySpec {
from("${project.rootDir}/LICENSE")
from("${project.rootDir}/NOTICE")
}

if (subproject.name != "integ-test-utils") {
// Configure all jars to include license info
tasks.withType<Jar>() {
metaInf.with(licenseSpec)
}

/*
* Configure integration tests
* ====================================================
*/
sourceSets {
create("it") {
compileClasspath += sourceSets["main"].output + configurations["testRuntimeClasspath"]
runtimeClasspath += output + compileClasspath + sourceSets["test"].runtimeClasspath
}
}

tasks.register<Test>("integTest") {
useJUnitPlatform()
testClassesDirs = sourceSets["it"].output.classesDirs
classpath = sourceSets["it"].runtimeClasspath
maxParallelForks = Runtime.getRuntime().availableProcessors() / 2
}

afterEvaluate {
tasks["integTest"].dependsOn("publishToMavenLocal")

// Always run javadoc and integration tests after build.
tasks["assemble"].dependsOn("javadoc")
tasks["build"].finalizedBy(tasks["integTest"])
}

/*
* Common plugin settings
* ====================================================
*/
apply(plugin = "com.gradle.plugin-publish")
configure<GradlePluginDevelopmentExtension> {
website.set("https://smithy.io")
vcsUrl.set("https://github.com/smithy-lang/smithy-gradle-plugin")
}

/*
* Staging repository
* ====================================================
*
* Configure publication to staging repo for jreleaser
*/
configure<PublishingExtension> {
repositories {
maven {
name = "stagingRepository"
url = uri(stagingDirectory)
}
}
}

/*
* CheckStyle
* ====================================================
*
* Apply CheckStyle to source files but not tests.
*/
apply(plugin = "checkstyle")
tasks["checkstyleTest"].enabled = false
tasks["checkstyleIt"].enabled = false

/*
* Code coverage
* ====================================================
*
* Create code coverage reports after running tests.
*/
apply(plugin = "jacoco")
// Always run the jacoco test report after testing.
tasks["test"].finalizedBy(tasks["jacocoTestReport"])
// Configure jacoco to generate an HTML report.
tasks.jacocoTestReport {
reports {
xml.required.set(false)
csv.required.set(false)
html.outputLocation.set(file("$buildDir/reports/jacoco"))
}
}

/*
* Spotbugs
* ====================================================
*
* Run spotbugs against source files and configure suppressions.
*/
apply(plugin = "com.github.spotbugs")
// We don't need to lint tests.
tasks["spotbugsTest"].enabled = false
tasks["spotbugsIt"].enabled = false

// Configure the bug filter for spotbugs.
tasks.withType<SpotBugsTask>().configureEach {
effort.set(Effort.MAX)
excludeFilter.set(project.file("${project.rootDir}/config/spotbugs/filter.xml"))
}
}

/*
* Repositories
* ====================================================
*/
repositories {
mavenLocal()
mavenCentral()
}
}


/*
* Jreleaser (https://jreleaser.org) config.
*/
Expand Down Expand Up @@ -262,14 +68,9 @@ jreleaser {
snapshotUrl = "https://aws.oss.sonatype.org/content/repositories/snapshots"
closeRepository.set(false)
releaseRepository.set(false)
stagingRepositories.add(stagingDirectory)
stagingRepositories.add("${rootProject.buildDir}/staging")
}
}
}
}
}

repositories {
mavenLocal()
mavenCentral()
}
20 changes: 20 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
// Support convention plugins written in Kotlin. Convention plugins are
// build scripts in 'src/main' that automatically become available as
// plugins in the main build.
`kotlin-dsl`
}

repositories {
// Use the plugin portal to apply community plugins in convention plugins.
gradlePluginPortal()
}

dependencies {
// Java convention dependencies
implementation("com.github.spotbugs.snom:spotbugs-gradle-plugin:6.0.6")
implementation("com.adarshr:gradle-test-logger-plugin:4.0.0")

// Plugin convention dependencies
implementation("com.gradle.publish:plugin-publish-plugin:1.2.1")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

plugins {
`java-library`
checkstyle
}

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

dependencies {
implementation("software.amazon.smithy:smithy-model:[1.0, 2.0[")
implementation("software.amazon.smithy:smithy-build:[1.0, 2.0[")
implementation("software.amazon.smithy:smithy-cli:[1.0, 2.0[")
}

//// ==== Licensing =====
// Reusable license copySpec
val licenseSpec = copySpec {
from("${project.rootDir}/LICENSE")
from("${project.rootDir}/NOTICE")
}

// Configure all jars to include license info
tasks.withType<Jar>() {
metaInf.with(licenseSpec)
}



// Suppress warnings in javadocs
tasks.withType<Javadoc> {
(options as StandardJavadocDocletOptions).addStringOption("Xdoclint:-html", "-quiet")
}

/*
* CheckStyle
* ====================================================
*
* Apply CheckStyle to source files but not tests.
*/
tasks["checkstyleTest"].enabled = false

/*
* Repositories
* ====================================================
*/
repositories {
mavenLocal()
mavenCentral()
}
Loading

0 comments on commit c1128e9

Please sign in to comment.