我有一堆像这样的 gradle 脚本插件https://github.com/docToolchain/docToolchain/blob/master/scripts/exportExcel.gradle https://github.com/docToolchain/docToolchain/blob/master/scripts/exportExcel.gradle并希望将它们变成“真正的”二进制插件,以便轻松分发它们。
我知道我可以通过 http 构建来引用脚本插件,但这并不像真正的插件那么好。
正如我所看到的,编写插件的一种方法是实现org.gradle.api.Plugin
界面。这基本上意味着我必须重写所有 mit 脚本插件。
有没有其他方法可以轻松地将它们变成二进制插件?也许某种包装?
您的脚本似乎可以轻松转换为插件。
你为什么说你必须重写你的脚本?据我所知,这基本上是更改一些小细节并添加所有样板/元数据的问题。
我写了很多插件,这里是一个非常简单的插件:https://github.com/renatoahaydes/pony-gradle-plugin https://github.com/renatoathaydes/pony-gradle-plugin
这是您需要更改才能制作真正的插件的内容:
- 为您最喜欢的 JVM 语言创建一个普通的 Gradle 项目。
- In the
buildScript
块,添加类路径依赖com.gradle.publish:plugin-publish-plugin:0.9.10
并应用com.gradle.plugin-publish
插入。Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/build.gradle#L10.
- 添加编译依赖
gradleApi()
. Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/build.gradle#L31.
- Add a
pluginBundle
配置到构建文件。Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/build.gradle#L91.
- 创建一个类来实现
org.gradle.api.Plugin<org.gradle.api.Project>
界面。Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/src/main/groovy/com/athaydes/gradle/pony/PonyPlugin.groovy.
- 在里面配置插件
apply
方法(它给你一个句柄Project
)。这包括将您可能需要的任何任务添加到项目中。
- 创建一个类扩展
org.gradle.api.DefaultTask
(通常)针对每个任务。Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/src/main/groovy/com/athaydes/gradle/pony/PonyPlugin.groovy#L62.
- 在某个方法中实现任务的逻辑,用注释来注释该方法
@org.gradle.api.tasks.TaskAction
. Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/src/main/groovy/com/athaydes/gradle/pony/PonyPlugin.groovy#L93.
- 创建一个描述符
META-INF/gradle-plugins/plugin-name.properties
带有类似的条目implementation-class=your.plugin.Class
. Example https://github.com/renatoathaydes/pony-gradle-plugin/blob/master/src/main/resources/META-INF/gradle-plugins/com.athaydes.pony.properties.
基本上就是这样!
您可能想让插件可配置,Gradle 文档 https://docs.gradle.org/current/userguide/custom_plugins.html#sec:getting_input_from_the_build展示如何做到这一点,但为了方便起见,这里是他们当前的示例:
class GreetingPluginExtension {
String message = 'Hello from GreetingPlugin'
}
class GreetingPlugin implements Plugin<Project> {
void apply(Project project) {
// Add the 'greeting' extension object
def extension = project.extensions.create('greeting', GreetingPluginExtension)
// Add a task that uses configuration from the extension object
project.task('hello') {
doLast {
println extension.message
}
}
}
}
然后可以在用户的构建文件中使用它,如下所示:
greeting {
message = 'Hi'
}
要在 Gradle 门户中实际发布插件,您需要创建一个帐户...see this https://plugins.gradle.org/docs/submit更多细节。 (或者您可以将其发布到 Bintray 上,但是对于您的用户来说应用您的插件会有点尴尬 - 另一方面 Bintray 作为存储库要好得多,所以我最终两者都做了,请参阅上面的示例)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)