Session 8
Ingmar Steiner
2017-06-28
We can write a custom plugin inline in the build script
build.gradle
class CountdownPlugin implements Plugin<Project> {
void apply(Project project) {
project.task("countdown") {
doLast {
println "Executing $name"
}
}
}
}
apply plugin: CountdownPlugin
buildSrc
subprojectPlugin code can also be provided in the buildSrc
buildSrc/src/main/groovy/CountdownPlugin.groovy
import org.gradle.api.*
class CountdownPlugin implements Plugin<Project> {
void apply(Project project) {
project.task("countdown") {
doLast {
println "Executing $name"
}
}
}
}
build.gradle
apply plugin: CountdownPlugin
countdown-plugin/settings.gradle
rootProject.name = 'countdown-plugin'
countdown-plugin/build.gradle
plugins {
id 'java-gradle-plugin'
id 'groovy'
}
gradlePlugin {
plugins {
countdownPlugin {
id = 'countdown'
implementationClass = 'CountdownPlugin'
}
}
}
countdown-plugin/src/main/groovy/CountdownPlugin.groovy
(see above)
countdown-project/build.gradle
buildscript {
dependencies {
classpath name: 'countdown-plugin'
}
}
apply plugin: 'countdown'
Composite build:
plugins
DSL in consumer projectpluginRepositories
block in settings.gradle
)Automatic software testing frameworks exist for
etc.
src/main/java/Foo.java
public class Foo {
public static String foo() {
return "foo";
}
}
src/test/java/FooTest.java
import org.testng.*;
import org.testng.annotations.*;
public class FooTest {
@Test
public void testFoo() {
String expected = "foo";
String actual = Foo.foo();
Assert.assertEquals(expected, actual);
}
@Test
public void testBar() {
Assert.assertTrue(false);
}
}
build.gradle
apply plugin: 'java'
repositories.jcenter()
dependencies.testCompile 'org.testng:testng:6.9.6'
test.useTestNG()
src/main/groovy/CountdownPlugin.groovy
import org.gradle.api.*
class CountdownPlugin implements Plugin<Project> {
void apply(Project project) {
project.task("countdown", type: CountdownTask)
}
}
src/main/groovy/CountdownTask.groovy
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*
class CountdownTask extends DefaultTask {
@Input
int from = 3
@TaskAction
void countdown() {
(from..1).each {
print "$it... "
}
println "BLAST-OFF!!!"
}
}
src/test/groovy/CountdownPluginTest.groovy
import org.gradle.testkit.runner.GradleRunner
import org.testng.annotations.*
class CountdownPluginTest {
@Test
void testCountdown() {
def tmpDir = File.createTempDir()
new File("$tmpDir/build.gradle").withWriter {
it.println 'plugins {'
it.println " id 'countdown'"
it.println '}'
}
def gradle = GradleRunner.create().withProjectDir(tmpDir).withPluginClasspath()
def result = gradle.withArguments('countdown').build()
assert result.output.contains('3... 2... 1... BLAST-OFF!!!')
}
@Test
void testCountdownWithCustomFrom() {
def tmpDir = File.createTempDir()
new File("$tmpDir/build.gradle").withWriter {
it.println 'plugins {'
it.println " id 'countdown'"
it.println '}'
it.println 'countdown {'
it.println ' from = 5'
it.println '}'
}
def gradle = GradleRunner.create().withProjectDir(tmpDir).withPluginClasspath()
def result = gradle.withArguments('countdown').build()
assert result.output.contains('5... 4... 3... 2... 1... BLAST-OFF!!!')
}
}
**/*.wav
**/*.lab
txt.done.data