1. 程式人生 > >gradle 自定義插件

gradle 自定義插件

圖片 file tree 編譯 man users dual prop rep

gradle自定義規則有:
目錄規則樹 默認是src-> main
resources就只用這樣
properties裏面寫類名:
格式: implementation-class=com.allen.rujews.plugins.allen

liuhailongdeMacBook-Air:allen liuhailong$ tree
.
├── build.gradle
└── src
    └── main
        ├── groovy
        │?? └── com
        │??     └── allen
        │??         └── rujews
        │??             └── plugins
        │??                 └── allen.groovy
        └── resources
            └── META-INF
                └── gradle-plugins
                    └── com.github.rujews.plugins.allen.properties

10 directories, 3 files
liuhailongdeMacBook-Air:allen liuhailong$ cat /Users/liuhailong/Desktop/testdradle/allen/src/main/resources/META-INF/gradle-plugins/com.github.rujews.plugins.allen.properties 
implementation-class=com.allen.rujews.plugins.allen
liuhailongdeMacBook-Air:allen liuhailong$ 

寫插件語言 groovy (不是必須)
必須實現 apply接口
//allen.groovy

package com.allen.rujews.plugins

import org.gradle.api.Plugin
import org.gradle.api.Project

class allen implements Plugin<Project>{

    @Override
    void apply(Project target) {
        target.task(‘allen‘) << {
            println "fuck必須實現apply 也就是 apply plugin:時會調用"
        }
    }
}

//build.gradle

apply plugin: ‘maven‘
apply plugin: ‘groovy‘
dependencies {
//指定編譯插件需要的依賴
implementation gradleApi()
implementation localGroovy()
}
//生成插件的task
uploadArchives {
repositories {
mavenDeployer {
        //我們發布到本地的地址,可以發布到jcenter上,具體可google下相關教程
        repository(url: uri(‘./allen‘))
        }
    }
}

運行命令:gradle uploadArchives

liuhailongdeMacBook-Air:allen liuhailong$ gradle uploadArchives

BUILD SUCCESSFUL in 2s
4 actionable tasks: 4 executed
liuhailongdeMacBook-Air:allen liuhailong$ 

技術分享圖片
技術分享圖片
使用自定義的插件

//編譯插件
/*
    apply plugin: ‘maven‘
    apply plugin: ‘groovy‘
    dependencies {
        //指定編譯插件需要的依賴
        implementation gradleApi()
        implementation localGroovy()
    }
    //生成插件的task
    uploadArchives {
        repositories {
            mavenDeployer {
                    //我們發布到本地的地址,可以發布到jcenter上,具體可google下相關教程
                    repository(url: uri(‘./allen‘))
                    }
         }
    } 
*/
//使用插件
buildscript {
    dependencies {
    classpath files(‘build/libs/allen.jar‘)
    }
}
apply plugin: ‘groovy‘
apply plugin: ‘com.allen.rujews.plugins.allen‘

dependencies {
    compile gradleApi()
    compile localGroovy()
}

命令:gradle allen

liuhailongdeMacBook-Air:allen liuhailong$ gradle allen

> Task :allen
fuck必須實現apply 也就是 apply plugin:時會調用

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use ‘--warning-mode all‘ to show the individual deprecation warnings.
See https://docs.gradle.org/4.10/userguide/command_line_interface.html#sec:command_line_warnings

BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
liuhailongdeMacBook-Air:allen liuhailong$ cat build.gradle
//編譯插件
/*
    apply plugin: ‘maven‘
    apply plugin: ‘groovy‘
    dependencies {
        //指定編譯插件需要的依賴
        implementation gradleApi()
        implementation localGroovy()
    }
    //生成插件的task
    uploadArchives {
        repositories {
            mavenDeployer {
                    //我們發布到本地的地址,可以發布到jcenter上,具體可google下相關教程
                    repository(url: uri(‘./allen‘))
                    }
         }
    } //編譯插件
*/
//使用插件
buildscript {
    dependencies {
    classpath files(‘build/libs/allen.jar‘)
    }
}
apply plugin: ‘groovy‘
apply plugin: ‘com.allen.rujews.plugins.allen‘

dependencies {
    compile gradleApi()
    compile localGroovy()
}

liuhailongdeMacBook-Air:allen liuhailong$ 

技術分享圖片

gradle 自定義插件