引入grale插件
plugins {
id «plugin id» version «plugin version» [apply «false»]
}
限制插件的执行范围
plugins {
id "org.gradle.sample.hello" version "1.0.0" apply false
id "org.gradle.sample.goodbye" version "1.0.0" apply false //false表示在那个project上不要执行。
}
subprojects { subproject ->
if (subproject.name.startsWith("hello")) {
apply plugin: 'org.gradle.sample.hello'
}
if (subproject.name.startsWith("goodbye")) {
apply plugin: 'org.gradle.sample.goodbye'
}
}
发布
repositories {
flatDir {
name "fileRepo"
dirs "repo"
}
}
uploadArchives {
repositories {
add project.repositories.fileRepo
ivy {
credentials {
username "username"
password "pw"
}
url "http://repo.mycompany.com"
}
}
}
写一个普通的task类
- 继承DefaultTask
class GreetingTask extends DefaultTask {
}
这个类并不会有什么作用,所以要添加一些行为。我们要为方法加上@TaskAction注解,当gradle执行的时候将会调用这个方法。你不必要定义一个方法来来控制他的行为。当然,你也可以这么做,比如重写doFirst或者doLast方法。
task hello(type: GreetingTask)
class GreetingTask extends DefaultTask {
@TaskAction
def greet() {
println 'hello from GreetingTask'
}
}
结果:
> gradle -q hello
hello from GreetingTask
给task添加属性值
// Use the default greeting
task hello(type: GreetingTask)
// Customize the greeting
task greeting(type: GreetingTask) {
greeting = 'greetings from GreetingTask'
}
class GreetingTask extends DefaultTask {
String greeting = 'hello from GreetingTask'
@TaskAction
def greet() {
println greeting
}
}
结果:
> gradle -q hello greeting
hello from GreetingTask
greetings from GreetingTask
写一个简单的Plugin
apply plugin: GreetingPlugin
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.task('hello') {
doLast {
println "Hello from the GreetingPlugin"
}
}
}
}
结果:
> gradle -q hello
Hello from the GreetingPlugin
从gradle文件中获取参数
apply plugin: GreetingPlugin
greeting.message = 'Hi from Gradle'
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
project.extensions.create("greeting", GreetingPluginExtension)
// Add a task that uses the configuration
project.task('hello') {
doLast {
println project.greeting.message
}
}
}
}
class GreetingPluginExtension {
def String message = 'Hello from GreetingPlugin'
}
结果:
> gradle -q hello
Hi from Gradle
用闭包来配置普通的plugin
apply plugin: GreetingPlugin
greeting {
message = 'Hi'
greeter = 'Gradle'
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
project.extensions.create("greeting", GreetingPluginExtension)
project.task('hello') {
doLast {
println "${project.greeting.message} from ${project.greeting.greeter}"
}
}
}
}
class GreetingPluginExtension {
String message
String greeter
}
结果:
> gradle -q hello
Hi from Gradle
task和plugin的文件操作
当开发一个通用的task或者plugin时,这是一个很好的想法来通过配置文件的位置创造一个灵活的工程。可以通过Project.file(java.lang.Object)方法来实现。
class GreetingToFileTask extends DefaultTask {
def destination
File getDestination() {
project.file(destination)
}
@TaskAction
def greet() {
def file = getDestination()
file.parentFile.mkdirs()
file.write "Hello!"
}
}
task greet(type: GreetingToFileTask) {
destination = { project.greetingFile }
}
task sayGreeting(dependsOn: greet) {
doLast {
println file(greetingFile).text
}
}
ext.greetingFile = "$buildDir/hello.txt"
结果:
> gradle -q sayGreeting
Hello!
映射额外的属性到task的属性
apply plugin: GreetingPlugin
greeting {
message = 'Hi from Gradle'
outputFiles = files('a.txt', 'b.txt')
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension, project)
// Add a task that uses the configuration
project.tasks.create('hello', Greeting) {
message = extension.messageProvider
outputFiles = extension.outputFiles
}
}
}
class GreetingPluginExtension {
final PropertyState<String> message
final ConfigurableFileCollection outputFiles
GreetingPluginExtension(Project project) {
message = project.property(String)
setMessage('Hello from GreetingPlugin')
outputFiles = project.files()
}
String getMessage() {
message.get()
}
Provider<String> getMessageProvider() {
message
}
void setMessage(String message) {
this.message.set(message)
}
FileCollection getOutputFiles() {
outputFiles
}
void setOutputFiles(FileCollection outputFiles) {
this.outputFiles.setFrom(outputFiles)
}
}
class Greeting extends DefaultTask {
final PropertyState<String> message = project.property(String)
final ConfigurableFileCollection outputFiles = project.files()
@Input
String getMessage() {
message.get()
}
void setMessage(String message) {
this.message.set(message)
}
void setMessage(Provider<String> message) {
this.message.set(message)
}
FileCollection getOutputFiles() {
outputFiles
}
void setOutputFiles(FileCollection outputFiles) {
this.outputFiles.setFrom(outputFiles)
}
@TaskAction
void printMessage() {
getOutputFiles().each {
logger.quiet "Writing message 'Hi from Gradle' to file"
it.text = getMessage()
}
}
}
结果:
> gradle -q hello
Writing message 'Hi from Gradle' to file
Writing message 'Hi from Gradle' to file
从当前的脚本文件中执行其他脚本
build.gradle
task build(type: GradleBuild) {
buildFile = 'other.gradle'
tasks = ['hello']
}
othre.gradle
task hello {
doLast {
println "hello from the other build."
}
}
结果:
> gradle -q build
hello from the other build.
改变project的位置
sourceSets {
main {
java {
srcDirs = ['src/java']
}
resources {
srcDirs = ['src/resources']
}
}
}
网友评论