Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
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 Skeleton
How Gradle finds the Plugin Implementation
Dependencies
@brwngrldev
apply plugin: ‘groovy'
dependencies {
compile gradleApi()
compile localGroovy()
}
Plugin.groovy
Implement the Plugin interface and override apply method
class CustomPlugin implements Plugin<Project> {
@Ov...
QualityChecksPlugin.groovy
Implement 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 stuf...
So far…
• Plugin Skeleton
• Dependencies
• Plugin.groovy
• CustomTask.groovy
@brwngrldev
Questions?
Publishing
@brwngrldev
Publishing
buildscript {
repositories {
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.gra...
version = "0.1.3"
group = "info.adavis"
version = "0.1.3"
group = "info.adavis"
pluginBundle {
website = 'https://github.com/adavis/quality-checks'
vcsUrl = 'http...
version = "0.1.3"
group = "info.adavis"
pluginBundle {
website = 'https://github.com/adavis/quality-checks'
vcsUrl = 'http...
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 pl...
Bonus: README
@brwngrldev
Bonus: README
@brwngrldev
Summary
• Helps avoid copy/paste horror
• Simple project structure
• Extending DefaultTask
• Testing techniques
• Easy to ...
Next Steps
✓Sample: https://git.io/vzJ2w
✓Sample: https://git.io/vzJwS
✓Applaud 😉
@brwngrldev
Thanks!
@brwngrldev
+AnnyceDavis
www.adavis.info
@brwngrldev
Upcoming SlideShare
Loading in …5
×

Creating Gradle Plugins

789 views

Published on

If you find yourself copying and pasting code from your application's build.gradle file every time you start a new project, then it's time you learn about creating Gradle plugins. In this presentation you will learn about what it takes to create a custom Gradle plugin for use in your Android applications.

It covers: Plugin Structure, Key Components, Testing, and Publishing

Published in: Technology
  • Be the first to comment

Creating Gradle Plugins

  1. 1. Creating Gradle Plugins Annyce Davis - @brwngrldev
  2. 2. @brwngrldev Without Plugins
  3. 3. @brwngrldev With Plugins
  4. 4. Overview • Plugin Skeleton • Dependencies • Plugin.groovy • CustomTask.groovy • Publishing @brwngrldev
  5. 5. Plugin Skeleton @brwngrldev
  6. 6. Plugin Skeleton
  7. 7. Plugin Skeleton How Gradle finds the Plugin Implementation
  8. 8. Dependencies @brwngrldev apply plugin: ‘groovy' dependencies { compile gradleApi() compile localGroovy() }
  9. 9. Plugin.groovy Implement the Plugin interface and override apply method class CustomPlugin implements Plugin<Project> { @Override void apply(Project project) { } }
  10. 10. QualityChecksPlugin.groovy Implement the Plugin interface and override apply method
  11. 11. Apply Method @brwngrldev Creates the tasks for the plugin and performs any setup
  12. 12. Project Extension @brwngrldev Allows the user to customize the behavior of the plugin
  13. 13. Project Extension @brwngrldev Back in the application’s build.gradle file…
  14. 14. Creating tasks @brwngrldev Give it a name and a type
  15. 15. @brwngrldev
  16. 16. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { }
  17. 17. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { @TaskAction def defaultAction() { } }
  18. 18. CustomTask.groovy @brwngrldev class CustomTask extends DefaultTask { @TaskAction def defaultAction() { < do your cool stuff > } }
  19. 19. So far… • Plugin Skeleton • Dependencies • Plugin.groovy • CustomTask.groovy @brwngrldev
  20. 20. Questions?
  21. 21. Publishing @brwngrldev
  22. 22. Publishing buildscript { 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'
  23. 23. version = "0.1.3" group = "info.adavis"
  24. 24. 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'] }
  25. 25. 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' } } }
  26. 26. Publishing @brwngrldev Once your Plugin is published on the Gradle portal
  27. 27. We’re done… @brwngrldev WRONG!
  28. 28. We’re done… @brwngrldev TESTS
  29. 29. Testing @brwngrldev You can use JUnit and the ProjectBuilder for most testing
  30. 30. Testing @brwngrldev @Test void shouldBeAbleToCreateTask() { assertTrue(task instanceof WriteConfigFileTask) }
  31. 31. 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’)) }
  32. 32. Bonus: README @brwngrldev
  33. 33. Bonus: README @brwngrldev
  34. 34. Summary • Helps avoid copy/paste horror • Simple project structure • Extending DefaultTask • Testing techniques • Easy to publish @brwngrldev
  35. 35. Next Steps ✓Sample: https://git.io/vzJ2w ✓Sample: https://git.io/vzJwS ✓Applaud 😉 @brwngrldev
  36. 36. Thanks! @brwngrldev +AnnyceDavis www.adavis.info @brwngrldev

×