zoukankan      html  css  js  c++  java
  • 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

    友情链接

  • 相关阅读:
    YOLO V5 is Here! Custom Object Detection Tutorial with YOLO V5
    pjsip application notes
    Tilt Angle Visualization With Edison, Accelerometer and Python
    WebRTC 镜像源
    Get Started with WebRTC
    sqlserver关于发布订阅replication_subscription的总结
    在Flask中,g是什么?它的生命周期是?能做什么?
    46.全排列问题
    【LeetCode】代码模板,刷题必会
    QEMU+KVM学习笔记
  • 原文地址:https://www.cnblogs.com/huhx/p/baseusejavacustomgradleplugin.html
Copyright © 2011-2022 走看看