applyplugin:GreetingPluginclassGreetingPluginimplementsPlugin<Project>{voidapply(Projectproject){project.task('hello')<<{println"Hello from the GreetingPlugin"}}}
在当前目录下,执行 gradle -q hello,会得到输出:Hello from the GreetingPlugin
applyplugin:GreetingPlugingreeting.message='Hi from Gradle'classGreetingPluginimplementsPlugin<Project>{voidapply(Projectproject){// Add the 'greeting' extension objectproject.extensions.create("greeting",GreetingPluginExtension)// Add a task that uses the configurationproject.task('hello')<<{printlnproject.greeting.message}}}classGreetingPluginExtension{defStringmessage='Hello from GreetingPlugin'}
执行同上命令,会得到输出 Hi from Gradle.
利用闭包输入参数
1234567891011121314151617181920
applyplugin:GreetingPlugingreeting{message='Hi'greeter='Gradle'}classGreetingPluginimplementsPlugin<Project>{voidapply(Projectproject){project.extensions.create("greeting",GreetingPluginExtension)project.task('hello')<<{println"${project.greeting.message} from ${project.greeting.greeter}"}}}classGreetingPluginExtension{StringmessageStringgreeter}
通过以闭包的格式来设置 greeting 参数 message 和 greeter,来控制输出。
执行之后,会得到输出 Hi from Gradle.