Skip to content

Commit

Permalink
Model parameters using org.gradlex.build-parameters (#3170)
Browse files Browse the repository at this point in the history
Introduces the org.gradlex.build-parameters plugin to model parameters
that can be supplied to this build. For this the existing build logic
in gradle/plugins is moved to a submodule called 'common' and a new
module called 'build-parameters' is introduced. This way, build logic
can access build parameters using the `buildParameters` pre-compiled
accessor in Kotlin.

Occurences of project.findProperty as well as project.hasProperty
are replaced by properly modeled build parameters. Since validation
of parameters is not turned off, the build-parameters plugin will
fail the build is a parameter is supplied which is not modeled.
For that reason, any other parameters, such as signing or deployment
parameters that are supplied by developers only during releasing need to
be added before this can be merged.
  • Loading branch information
britter authored Mar 9, 2023
1 parent 6976f0d commit 557c419
Show file tree
Hide file tree
Showing 43 changed files with 136 additions and 50 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[*.{kt,kts}]
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false

[*.kts]
indent_style = tab
2 changes: 1 addition & 1 deletion .github/actions/run-gradle/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ runs:
with:
arguments: |
-Porg.gradle.java.installations.auto-download=false
-PenablePredictiveTestSelection=${{ github.event_name == 'pull_request' }}
-Penterprise.predictiveTestSelection.enabled=${{ github.event_name == 'pull_request' }}
"-Dscan.value.GitHub job=${{ github.job }}"
javaToolchains
${{ inputs.arguments }}
6 changes: 3 additions & 3 deletions .github/workflows/cross-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ on:
- '*'

env:
ORG_GRADLE_PROJECT_enableTestDistribution: true
ORG_GRADLE_PROJECT_junitBuildCacheUsername: ${{ secrets.BUILD_CACHE_USERNAME }}
ORG_GRADLE_PROJECT_junitBuildCachePassword: ${{ secrets.BUILD_CACHE_PASSWORD }}
ENTERPRISE_TESTDISTRIBUTION_ENABLED: true
BUILDCACHE_USERNAME: ${{ secrets.BUILD_CACHE_USERNAME }}
BUILDCACHE_PASSWORD: ${{ secrets.BUILD_CACHE_PASSWORD }}
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}

jobs:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ on:
- '*'

env:
ORG_GRADLE_PROJECT_enableTestDistribution: true
ORG_GRADLE_PROJECT_junitBuildCacheUsername: ${{ secrets.BUILD_CACHE_USERNAME }}
ORG_GRADLE_PROJECT_junitBuildCachePassword: ${{ secrets.BUILD_CACHE_PASSWORD }}
ENTERPRISE_TESTDISTRIBUTION_ENABLED: true
BUILDCACHE_USERNAME: ${{ secrets.BUILD_CACHE_USERNAME }}
BUILDCACHE_PASSWORD: ${{ secrets.BUILD_CACHE_PASSWORD }}
GRADLE_ENTERPRISE_ACCESS_KEY: ${{ secrets.GRADLE_ENTERPRISE_ACCESS_KEY }}

jobs:
Expand All @@ -38,7 +38,7 @@ jobs:
uses: ./.github/actions/main-build
with:
arguments: |
-PenableJaCoCo
-Ptesting.enableJaCoCo
build
jacocoRootReport
prepareDocsForUploadToGhPages
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ builds of the next OpenJDK.
Code coverage using [JaCoCo] for the latest build is available on [Codecov].

A code coverage report can also be generated locally via the [Gradle Wrapper] by
executing `./gradlew -PenableJaCoCo clean jacocoRootReport`. The results will be available
executing `./gradlew -Ptesting.enableJaCoCo clean jacocoRootReport`. The results will be available
in `build/reports/jacoco/jacocoRootReport/html/index.html`.

## Gradle Enterprise
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,5 @@ nexusPublishing {
}

nohttp {
source.exclude("**/.gradle/**", "gradle/plugins/build/**")
source.exclude("**/.gradle/**", "gradle/plugins/**/build/**")
}
3 changes: 2 additions & 1 deletion documentation/documentation.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ plugins {
id("org.asciidoctor.jvm.convert")
id("org.asciidoctor.jvm.pdf")
id("org.ajoberstar.git-publish")
id("junitbuild.build-parameters")
id("junitbuild.kotlin-library-conventions")
id("junitbuild.testing-conventions")
}
Expand Down Expand Up @@ -71,7 +72,7 @@ val snapshot = rootProject.version.toString().contains("SNAPSHOT")
val docsVersion = if (snapshot) "snapshot" else rootProject.version
val releaseBranch = if (snapshot) "HEAD" else "r${rootProject.version}"
val docsDir = file("$buildDir/ghpages-docs")
val replaceCurrentDocs = project.hasProperty("replaceCurrentDocs")
val replaceCurrentDocs = buildParameters.documentation.replaceCurrentDocs
val uploadPdfs = !snapshot
val userGuidePdfFileName = "junit-user-guide-${rootProject.version}.pdf"
val ota4jDocVersion = if (libs.versions.opentest4j.get().contains("SNAPSHOT")) "snapshot" else libs.versions.opentest4j.get()
Expand Down
81 changes: 81 additions & 0 deletions gradle/plugins/build-parameters/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
plugins {
id("org.gradlex.build-parameters") version "1.4.3"
}

group = "junitbuild"

buildParameters {
pluginId("junitbuild.build-parameters")
bool("ci") {
description.set("Whether or not this build is running in a CI environment")
defaultValue.set(false)
fromEnvironment()
}
integer("javaToolchainVersion") {
description.set("Defines the Java toolchain version to use for compiling code")
}
group("buildCache") {
string("username") {
description.set("Username to authenticate with the remote build cache")
fromEnvironment()
}
string("password") {
description.set("Password to authenticate with the remote build cache")
fromEnvironment()
}
string("url") {
description.set("URL to the remote build cache")
fromEnvironment()
}
}
group("documentation") {
description.set("Parameters controlling how the documentation is built")
bool("replaceCurrentDocs") {
description.set("The documentation that is being deployed will replace what's currently deployed as 'current'")
defaultValue.set(false)
}
}
group("enterprise") {
description.set("Parameters controlling Gradle Enterprise features")
group("predictiveTestSelection") {
bool("enabled") {
description.set("Whether or not to use Predictive Test Selection for selecting tests to execute")
defaultValue.set(true)
}
}
group("testDistribution") {
bool("enabled") {
description.set("Whether or not to use Test Distribution for executing tests")
defaultValue.set(false)
fromEnvironment()
}
integer("maxLocalExecutors") {
description.set("How many local executors to use for executing tests")
defaultValue.set(1)
}
integer("maxRemoteExecutors") {
description.set("How many remote executors to request for executing tests")
}
}
}
group("testing") {
description.set("Testing related parameters")
bool("enableJaCoCo") {
description.set("Enables JaCoCo test coverage reporting")
defaultValue.set(false)
}
bool("enableJFR") {
description.set("Enables Java Flight Recorder functionality")
defaultValue.set(false)
}
integer("retries") {
description.set("Configures the number of times failing test are retried")
}
}
}

tasks {
withType<JavaCompile>().configureEach {
options.release.set(11)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repositories {
}

dependencies {
implementation(project(":build-parameters"))
implementation(kotlin("gradle-plugin"))
implementation("biz.aQute.bnd:biz.aQute.bnd.gradle:6.4.0")
implementation("com.diffplug.spotless:spotless-plugin-gradle:6.15.0")
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
plugins {
jacoco
id("junitbuild.build-parameters")
}

val enableJaCoCo = project.hasProperty("enableJaCoCo")

jacoco {
toolVersion = requiredVersionFromLibs("jacoco")
}

tasks.withType<JacocoReport>().configureEach {
enabled = enableJaCoCo
enabled = buildParameters.testing.enableJaCoCo
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import org.gradle.api.attributes.LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE

plugins {
java
id("junitbuild.build-parameters")
id("junitbuild.jacoco-conventions")
}

val mavenizedProjects: List<Project> by rootProject.extra
val enableJaCoCo = project.hasProperty("enableJaCoCo")

tasks.withType<Test>().configureEach {
configure<JacocoTaskExtension> {
isEnabled = enableJaCoCo
isEnabled = buildParameters.testing.enableJaCoCo
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension

plugins {
id("junitbuild.build-parameters")
}

project.pluginManager.withPlugin("java") {
val javaToolchainVersion: String? by project
val defaultLanguageVersion = JavaLanguageVersion.of(17)
val javaLanguageVersion = javaToolchainVersion?.let { JavaLanguageVersion.of(it) } ?: defaultLanguageVersion
val javaLanguageVersion = buildParameters.javaToolchainVersion.map { JavaLanguageVersion.of(it) }.getOrElse(defaultLanguageVersion)

val extension = the<JavaPluginExtension>()
val javaToolchainService = the<JavaToolchainService>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ plugins {
`maven-publish`
signing
id("junitbuild.base-conventions")
id("junitbuild.build-parameters")
}

val isSnapshot = project.version.toString().contains("SNAPSHOT")
val isContinuousIntegrationEnvironment = System.getenv("CI")?.toBoolean() ?: false

val jupiterProjects: List<Project> by rootProject
val platformProjects: List<Project> by rootProject
Expand Down Expand Up @@ -43,7 +43,7 @@ tasks.withType<PublishToMavenLocal>().configureEach {
signing {
useGpgCmd()
sign(publishing.publications)
isRequired = !(isSnapshot || isContinuousIntegrationEnvironment)
isRequired = !(isSnapshot || buildParameters.ci)
}

tasks.withType<Sign>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.gradle.internal.os.OperatingSystem

plugins {
`java-library`
id("junitbuild.build-parameters")
}

tasks.withType<Test>().configureEach {
Expand All @@ -17,18 +18,14 @@ tasks.withType<Test>().configureEach {
events = setOf(FAILED)
exceptionFormat = FULL
}
val isCiServer = System.getenv("CI") != null
retry {
maxRetries.set(providers.gradleProperty("retries").map(String::toInt).orElse(if (isCiServer) 2 else 0))
maxRetries.set(buildParameters.testing.retries.orElse(if (buildParameters.ci) 2 else 0))
}
distribution {
enabled.convention(providers.gradleProperty("enableTestDistribution")
.map(String::toBoolean)
.map { enabled -> enabled && (!isCiServer || System.getenv("GRADLE_ENTERPRISE_ACCESS_KEY").isNotBlank()) }
.orElse(false))
maxLocalExecutors.set(providers.gradleProperty("testDistribution.maxLocalExecutors").map(String::toInt).orElse(1))
maxRemoteExecutors.set(providers.gradleProperty("testDistribution.maxRemoteExecutors").map(String::toInt))
if (isCiServer) {
enabled.convention(buildParameters.enterprise.testDistribution.enabled && (!buildParameters.ci || System.getenv("GRADLE_ENTERPRISE_ACCESS_KEY").isNotBlank()))
maxLocalExecutors.set(buildParameters.enterprise.testDistribution.maxLocalExecutors)
maxRemoteExecutors.set(buildParameters.enterprise.testDistribution.maxRemoteExecutors)
if (buildParameters.ci) {
when {
OperatingSystem.current().isLinux -> requirements.add("os=linux")
OperatingSystem.current().isWindows -> requirements.add("os=windows")
Expand All @@ -37,7 +34,7 @@ tasks.withType<Test>().configureEach {
}
}
predictiveSelection {
enabled.set(providers.gradleProperty("enablePredictiveTestSelection").map(String::toBoolean).orElse(true))
enabled.set(buildParameters.enterprise.predictiveTestSelection.enabled)

// Ensure PTS works when publishing Build Scans to scans.gradle.com
this as PredictiveTestSelectionExtensionInternal
Expand All @@ -46,7 +43,7 @@ tasks.withType<Test>().configureEach {
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
// Required until ASM officially supports the JDK 14
systemProperty("net.bytebuddy.experimental", true)
if (project.hasProperty("enableJFR")) {
if (buildParameters.testing.enableJFR) {
jvmArgs(
"-XX:+UnlockDiagnosticVMOptions",
"-XX:+DebugNonSafepoints",
Expand All @@ -59,7 +56,7 @@ tasks.withType<Test>().configureEach {
trackOperationSystemAsInput()

// Avoid passing unnecessary environment variables to the JVM (from GitHub Actions)
if (isCiServer) {
if (buildParameters.ci) {
environment.remove("RUNNER_TEMP")
environment.remove("GITHUB_ACTION")
}
Expand Down
2 changes: 2 additions & 0 deletions gradle/plugins/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include("build-parameters")
include("common")
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import org.gradle.api.tasks.PathSensitivity.RELATIVE
import org.gradle.jvm.toolchain.internal.NoToolchainAvailableException

plugins {
id("junitbuild.build-parameters")
id("junitbuild.kotlin-library-conventions")
id("junitbuild.testing-conventions")
}
Expand Down Expand Up @@ -135,8 +136,6 @@ class MavenRepo(@get:InputDirectory @get:PathSensitive(RELATIVE) val repoDir: Fi
}

class JavaHomeDir(project: Project, @Input val version: Int) : CommandLineArgumentProvider {
@Internal
val passToolchain = project.providers.gradleProperty("enableTestDistribution").map(String::toBoolean).orElse(false).map { !it }

@Internal
val javaLauncher: Property<JavaLauncher> = project.objects.property<JavaLauncher>()
Expand All @@ -151,7 +150,7 @@ class JavaHomeDir(project: Project, @Input val version: Int) : CommandLineArgume
})

override fun asArguments(): List<String> {
if (passToolchain.get()) {
if (buildParameters.enterprise.testDistribution.enabled) {
val metadata = javaLauncher.map { it.metadata }
val javaHome = metadata.map { it.installationPath.asFile.absolutePath }.orNull
return javaHome?.let { listOf("-Djava.home.$version=$it") } ?: emptyList()
Expand Down
Loading

0 comments on commit 557c419

Please sign in to comment.