creating gradle plugins
Post on 14-Apr-2017
681 Views
Preview:
TRANSCRIPT
Creating Gradle Plugins
Annyce Davis - @brwngrldev
@brwngrldev
Without Plugins
@brwngrldev
With Plugins
Overview• Plugin Skeleton
• Dependencies
• Plugin.groovy
• CustomTask.groovy
• Publishing
@brwngrldev
Plugin Skeleton
@brwngrldev
Plugin Skeleton
Plugin SkeletonHow Gradle finds the Plugin Implementation
Dependencies
@brwngrldev
apply plugin: ‘groovy'
dependencies { compile gradleApi() compile localGroovy() }
Plugin.groovyImplement the Plugin interface and override apply method
class CustomPlugin implements Plugin<Project> {
@Override void apply(Project project) {
} }
QualityChecksPlugin.groovyImplement the Plugin interface and override apply method
Apply Method
@brwngrldev
Creates the tasks for the plugin and performs any setup
Project Extension
@brwngrldev
Allows the user to customize the behavior of the plugin
Project Extension
@brwngrldev
Back in the application’s build.gradle file…
Creating tasks
@brwngrldev
Give it a name and a type
@brwngrldev
CustomTask.groovy
@brwngrldev
class CustomTask extends DefaultTask {
}
CustomTask.groovy
@brwngrldev
class CustomTask extends DefaultTask {
@TaskAction def defaultAction() {
} }
CustomTask.groovy
@brwngrldev
class CustomTask extends DefaultTask {
@TaskAction def defaultAction() {
< do your cool stuff >
} }
So far…• Plugin Skeleton
• Dependencies
• Plugin.groovy
• CustomTask.groovy
@brwngrldev
Questions?
Publishing
@brwngrldev
Publishingbuildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } } dependencies { classpath "com.gradle.publish:plugin-publish-plugin:0.9.2" } }
apply plugin: 'com.gradle.plugin-publish'
version = "0.1.3" group = "info.adavis"
version = "0.1.3" group = "info.adavis"
pluginBundle { website = 'https://github.com/adavis/quality-checks' vcsUrl = 'https://github.com/adavis/quality-checks.git' description = 'Gradle Plugin for…’ tags = ['Checkstyle', 'FindBugs', 'PMD']
}
version = "0.1.3" group = "info.adavis"
pluginBundle { website = 'https://github.com/adavis/quality-checks' vcsUrl = 'https://github.com/adavis/quality-checks.git' description = 'Gradle Plugin for…’ tags = ['Checkstyle', 'FindBugs', 'PMD']
plugins { qualityChecksPlugin { id = 'info.adavis.qualitychecks' displayName = 'Quality Checks Plugin' } } }
Publishing
@brwngrldev
Once your Plugin is published on the Gradle portal
We’re done…
@brwngrldev
WRONG!
We’re done…
@brwngrldev
TESTS
Testing
@brwngrldev
You can use JUnit and the ProjectBuilder for most testing
Testing
@brwngrldev
@Test void shouldBeAbleToCreateTask() { assertTrue(task instanceof WriteConfigFileTask) }
Testing
@brwngrldev
@Test void shouldBeAbleToCreateTask() { assertTrue(task instanceof WriteConfigFileTask) }
@Test void pluginShouldBeApplied() { def project = ProjectBuilder.builder().build()
project.apply(plugin: QualityChecksPlugin)
assertNotNull(project.tasks.findByName(‘mytask’)) }
Bonus: README
@brwngrldev
Bonus: README
@brwngrldev
Summary• Helps avoid copy/paste horror
• Simple project structure
• Extending DefaultTask
• Testing techniques
• Easy to publish
@brwngrldev
Next Steps✓Sample: https://git.io/vzJ2w
✓Sample: https://git.io/vzJwS
✓Applaud 😉
@brwngrldev
top related