Skip to content

Commit aa88309

Browse files
committed
Update gradle-plugin sample to use Kotlin DSL
1 parent 086c9e4 commit aa88309

File tree

8 files changed

+57
-86
lines changed

8 files changed

+57
-86
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This file was generated by the Gradle 'init' task.
2+
# https://docs.gradle.org/current/userguide/platforms.html#sub::toml-dependencies-format

.github/workflow-samples/gradle-plugin/plugin/build.gradle

-60
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
plugins {
2+
`java-gradle-plugin`
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
testing {
10+
suites {
11+
val test by getting(JvmTestSuite::class) {
12+
useJUnitJupiter()
13+
}
14+
15+
val functionalTest by registering(JvmTestSuite::class) {
16+
dependencies {
17+
implementation(project())
18+
}
19+
20+
targets {
21+
all {
22+
testTask.configure { shouldRunAfter(test) }
23+
}
24+
}
25+
}
26+
}
27+
}
28+
29+
gradlePlugin {
30+
val greeting by plugins.creating {
31+
id = "org.example.greeting"
32+
implementationClass = "org.example.GradlePluginPlugin"
33+
}
34+
}
35+
36+
gradlePlugin.testSourceSets.add(sourceSets["functionalTest"])
37+
38+
tasks.named<Task>("check") {
39+
dependsOn(testing.suites.named("functionalTest"))
40+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* This Java source file was generated by the Gradle 'init' task.
2+
* This source file was generated by the Gradle 'init' task
33
*/
4-
package org.example.gradle.plugin;
4+
package org.example;
55

66
import java.io.File;
77
import java.io.IOException;
@@ -15,7 +15,7 @@
1515
import static org.junit.jupiter.api.Assertions.*;
1616

1717
/**
18-
* A simple functional test for the 'org.example.gradle.plugin.greeting' plugin.
18+
* A simple functional test for the 'org.example.greeting' plugin.
1919
*/
2020
class GradlePluginPluginFunctionalTest {
2121
@TempDir
@@ -29,24 +29,23 @@ private File getSettingsFile() {
2929
return new File(projectDir, "settings.gradle");
3030
}
3131

32-
@Test void canRunTaskWithGradle691() throws IOException {
32+
@Test void canRunTask() throws IOException {
3333
writeString(getSettingsFile(), "");
3434
writeString(getBuildFile(),
3535
"plugins {" +
36-
" id('org.example.gradle.plugin.greeting')" +
36+
" id('org.example.greeting')" +
3737
"}");
3838

3939
// Run the build
4040
GradleRunner runner = GradleRunner.create();
4141
runner.forwardOutput();
42-
runner.withGradleVersion("6.9.1");
4342
runner.withPluginClasspath();
4443
runner.withArguments("greeting");
4544
runner.withProjectDir(projectDir);
4645
BuildResult result = runner.build();
4746

4847
// Verify the result
49-
assertTrue(result.getOutput().contains("Hello from plugin 'org.example.gradle.plugin.greeting'"));
48+
assertTrue(result.getOutput().contains("Hello from plugin 'org.example.greeting'"));
5049
}
5150

5251
private void writeString(File file, String string) throws IOException {
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* This Java source file was generated by the Gradle 'init' task.
2+
* This source file was generated by the Gradle 'init' task
33
*/
4-
package org.example.gradle.plugin;
4+
package org.example;
55

66
import org.gradle.api.Project;
77
import org.gradle.api.Plugin;
@@ -13,7 +13,7 @@ public class GradlePluginPlugin implements Plugin<Project> {
1313
public void apply(Project project) {
1414
// Register a task
1515
project.getTasks().register("greeting", task -> {
16-
task.doLast(s -> System.out.println("Hello from plugin 'org.example.gradle.plugin.greeting'"));
16+
task.doLast(s -> System.out.println("Hello from plugin 'org.example.greeting'"));
1717
});
1818
}
1919
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
/*
2-
* This Java source file was generated by the Gradle 'init' task.
2+
* This source file was generated by the Gradle 'init' task
33
*/
4-
package org.example.gradle.plugin;
4+
package org.example;
55

66
import org.gradle.testfixtures.ProjectBuilder;
77
import org.gradle.api.Project;
88
import org.junit.jupiter.api.Test;
99
import static org.junit.jupiter.api.Assertions.*;
1010

1111
/**
12-
* A simple unit test for the 'org.example.gradle.plugin.greeting' plugin.
12+
* A simple unit test for the 'org.example.greeting' plugin.
1313
*/
1414
class GradlePluginPluginTest {
1515
@Test void pluginRegistersATask() {
1616
// Create a test project and apply the plugin
1717
Project project = ProjectBuilder.builder().build();
18-
project.getPlugins().apply("org.example.gradle.plugin.greeting");
18+
project.getPlugins().apply("org.example.greeting");
1919

2020
// Verify the result
2121
assertNotNull(project.getTasks().findByName("greeting"));

.github/workflow-samples/gradle-plugin/settings.gradle

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
rootProject.name = "gradle-plugin-2"
2+
include("plugin")

0 commit comments

Comments
 (0)