2016-07-21 4 views
0

"apply from:"に含まれるGradleスクリプトで書かれた複数のプラグインのセットから、 "apply plugin:"に含まれているGroovyスクリプトのjarファイルにコードを移動しようとしています。 。私はほとんど成功していますが、それらを持つプラグインのために '拡張オブジェクト'を作成するのに問題があります。私はプラグインのapply関数内で拡張クラス名を参照することができないようです。私は何か基本的なものを欠いている - それは何ですか?プラグインjarのグレイド拡張オブジェクト

私は問題を単純な例にして、有用なテンプレートとして役立つことを願っています。ここでは、プラグインが命名された "サンプル":

PLUGIN: プラグイン\ 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があるため、プロジェクトに便利ですサンプルを使用するプラグインエラーは、プラグインスクリプトではなくプロジェクトスクリプト内のある行で発生したと報告されます。コンストラクタが必要かどうかはわかりません。

implementation-class=com.company.gradle.sample 

プラグインの\ build.gradle:私はまた、クラスのサンプル」

プラグイン\ SRC \メイン\リソース\ META-INF \ Gradleでは、プラグイン\ sample.properties内の拡張クラスを入れ子に試してみました

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' 

プラグインは 'で構築されているだけでサンプルプラグインが提供するタスクを実行する - ここでは、この例の目的のために何のコードを持っていないプロジェクトのスクリプトは、ありますグラデルビルドuploadArchives 'それはきれいにコンパイルします。プロジェクトは 'gradle printProperties'で構築されています。 "A"というメッセージが表示され、サンプルプラグインの適用に失敗したため、クラス 'com.company.gradle.sample'の 'no such property:sampleExtension'が表示されます。 「sample.groovy」スクリプトが「:sample.groovyから適用されます」と、プロジェクトの\ build.gradleに含まれるプラグインが書かれたとき

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 

プラグインコードが働きました。

拡張クラスがネストされているとき、「com.company.gradle.sample $ sampleExtension_Decoratedタイプのインスタンスを作成できませんでした。

拡張オブジェクトがあり、プラグインがプラグインjarのために.groovyで書かれている場合のパターンは、直接インクルードの場合は.gradleではなく?私は、モデルの値を取得することができるよ、あなたのコードを少し変更したバージョンを使用して、このすべてを通じて渡るために時間を割いて

おかげで...

答えて

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// a3850424/SamplePluginそう。グルーヴィー:

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 /そう/ 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私たちはあなたのDSLのブロックに、呼び出し側から供給された値を見ることができ、この設定を使用して

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

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

ここで、プラグインは定義されている同じGradleプロジェクトで参照されます。私は、プラグインをjarファイルにパッケージ化して、別のプロジェクト(完全に別のコードベース)を使用して、ソースツリーにプラグインソースを持たずにプラグインを適用できるようにしたいと考えています。 (実際には_lots_異なるプロジェクト!) しかし、この例では、Extensionクラスがプラグインとは別のファイルにあることもわかります。私は秘密を知っているのだろうか。 (ああ、明日までこれを試すことはできません) ありがとうございます。細部を感謝します。 –

+0

概念は同じです。 'buildSrc'を使うのではなく、プラグインを自分のプロジェクトにするだけです。それを 'buildScript'ブロックのクラスパスに追加するだけです。どちらの方法でも、上記のソースはスタンドアロンのプラグインとして使用できます。 – JBirdVegas

+0

独自のクラスファイルに拡張機能を移動できませんでした。 :-( –

関連する問題