Skip to content

Commit

Permalink
Support run. and test. properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Virtlink committed Jul 24, 2024
1 parent 80d078c commit 6b68b7f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -34,7 +37,7 @@ class JavaConventionPlugin: Plugin<Project> {
withJavadocJar()
}

tasks.withType<JavaCompile> {
tasks.withType<JavaCompile>().configureEach {
// Use UTF-8 encoding by default
options.encoding = "UTF-8"
// Warn for unchecked casts
Expand All @@ -47,7 +50,7 @@ class JavaConventionPlugin: Plugin<Project> {
options.compilerArgs.add("-Xdoclint:none")
}

tasks.withType<Javadoc> {
tasks.withType<Javadoc>().configureEach {
options {
this as CoreJavadocOptions
// Use UTF-8 encoding by default
Expand All @@ -61,6 +64,35 @@ class JavaConventionPlugin: Plugin<Project> {
quiet()
}
}

tasks.named<Test>(JavaPlugin.TEST_TASK_NAME) {
addPropertiesWithPrefix("test.", project)
}
}

plugins.withType<ApplicationPlugin> {
tasks.named<JavaExec>(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<String, *>) {
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())
}
}
}
10 changes: 8 additions & 2 deletions docs/content/conventions/java.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 6b68b7f

Please sign in to comment.