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$
原文地址:http://blog.51cto.com/haidragon/2174472
时间: 2024-10-13 02:13:22