
Gradle plugin
Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds.
Packaging a pluginH1
Build scriptH2
You can include the source for the plugin directly in the build script. This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything. However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.
buildSrc projectH2
You can put the source for the plugin in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.
See Chapter 60, Organizing Build Logic for more details about the buildSrc project.
Standalone projectH2
You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others. Generally, this JAR might include some custom plugins, or bundle several related task classes into a single library. Or some combination of the two.
Writing a simple pluginH1
build.gradle
bash
apply plugin: GreetingPluginclass GreetingPlugin implements Plugin<Project> {void apply(Project project) {project.task('hello') << {println "Hello from the GreetingPlugin"}}}
Output of gradle -q hello `
gradle -q hello Hello from the GreetingPlugin `
A standalone projectH2
Now we will move our plugin to a standalone project, so we can publish it and share it with others. This project is simply a Groovy project that produces a JAR containing the plugin classes.
So how does Gradle find the Plugin implementation? The answer is you need to provide a properties file in the jar’s META-INF/gradle-plugins directory that matches the id of your plugin.
Example 59.6. Wiring for a custom plugin
src/main/resources/META-INF/gradle-plugins/org.samples.greeting.properties
implementation-class=org.gradle.GreetingPlugin
Using your plugin in another projectH1
To use a plugin in a build script, you need to add the plugin classes to the build script’s classpath. To do this, you use a “buildscript { }” block, as described in Section 60.5, “External dependencies for the build script”.
Example 59.7. Using a custom plugin in another project
build.gradle
bash
buildscript {repositories {maven {url uri('../repo')}}dependencies {classpath group: 'org.gradle', name: 'customPlugin',version: '1.0-SNAPSHOT'}}apply plugin: 'org.samples.greeting'
评论
新的评论
上一篇
Android Authenticator
对用Android的Authenticator,官方的API Guides是似乎没有很好的介绍文档,Reference中也只是简单地介绍了一下。虽然 sync-adapters 中对于Authenticator有提及,但是对于其用法也没有一个完整的认识,所以对其进行一定的学习并…
下一篇
Guice Account Scope
Google Guice 支持自定义Scope,当然通常情况下我们不需要自己实现Scope。RoboGuice作为Android的IOC容器,实现了基于当前Context的ContextScope。我们可以根据自己需求自定义Scope,实现一些特殊的需求。GitHub的Andr…
