我正在尝试将代码从一组用“apply from:”包含的 Gradle 脚本编写的插件移动到“apply plugin:”包含的一 jar Groovy 脚本中。我基本上是成功的,但是我在为那些拥有它们的插件创建“扩展对象”时遇到了麻烦。我似乎无法在插件的应用函数中引用扩展类名称。我缺少一些基本的东西——它是什么?
我已经将问题简化为一个简单的示例,我希望它可以作为一个有用的模板。这是一个名为“sample”的插件:
插入:
插件\src\main\groovy\com\company\gradle\sample.groovy:
package com.company.gradle
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.Plugin
import org.gradle.api.Task
// Configuration object (project may override these values)
class sampleExtension {
String product // product code - 3 alphanumeric characters
String customer // customer id - integer 0-999999
sampleExtension() {
this.product = 'ABC'
this.customer = '123'
}
}
class sample implements Plugin<Project> {
void apply(Project target) {
println "SAMPLE: A"
// create the configuration objects for this plugin
target.extensions.create('sample', sampleExtension)
println "SAMPLE: B"
// This task generates the license.
Task sampleTask = target.task('printProperties') {
doFirst {
println("target.sample.product = '$target.sample.product'")
println("target.sample.customer = '$target.sample.customer'")
}
}
println "SAMPLE: C"
}
}
printlns 很有帮助,因为在使用示例的项目中,插件错误被报告为发生在项目脚本中的某行,而不是插件脚本。我不确定构造函数是否必要;我也尝试将扩展类嵌套在类'sample'中
插件\src\main\resources\META-INF\gradle-plugins\sample.properties:
implementation-class=com.company.gradle.sample
插件\build.gradle
apply plugin: 'groovy'
apply plugin: 'maven'
dependencies {
compile gradleApi()
compile localGroovy()
}
group = 'com.company.gradle'
version = '1.0'
uploadArchives {
repositories { mavenDeployer { repository(url: uri('../repo')) }}
}
那就是插件。这是项目脚本,出于本示例的目的,它没有代码 - 只需执行示例插件提供的任务:
项目\build.gradle:
buildscript {
repositories{ maven{url uri('../repo')} }
dependencies { classpath 'com.company.gradle:plugin:1.0' }
}
apply plugin: 'sample'
该插件是用“gradle build uploadArchives”构建的,它编译干净。该项目是用“gradle printProperties”构建的。它打印“A”消息,并提示它未能应用示例插件,因为类“com.company.gradle.sample”的“没有这样的属性:sampleExtension”。
c:\jdev\project>gradle printProperties
SAMPLE: A
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\jdev\project\build.gradle' line: 9
* What went wrong:
A problem occurred evaluating root project 'project'.
> Failed to apply plugin [id 'sample']
> No such property: sampleExtension for class: com.company.gradle.sample
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug
option to get more log output.
BUILD FAILED
Total time: 2.13 secs
当插件被编写为包含在 project\build.gradle 中的“sample.groovy”脚本时,插件代码工作,并带有“apply from: sample.groovy”。
当我的扩展类嵌套时,失败是不同的:“无法创建 com.company.gradle.sample$sampleExtension_Decorated 类型的实例。”
当您有一个扩展对象并且插件是用 .groovy 编写的插件 jar 而不是 .gradle 直接包含时,模式是什么?
感谢您花时间涉足这一切……
最佳答案
使用稍微修改过的代码版本,我可以获得模型的值。
构建.gradle:
group 'com.jbirdvegas.so'
version '1.0-SNAPSHOT'
apply plugin: 'groovy'
// apply our plugin... calls SamplePlugin#apply(Project)
apply plugin: 'sample'
repositories {
mavenCentral()
}
dependencies {
compile localGroovy()
}
// caller populates the extension model applied above
sample {
product = 'abc'
customer = 'zyx'
}
// dummy task to limit console output for example
task doNothing <<{}
buildSrc/src/main/groovy/com/jbirdvegas/so/a3850424/SamplePlugin.groovy:
package com.jbirdvegas.so.a38504024
import org.gradle.api.Plugin
import org.gradle.api.Project
class SamplePlugin implements Plugin<Project> {
@Override
void apply(Project target) {
// create our extension on the project for our model
target.extensions.create('sample', SampleModel)
// once the script has been evaluated the values are available
target.afterEvaluate {
// here we can do whatever we need to with our values
println "populated model: $target.extensions.sample"
}
}
}
buildSrc/src/main/groovy/com/jbirdvegas/so/a38504024/SampleModel.groovy:
package com.jbirdvegas.so.a38504024
// define our DSL model
class SampleModel {
public String product;
public String customer;
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("SampleModel{");
sb.append("product='").append(product).append('\'');
sb.append(", customer='").append(customer).append('\'');
sb.append('}');
return sb.toString();
}
}
buildSrc/src/main/resources/META-INF/gradle-plugins/sample.properties
implementation-class=com.jbirdvegas.so.a38504024.SamplePlugin
使用此设置,我们可以在 DSL block 中看到调用者提供的值
$ ./gradlew -q doNothing
SampleModel{product='abc', customer='zyx'}
关于plugins - 插件 jar 中的 Gradle 扩展对象(groovy 代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38504024/