2016-08-24 5 views
4

Gradleプロジェクトのtypical Kotlin configurationは非常に定型的なもので、私はそれを再利用できるように外部ビルドスクリプトに抽象化する方法を探しています。複数のGradleプロジェクト全体に共通のKotlin構成を共有するにはどうすればよいですか?

私は(下記)作業溶液を持っていますが、kotlin-のGradle-プラグインが箱から出して、このように動作しないとして、それはハックのビットのように感じています。

それはすなわち

apply plugin: 'kotlin'は、単純な(まあ、通常は)回避策は、完全修飾によって適用することですPlugin with id 'kotlin' not found.

になり、あなたcan't apply the plugin by idとして外部スクリプトからの任意の非標準のプラグインを適用するには厄介ですすなわち、この場合に示す良い小さな例外をスロー

apply plugin: org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

プラグインのクラス名、番目のものEプラグインは、おそらくこのよう呼び出されることを意図していなかった。

Failed to determine source cofiguration of kotlin plugin. 
Can not download core. Please verify that this or any parent project 
contains 'kotlin-gradle-plugin' in buildscript's classpath configuration. 

は、だから私は、現在のbuildscriptからプラグインを見つけるためにそれを強制的にプラグイン(real pluginだけの修正版)を一緒にハックするために管理しました。

buildscript { 
    ext.kotlin_version = "1.0.3" 
    repositories { 
     jcenter() 
    } 
    dependencies { 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
    } 
} 

dependencies { 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
    compile "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version" 
} 

apply plugin: CustomKotlinPlugin 
import org.jetbrains.kotlin.gradle.plugin.CleanUpBuildListener 
import org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper 
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin 
import org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider 

/** 
* Wrapper around the Kotlin plugin wrapper (this code is largely a refactoring of KotlinBasePluginWrapper). 
* This is required because the default behaviour expects the kotlin plugin to be applied from the project, 
* not from an external buildscript. 
*/ 
class CustomKotlinPlugin extends KotlinBasePluginWrapper { 

    @Override 
    void apply(Project project) { 
     // use String literal as KOTLIN_COMPILER_ENVIRONMENT_KEEPALIVE_PROPERTY constant isn't available 
     System.setProperty("kotlin.environment.keepalive", "true") 

     // just use the kotlin version defined in this script 
     project.extensions.extraProperties?.set("kotlin.gradle.plugin.version", project.property('kotlin_version')) 

     // get the plugin using the current buildscript 
     def plugin = getPlugin(this.class.classLoader, project.buildscript) 
     plugin.apply(project) 

     def cleanUpBuildListener = new CleanUpBuildListener(this.class.classLoader, project) 
     cleanUpBuildListener.buildStarted() 
     project.gradle.addBuildListener(cleanUpBuildListener) 
    } 

    @Override 
    Plugin<Project> getPlugin(ClassLoader pluginClassLoader, ScriptHandler scriptHandler){ 
     return new KotlinPlugin(scriptHandler, new KotlinTasksProvider(pluginClassLoader)); 
    } 
} 

このkotlin.gradle

は、任意のプロジェクトで適用することができる(すなわちapply from: "kotlin.gradle")、あなたがアップし、Kotlinの開発のために実行しています。

それは動作しますが、私はまだ問題を抱えていませんが、良い方法があれば、私は思ったんだけど?私はKotlinの新しいバージョンがあるたびに、プラグインの変更をマージすることに本当に熱心ではありません。

答えて

5

nebula-kotlin-pluginをご覧ください。あなたがそこで達成しようとしているものに非常に近いようです。

+0

まあそれは陽気だ - 私はすでにkotlinプラグインは、私が最後に見た時間は存在しませんでした....ネットフリックスの星雲プラグイン(プロジェクト、リリース、等)の一握りを使用しています。私はこれを試してみましょう、ありがとう! –

関連する問題