From aa88309fbde788037cd53df497543c8c40e5e2ae Mon Sep 17 00:00:00 2001 From: daz Date: Tue, 25 Mar 2025 15:19:23 -0600 Subject: [PATCH] Update gradle-plugin sample to use Kotlin DSL --- .../gradle-plugin/gradle/libs.versions.toml | 2 + .../gradle-plugin/plugin/build.gradle | 60 ------------------- .../gradle-plugin/plugin/build.gradle.kts | 40 +++++++++++++ .../GradlePluginPluginFunctionalTest.java | 13 ++-- .../plugin => }/GradlePluginPlugin.java | 6 +- .../plugin => }/GradlePluginPluginTest.java | 8 +-- .../gradle-plugin/settings.gradle | 12 ---- .../gradle-plugin/settings.gradle.kts | 2 + 8 files changed, 57 insertions(+), 86 deletions(-) create mode 100644 .github/workflow-samples/gradle-plugin/gradle/libs.versions.toml delete mode 100644 .github/workflow-samples/gradle-plugin/plugin/build.gradle create mode 100644 .github/workflow-samples/gradle-plugin/plugin/build.gradle.kts rename .github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/{gradle/plugin => }/GradlePluginPluginFunctionalTest.java (77%) rename .github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/{gradle/plugin => }/GradlePluginPlugin.java (72%) rename .github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/{gradle/plugin => }/GradlePluginPluginTest.java (65%) delete mode 100644 .github/workflow-samples/gradle-plugin/settings.gradle create mode 100644 .github/workflow-samples/gradle-plugin/settings.gradle.kts diff --git a/.github/workflow-samples/gradle-plugin/gradle/libs.versions.toml b/.github/workflow-samples/gradle-plugin/gradle/libs.versions.toml new file mode 100644 index 0000000..4ac3234 --- /dev/null +++ b/.github/workflow-samples/gradle-plugin/gradle/libs.versions.toml @@ -0,0 +1,2 @@ +# This file was generated by the Gradle 'init' task. +# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format diff --git a/.github/workflow-samples/gradle-plugin/plugin/build.gradle b/.github/workflow-samples/gradle-plugin/plugin/build.gradle deleted file mode 100644 index 7b32ecc..0000000 --- a/.github/workflow-samples/gradle-plugin/plugin/build.gradle +++ /dev/null @@ -1,60 +0,0 @@ -/* - * This file was generated by the Gradle 'init' task. - * - * This generated file contains a sample Gradle plugin project to get you started. - * For more details take a look at the Writing Custom Plugins chapter in the Gradle - * User Manual available at https://docs.gradle.org/7.3/userguide/custom_plugins.html - * This project uses @Incubating APIs which are subject to change. - */ - -plugins { - // Apply the Java Gradle plugin development plugin to add support for developing Gradle plugins - id 'java-gradle-plugin' -} - -repositories { - // Use Maven Central for resolving dependencies. - mavenCentral() -} - -testing { - suites { - // Configure the built-in test suite - test { - // Use JUnit Jupiter test framework - useJUnitJupiter('5.7.2') - } - - // Create a new test suite - functionalTest(JvmTestSuite) { - dependencies { - // functionalTest test suite depends on the production code in tests - implementation(project(':plugin')) - } - - targets { - all { - // This test suite should run after the built-in test suite has run its tests - testTask.configure { shouldRunAfter(test) } - } - } - } - } -} - -gradlePlugin { - // Define the plugin - plugins { - greeting { - id = 'org.example.gradle.plugin.greeting' - implementationClass = 'org.example.gradle.plugin.GradlePluginPlugin' - } - } -} - -gradlePlugin.testSourceSets(sourceSets.functionalTest) - -tasks.named('check') { - // Include functionalTest as part of the check lifecycle - dependsOn(testing.suites.functionalTest) -} diff --git a/.github/workflow-samples/gradle-plugin/plugin/build.gradle.kts b/.github/workflow-samples/gradle-plugin/plugin/build.gradle.kts new file mode 100644 index 0000000..157ba4b --- /dev/null +++ b/.github/workflow-samples/gradle-plugin/plugin/build.gradle.kts @@ -0,0 +1,40 @@ +plugins { + `java-gradle-plugin` +} + +repositories { + mavenCentral() +} + +testing { + suites { + val test by getting(JvmTestSuite::class) { + useJUnitJupiter() + } + + val functionalTest by registering(JvmTestSuite::class) { + dependencies { + implementation(project()) + } + + targets { + all { + testTask.configure { shouldRunAfter(test) } + } + } + } + } +} + +gradlePlugin { + val greeting by plugins.creating { + id = "org.example.greeting" + implementationClass = "org.example.GradlePluginPlugin" + } +} + +gradlePlugin.testSourceSets.add(sourceSets["functionalTest"]) + +tasks.named("check") { + dependsOn(testing.suites.named("functionalTest")) +} diff --git a/.github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/gradle/plugin/GradlePluginPluginFunctionalTest.java b/.github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/GradlePluginPluginFunctionalTest.java similarity index 77% rename from .github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/gradle/plugin/GradlePluginPluginFunctionalTest.java rename to .github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/GradlePluginPluginFunctionalTest.java index 7f17460..e179762 100644 --- a/.github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/gradle/plugin/GradlePluginPluginFunctionalTest.java +++ b/.github/workflow-samples/gradle-plugin/plugin/src/functionalTest/java/org/example/GradlePluginPluginFunctionalTest.java @@ -1,7 +1,7 @@ /* - * This Java source file was generated by the Gradle 'init' task. + * This source file was generated by the Gradle 'init' task */ -package org.example.gradle.plugin; +package org.example; import java.io.File; import java.io.IOException; @@ -15,7 +15,7 @@ import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.*; /** - * A simple functional test for the 'org.example.gradle.plugin.greeting' plugin. + * A simple functional test for the 'org.example.greeting' plugin. */ class GradlePluginPluginFunctionalTest { @TempDir @@ -29,24 +29,23 @@ class GradlePluginPluginFunctionalTest { return new File(projectDir, "settings.gradle"); } - @Test void canRunTaskWithGradle691() throws IOException { + @Test void canRunTask() throws IOException { writeString(getSettingsFile(), ""); writeString(getBuildFile(), "plugins {" + - " id('org.example.gradle.plugin.greeting')" + + " id('org.example.greeting')" + "}"); // Run the build GradleRunner runner = GradleRunner.create(); runner.forwardOutput(); - runner.withGradleVersion("6.9.1"); runner.withPluginClasspath(); runner.withArguments("greeting"); runner.withProjectDir(projectDir); BuildResult result = runner.build(); // Verify the result - assertTrue(result.getOutput().contains("Hello from plugin 'org.example.gradle.plugin.greeting'")); + assertTrue(result.getOutput().contains("Hello from plugin 'org.example.greeting'")); } private void writeString(File file, String string) throws IOException { diff --git a/.github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/gradle/plugin/GradlePluginPlugin.java b/.github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/GradlePluginPlugin.java similarity index 72% rename from .github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/gradle/plugin/GradlePluginPlugin.java rename to .github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/GradlePluginPlugin.java index 80dfc76..8c11f24 100644 --- a/.github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/gradle/plugin/GradlePluginPlugin.java +++ b/.github/workflow-samples/gradle-plugin/plugin/src/main/java/org/example/GradlePluginPlugin.java @@ -1,7 +1,7 @@ /* - * This Java source file was generated by the Gradle 'init' task. + * This source file was generated by the Gradle 'init' task */ -package org.example.gradle.plugin; +package org.example; import org.gradle.api.Project; import org.gradle.api.Plugin; @@ -13,7 +13,7 @@ public class GradlePluginPlugin implements Plugin { public void apply(Project project) { // Register a task project.getTasks().register("greeting", task -> { - task.doLast(s -> System.out.println("Hello from plugin 'org.example.gradle.plugin.greeting'")); + task.doLast(s -> System.out.println("Hello from plugin 'org.example.greeting'")); }); } } diff --git a/.github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/gradle/plugin/GradlePluginPluginTest.java b/.github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/GradlePluginPluginTest.java similarity index 65% rename from .github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/gradle/plugin/GradlePluginPluginTest.java rename to .github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/GradlePluginPluginTest.java index c2ee169..7b5fd4a 100644 --- a/.github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/gradle/plugin/GradlePluginPluginTest.java +++ b/.github/workflow-samples/gradle-plugin/plugin/src/test/java/org/example/GradlePluginPluginTest.java @@ -1,7 +1,7 @@ /* - * This Java source file was generated by the Gradle 'init' task. + * This source file was generated by the Gradle 'init' task */ -package org.example.gradle.plugin; +package org.example; import org.gradle.testfixtures.ProjectBuilder; import org.gradle.api.Project; @@ -9,13 +9,13 @@ import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; /** - * A simple unit test for the 'org.example.gradle.plugin.greeting' plugin. + * A simple unit test for the 'org.example.greeting' plugin. */ class GradlePluginPluginTest { @Test void pluginRegistersATask() { // Create a test project and apply the plugin Project project = ProjectBuilder.builder().build(); - project.getPlugins().apply("org.example.gradle.plugin.greeting"); + project.getPlugins().apply("org.example.greeting"); // Verify the result assertNotNull(project.getTasks().findByName("greeting")); diff --git a/.github/workflow-samples/gradle-plugin/settings.gradle b/.github/workflow-samples/gradle-plugin/settings.gradle deleted file mode 100644 index de78579..0000000 --- a/.github/workflow-samples/gradle-plugin/settings.gradle +++ /dev/null @@ -1,12 +0,0 @@ -/* - * This file was generated by the Gradle 'init' task. - * - * The settings file is used to specify which projects to include in your build. - * - * Detailed information about configuring a multi-project build in Gradle can be found - * in the user manual at https://docs.gradle.org/7.3/userguide/multi_project_builds.html - * This project uses @Incubating APIs which are subject to change. - */ - -rootProject.name = 'gradle-plugin' -include('plugin') diff --git a/.github/workflow-samples/gradle-plugin/settings.gradle.kts b/.github/workflow-samples/gradle-plugin/settings.gradle.kts new file mode 100644 index 0000000..56b9465 --- /dev/null +++ b/.github/workflow-samples/gradle-plugin/settings.gradle.kts @@ -0,0 +1,2 @@ +rootProject.name = "gradle-plugin-2" +include("plugin")