add custom groovy plugin and its usage from local Maven storage

This commit is contained in:
Juraj Michalek
2014-05-11 15:13:51 +02:00
parent 7b418f5d7d
commit fe7b073aa8
5 changed files with 70 additions and 0 deletions

View File

@@ -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()
}

View File

@@ -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<Project> {
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}"
}
}
}

View File

@@ -0,0 +1 @@
implementation-class=com.ysoft.training.LineCountPlugin

View File

@@ -0,0 +1,11 @@
buildscript {
repositories {
mavenLocal()
}
dependencies {
classpath 'com.ysoft.training:lines-count:1.0'
}
}
apply plugin: 'lines-count'

View File

@@ -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