Update gradle-plugin sample to use Kotlin DSL

This commit is contained in:
daz 2025-03-25 15:19:23 -06:00
parent 086c9e4b25
commit aa88309fbd
No known key found for this signature in database
8 changed files with 57 additions and 86 deletions

View File

@ -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

View File

@ -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)
}

View File

@ -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<Task>("check") {
dependsOn(testing.suites.named("functionalTest"))
}

View File

@ -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 {

View File

@ -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<Project> {
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'"));
});
}
}

View File

@ -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"));

View File

@ -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')

View File

@ -0,0 +1,2 @@
rootProject.name = "gradle-plugin-2"
include("plugin")