From 6b68b7ffefdc318c79947bcdb88f3c5374785715 Mon Sep 17 00:00:00 2001 From: "Daniel A. A. Pelsmaeker" Date: Wed, 24 Jul 2024 16:01:26 +0200 Subject: [PATCH] Support run. and test. properties --- .../convention/JavaConventionPlugin.kt | 36 +++++++++++++++++-- docs/content/conventions/java.md | 10 ++++-- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/convention-plugin/src/main/kotlin/org/metaborg/convention/JavaConventionPlugin.kt b/convention-plugin/src/main/kotlin/org/metaborg/convention/JavaConventionPlugin.kt index 3a2729e..ba59ace 100644 --- a/convention-plugin/src/main/kotlin/org/metaborg/convention/JavaConventionPlugin.kt +++ b/convention-plugin/src/main/kotlin/org/metaborg/convention/JavaConventionPlugin.kt @@ -2,13 +2,16 @@ package org.metaborg.convention import org.gradle.api.Plugin import org.gradle.api.Project +import org.gradle.api.plugins.ApplicationPlugin import org.gradle.api.plugins.JavaPlugin import org.gradle.api.plugins.JavaPluginExtension +import org.gradle.api.tasks.JavaExec import org.gradle.api.tasks.compile.JavaCompile import org.gradle.api.tasks.javadoc.Javadoc import org.gradle.api.tasks.testing.Test import org.gradle.external.javadoc.CoreJavadocOptions import org.gradle.kotlin.dsl.* +import org.gradle.process.JavaForkOptions /** * Configures a Gradle project that builds a Java library or application. @@ -34,7 +37,7 @@ class JavaConventionPlugin: Plugin { withJavadocJar() } - tasks.withType { + tasks.withType().configureEach { // Use UTF-8 encoding by default options.encoding = "UTF-8" // Warn for unchecked casts @@ -47,7 +50,7 @@ class JavaConventionPlugin: Plugin { options.compilerArgs.add("-Xdoclint:none") } - tasks.withType { + tasks.withType().configureEach { options { this as CoreJavadocOptions // Use UTF-8 encoding by default @@ -61,6 +64,35 @@ class JavaConventionPlugin: Plugin { quiet() } } + + tasks.named(JavaPlugin.TEST_TASK_NAME) { + addPropertiesWithPrefix("test.", project) + } + } + + plugins.withType { + tasks.named(ApplicationPlugin.TASK_RUN_NAME) { + addPropertiesWithPrefix("run.", project) + } + } + } + + /** + * Adds all system properties and project properties that have the given [prefix] (case-insensitive) + * to the properties of the Java process (without the prefix). + * + * @param prefix The property prefix to look for. + * @param project The Gradle project. + */ + private fun JavaForkOptions.addPropertiesWithPrefix(prefix: String, project: Project) { + @Suppress("UNCHECKED_CAST") + for ((k, v) in System.getProperties() as Map) { + if (!k.startsWith(prefix, ignoreCase = true)) continue + systemProperty(k.substring(prefix.length), v.toString()) + } + for ((k, v) in project.properties) { + if (!k.startsWith(prefix, ignoreCase = true)) continue + systemProperty(k.substring(prefix.length), v.toString()) } } } diff --git a/docs/content/conventions/java.md b/docs/content/conventions/java.md index ee7eea0..e5e0185 100644 --- a/docs/content/conventions/java.md +++ b/docs/content/conventions/java.md @@ -2,9 +2,15 @@ title: "Java Convention" --- # Java Convention Plugin -The Java convention plugin configures a project to be built using Java 11 by default, but this can be overridden. +The Java convention plugin configures a project to: -To use the plugin, you need to apply both the Java convention plugin and one of the Java plugins (`java-library`, `java`): +- build using a Java 11 toolchain by default; +- silence JavaDoc warnings and errors as much as possible; +- use UTF-8 encoding; +- transfer properties prefixed with `app.` to the `run` task if the Java `application` plugin is applied; +- transfer properties prefixed with `test.` to the `test` task. + +To use the plugin, you need to apply both the Java convention plugin and one of the Java plugins (e.g., `java-library`, `java`): ```kotlin title="build.gradle.kts" plugins {