mirror of
https://github.com/ysoftdevs/gradle-training.git
synced 2026-03-31 14:13:35 +02:00
Add example with custom task in one and separate files
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
.gradle
|
.gradle
|
||||||
|
.idea
|
||||||
build/
|
build/
|
||||||
|
|||||||
18
21-custom-task-in-one-file/build.gradle
Normal file
18
21-custom-task-in-one-file/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
// Based on example: https://gradle.org/docs/current/userguide/custom_tasks.html
|
||||||
|
|
||||||
|
// Use the default greeting
|
||||||
|
task hello(type: GreetingTask)
|
||||||
|
|
||||||
|
// Customize the greeting
|
||||||
|
task greeting(type: GreetingTask) {
|
||||||
|
greeting = 'greetings from Brno'
|
||||||
|
}
|
||||||
|
|
||||||
|
class GreetingTask extends DefaultTask {
|
||||||
|
String greeting = 'hello from Y Soft'
|
||||||
|
|
||||||
|
@TaskAction
|
||||||
|
def greet() {
|
||||||
|
println greeting
|
||||||
|
}
|
||||||
|
}
|
||||||
10
22-custom-task-in-buildSrc/build.gradle
Normal file
10
22-custom-task-in-buildSrc/build.gradle
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
apply plugin: 'groovy'
|
||||||
|
|
||||||
|
import com.ysoft.greeting.GreetingTask
|
||||||
|
|
||||||
|
task hello(type: GreetingTask)
|
||||||
|
|
||||||
|
// Customize the greeting
|
||||||
|
task greeting(type: GreetingTask) {
|
||||||
|
greeting = 'greetings from Brno'
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.ysoft.greeting
|
||||||
|
|
||||||
|
import org.gradle.api.DefaultTask
|
||||||
|
import org.gradle.api.tasks.TaskAction
|
||||||
|
|
||||||
|
class GreetingTask extends DefaultTask {
|
||||||
|
String greeting = 'hello from Y Soft'
|
||||||
|
|
||||||
|
@TaskAction
|
||||||
|
def greet() {
|
||||||
|
println greeting
|
||||||
|
}
|
||||||
|
}
|
||||||
26
README.md
26
README.md
@@ -227,3 +227,29 @@ Result is available at: http://localhost:8080/20-grails
|
|||||||
|
|
||||||
More information: https://github.com/grails/grails-gradle-plugin
|
More information: https://github.com/grails/grails-gradle-plugin
|
||||||
Video recording from Building Grails App With Gradle: http://youtu.be/FwZvDU2Jeh8
|
Video recording from Building Grails App With Gradle: http://youtu.be/FwZvDU2Jeh8
|
||||||
|
|
||||||
|
|
||||||
|
## 21-custom-task-in-one-file
|
||||||
|
|
||||||
|
Gradle has support for defining custom task classes.
|
||||||
|
Task class could be extended e.g. from DefaultTask.
|
||||||
|
|
||||||
|
You can verify this sample by:
|
||||||
|
|
||||||
|
gradle hello
|
||||||
|
gradle greeting
|
||||||
|
|
||||||
|
## 22-custom-task-in-buildSrc
|
||||||
|
|
||||||
|
In previous example we discussed how to add custom task. Storing many
|
||||||
|
custom tasks in one file makes it unreadable. It's possible to split logic into
|
||||||
|
Groovy classes.
|
||||||
|
|
||||||
|
Create buildSrc directory with src/main/groovy.
|
||||||
|
Store here the class. It's necessary to add package name declaration and imports
|
||||||
|
from GradleAPI.
|
||||||
|
|
||||||
|
Functionality is the same:
|
||||||
|
|
||||||
|
gradle hello
|
||||||
|
gradle greeting
|
||||||
Reference in New Issue
Block a user