插件jar中的Gradle扩展对象(groovy代码)

时间:2016-07-21 12:15:07

标签: plugins gradle groovy

我正在尝试从一组用Gradle脚本编写的一组插件中移动代码" apply from:"一个包含在"中的一个Groovy脚本:apply plugin:"。我一直很成功,但我在创建扩展对象时遇到了问题。对于那些拥有它们的插件。我好像无法在插件的apply函数中引用扩展类名。我遗漏了一些基本的东西 - 它是什么?

我已将问题简化为一个简单的例子,我希望它可以作为一个有用的模板。这是一个名为" sample":

的插件

插件: 插件\ SRC \主\常规\ COM \公司\ 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非常有用,因为在使用示例的项目中,插件错误报告发生在项目脚本中的某一行,而不是插件脚本。我不确定构造函数是否必要;我还尝试在类&#39; sample&#39;

中嵌套扩展类

插件\ SRC \主\资源\ META-INF \ gradle这个-插件\ 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'

该插件是使用&gra; gradle build uploadArchives&#39;它编译清洁。该项目使用&gradle printProperties&#39;构建。它打印&#34; A&#34;消息,并抱怨它未能应用示例插件,因为没有这样的属性:sampleExtension&#39; for class&#39; com.company.gradle.sample&#39;。

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

当插件被编写为&#39; sample.groovy&#39;插件代码有效。包含在项目\ build.gradle中的脚本,来自:sample.groovy&#39;。

当我的扩展类嵌套时,失败是不同的:&#39;无法创建类型为com.company.gradle.sample $ sampleExtension_Decorated的实例。&#39;

当你有一个扩展对象并且插件用.groovy写入插件jar而不是.gradle直接包含时,模式是什么?

感谢您花时间浏览所有这些......

1 个答案:

答案 0 :(得分:1)

使用稍微修改过的代码版本,我就可以获得模型的值。

的build.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 /主/常规/ 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 /主/常规/ 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 /主/资源/ META-INF / gradle这个-插件/ sample.properties

implementation-class=com.jbirdvegas.so.a38504024.SamplePlugin

使用此设置,我们可以看到DSL块中调用者提供的值

$ ./gradlew -q doNothing
SampleModel{product='abc', customer='zyx'}