全屏浏览
缩小浏览
回到页首

java基础---->自定义gradle的插件

  这里面简单的介绍一下gradle插件的编写。

 

自定义gradle插件

我们编写的gradle脚本一般是放在build.gradle文件中。所以首先创建一下build.gradle文件,下面的例子都是在此文件中。

一、简单的gradle插件

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        project.task('hello') {
            doLast {
                println 'Hello from the GreetingPlugin'
            }
        }
    }
}
apply plugin: GreetingPlugin

运行结果:

 

二、 编写可配置的插件

class GreetingPluginExtension {
    String message
    String greeter
}

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('greeting', GreetingPluginExtension)
        project.task('hello') {
            doLast {
                println "${extension.message} from ${extension.greeter}"
            }
        }
    }
}
apply plugin: GreetingPlugin

greeting {
    message = 'Hello'
    greeter = 'huhx'
}

 运行结果:

 

三、编写与文件交互的插件

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"

运行结果:并且在2018-12-28/build目录下面生成一个hello.txt,里面的内容是Hello。

 

四、编写带有拓展功能的插件

class GreetingPlugin implements Plugin<Project> {
    void apply(Project project) {
        def extension = project.extensions.create('greeting', GreetingPluginExtension, project)
        project.tasks.create('hello', Greeting) {
            message = extension.message
            outputFiles = extension.outputFiles
        }
    }
}

class GreetingPluginExtension {
    final Property<String> message
    final ConfigurableFileCollection outputFiles

    GreetingPluginExtension(Project project) {
        message = project.objects.property(String)
        message.set('Hello from GreetingPlugin')
        outputFiles = project.layout.configurableFiles()
    }

    void setOutputFiles(FileCollection outputFiles) {
        this.outputFiles.setFrom(outputFiles)
    }
}

class Greeting extends DefaultTask {
    final Property<String> message = project.objects.property(String)
    final ConfigurableFileCollection outputFiles = project.layout.configurableFiles()

    void setOutputFiles(FileCollection outputFiles) {
        this.outputFiles.setFrom(outputFiles)
    }

    @TaskAction
    void printMessage() {
        outputFiles.each {
            logger.quiet "Writing message 'Hi from Gradle' to file"
            it.text = message.get()
        }
    }
}

apply plugin: GreetingPlugin

greeting {
    message = 'Hi from Gradle'
    outputFiles = layout.files('a.txt', 'b.txt')
} 

运行结果:并且在2018-12-28目录下面生成a.txt和b.txt,文件的内容均为:Hi from Gradle

 

友情链接

 

posted @ 2019-01-08 22:28  huhx  阅读(491)  评论(0编辑  收藏