From b6589e100729db007b99d3c124d5a28d4052f122 Mon Sep 17 00:00:00 2001 From: "Daniel A. A. Pelsmaeker" Date: Wed, 29 May 2024 17:05:12 +0200 Subject: [PATCH] Add JavaPlugin --- convention-plugin/build.gradle.kts | 4 +- .../gradle/JavaConventionExtension.kt | 17 +++++ .../metaborg/gradle/JavaConventionPlugin.kt | 73 +++++++++++++++++++ .../gradle/MavenPublishConventionPlugin.kt | 23 ++++++ 4 files changed, 115 insertions(+), 2 deletions(-) create mode 100644 convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionExtension.kt create mode 100644 convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionPlugin.kt create mode 100644 convention-plugin/src/main/kotlin/org/metaborg/gradle/MavenPublishConventionPlugin.kt diff --git a/convention-plugin/build.gradle.kts b/convention-plugin/build.gradle.kts index e737c9e..b6027cb 100644 --- a/convention-plugin/build.gradle.kts +++ b/convention-plugin/build.gradle.kts @@ -18,8 +18,8 @@ dependencies { gradlePlugin { plugins { create("gradle-convention.java") { - id = "org.metaborg.gradle-convention.java" - implementationClass = "org.metaborg.gradleconvention.JavaPlugin" + id = "org.metaborg.gradle.java-convention" + implementationClass = "org.metaborg.gradle.JavaConventionPlugin" } } } diff --git a/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionExtension.kt b/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionExtension.kt new file mode 100644 index 0000000..756522d --- /dev/null +++ b/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionExtension.kt @@ -0,0 +1,17 @@ +package org.metaborg.gradle + +import org.gradle.api.provider.Property +import org.gradle.jvm.toolchain.JavaLanguageVersion + +/** Configuration for the Java convention. */ +interface JavaConventionExtension { + /** The Java version to compile to. */ + val javaVersion: Property + + /** + * Sets the convention (default values) for the configuration extension. + */ + fun setConvention() { + javaVersion.convention(JavaLanguageVersion.of(11)) + } +} \ No newline at end of file diff --git a/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionPlugin.kt b/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionPlugin.kt new file mode 100644 index 0000000..286b6a5 --- /dev/null +++ b/convention-plugin/src/main/kotlin/org/metaborg/gradle/JavaConventionPlugin.kt @@ -0,0 +1,73 @@ +package org.metaborg.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.plugins.JavaPlugin +import org.gradle.api.plugins.JavaPluginExtension +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.* + +/** + * Configures a Gradle project that builds a Java library or application. + * + * Also apply the `java`, `java-library` or `application` plugin to the project. + */ +@Suppress("unused") +class JavaConventionPlugin: Plugin { + override fun apply(project: Project): Unit = with(project) { + // Apply the configuration extension + val extension = extensions.create("javaConvention") + extension.setConvention() + + repositories { + // Maven Artifacts repository + maven(url = "https://artifacts.metaborg.org/content/groups/public/") + // Repository for other packages + mavenCentral() + } + + plugins.withType { + tasks.withType { + // Support any JUnit 5 compatible test runner + useJUnitPlatform() + } + + configure { + // Compile to a specific Java version + toolchain.languageVersion.set(extension.javaVersion) + // Generate a sources JAR + withSourcesJar() + // Generate a JavaDoc JAR + withJavadocJar() + } + + tasks.withType { + // Use UTF-8 encoding by default + options.encoding = "UTF-8" + // Warn for unchecked casts + options.compilerArgs.add("-Xlint:unchecked") + // Show more details when the project uses API that is deprecated + options.compilerArgs.add("-Xlint:deprecation") + // Show more details when the project uses API scheduled for removal + options.compilerArgs.add("-Xlint:removal") + // Silence doclint warnings + options.compilerArgs.add("-Xdoclint:none") + } + + tasks.withType { + options { + this as CoreJavadocOptions + // Use UTF-8 encoding by default + encoding = "UTF-8" + charset("UTF-8") + // Silence doclint warnings + addStringOption("Xdoclint:none", "-quiet") + quiet() + } + } + } + } +} diff --git a/convention-plugin/src/main/kotlin/org/metaborg/gradle/MavenPublishConventionPlugin.kt b/convention-plugin/src/main/kotlin/org/metaborg/gradle/MavenPublishConventionPlugin.kt new file mode 100644 index 0000000..7a78596 --- /dev/null +++ b/convention-plugin/src/main/kotlin/org/metaborg/gradle/MavenPublishConventionPlugin.kt @@ -0,0 +1,23 @@ +package org.metaborg.gradle + +import org.gradle.api.Plugin +import org.gradle.api.Project + +class MavenPublishConventionPlugin: Plugin { + override fun apply(target: Project) { + // maven-publish plugin + // - Configure maven-publish + // configure SCM (GitHub by default, etc) + // configure owner + // configure name + // configure license (Apache 2.0 by default) + // configure developers + // configure inception year + // configure publish to GitHub packages (by default if SCM = GitHub) + // configure publish to Metaborg Artifacts (by default) + // configure publish to Maven Central (optional) + // - Configure signing (optional) + + TODO("Not yet implemented") + } +} \ No newline at end of file