diff --git a/14-custom-groovy-plugin/build.gradle b/14-custom-groovy-plugin/build.gradle new file mode 100644 index 0000000..3be752e --- /dev/null +++ b/14-custom-groovy-plugin/build.gradle @@ -0,0 +1,12 @@ +apply plugin: "groovy" +apply plugin: "maven" + +archivesBaseName = "lines-count" +version = "1.0" +group "com.ysoft.training" + +dependencies { + compile localGroovy() + compile gradleApi() +} + diff --git a/14-custom-groovy-plugin/src/main/groovy/com/ysoft/training/LineCountPlugin.groovy b/14-custom-groovy-plugin/src/main/groovy/com/ysoft/training/LineCountPlugin.groovy new file mode 100644 index 0000000..31f98d9 --- /dev/null +++ b/14-custom-groovy-plugin/src/main/groovy/com/ysoft/training/LineCountPlugin.groovy @@ -0,0 +1,22 @@ +package com.ysoft.training + +import org.gradle.api.Plugin +import org.gradle.api.Project +import org.gradle.api.Task + +/** + * Simple plugin for counting lines in build.gradle. + **/ +class LineCountPlugin implements Plugin { + + void apply(Project project) { + Task countLineTask = project.tasks.add("countLines") + countLineTask.doLast { + def counter = 0 + new File("build.gradle").eachLine { line -> + counter += 1 + } + println "Total lines: ${counter}" + } + } +} diff --git a/14-custom-groovy-plugin/src/main/resources/META-INF/gradle-plugins/lines-count.properties b/14-custom-groovy-plugin/src/main/resources/META-INF/gradle-plugins/lines-count.properties new file mode 100644 index 0000000..ba6f158 --- /dev/null +++ b/14-custom-groovy-plugin/src/main/resources/META-INF/gradle-plugins/lines-count.properties @@ -0,0 +1 @@ +implementation-class=com.ysoft.training.LineCountPlugin diff --git a/15-use-custom-plugin/build.gradle b/15-use-custom-plugin/build.gradle new file mode 100644 index 0000000..e71afdf --- /dev/null +++ b/15-use-custom-plugin/build.gradle @@ -0,0 +1,11 @@ +buildscript { + repositories { + mavenLocal() + } + dependencies { + classpath 'com.ysoft.training:lines-count:1.0' + } +} + +apply plugin: 'lines-count' + diff --git a/README.md b/README.md index 3716496..5415d6f 100644 --- a/README.md +++ b/README.md @@ -139,3 +139,27 @@ In this example plugin is stored as gist at Github gradle tasks gradle helloFromGist + + +## 14-custom-groovy-plugin + +Example of stand alone plugin written in Groovy. Compile and install this +plugin to your local Maven repository. Then you will be able to use it in +the next example. + + gradle install + +Pay attention to special file: src/main/resources/META-INF/gradle-pluginslines-count.properties +This file will cause that it will be possible to apply plugin by it's id instead +of java class name. It's preferred solution in Gradle world, because it gives +you more flexibility when changing plugin implementation + + +## 15-use-custom-plugin + +You need to install plugin to your local Maven storage (see example 14.). +This example shows how to include plugin from local Maven storage. +Plugin is just counting lines in build.gradle file in current directory. + + gradle tasks + gradle countLines