Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在多个Gradle项目中共享样板Kotlin配置?_Gradle_Kotlin_Gradle Plugin - Fatal编程技术网

如何在多个Gradle项目中共享样板Kotlin配置?

如何在多个Gradle项目中共享样板Kotlin配置?,gradle,kotlin,gradle-plugin,Gradle,Kotlin,Gradle Plugin,Gradle项目中的脚本非常简单,我正在寻找一种将其抽象为外部构建脚本的方法,以便可以重用 我有一个可行的解决方案(见下文),但它感觉有点像黑客,因为kotlin gradle插件不是这样开箱即用的 当您使用外部脚本时,应用任何非标准插件都是很麻烦的 apply plugin:'kotlin'将导致找不到id为'kotlin'的插件。 简单(通常)的解决方法是使用插件的完全限定类名,即 应用插件:org.jetbrains.kotlin.gradle.plugin.KotlinPluginWra

Gradle项目中的脚本非常简单,我正在寻找一种将其抽象为外部构建脚本的方法,以便可以重用

我有一个可行的解决方案(见下文),但它感觉有点像黑客,因为kotlin gradle插件不是这样开箱即用的

当您使用外部脚本时,应用任何非标准插件都是很麻烦的

apply plugin:'kotlin'
将导致找不到id为'kotlin'的
插件。

简单(通常)的解决方法是使用插件的完全限定类名,即

应用插件:org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper

在本例中,它抛出了一个很好的小异常,表明插件可能不是以这种方式调用的:

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.
所以我设法拼凑了一个插件(只是的一个修改版本),强制它从当前的构建脚本中找到插件

kotlin.gradle

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));
    }
}
buildscript{
ext.kotlin_version=“1.0.3”
存储库{
jcenter()
}
依赖关系{
classpath“org.jetbrains.kotlin:kotlin gradle plugin:$kotlin_version”
}
}
依赖关系{
编译“org.jetbrains.kotlin:kotlin stdlib:$kotlin_version”
编译“org.jetbrains.kotlin:kotlin reflect:$kotlin_version”
}
应用插件:CustomKotlinPlugin
导入org.jetbrains.kotlin.gradle.plugin.CleanUpBuildListener
导入org.jetbrains.kotlin.gradle.plugin.KotlinBasePluginWrapper
导入org.jetbrains.kotlin.gradle.plugin.KotlinPlugin
导入org.jetbrains.kotlin.gradle.tasks.KotlinTasksProvider
/**
*Kotlin插件包装器(此代码主要是对KotlinBasePluginWrapper的重构)。
*这是必需的,因为默认行为要求从项目中应用kotlin插件,
*不是来自外部构建脚本。
*/
类CustomKotlinPlugin扩展了KotlinBasePluginWrapper{
@凌驾
无效申请(项目){
//使用字符串文字作为KOTLIN_编译器_环境_KEEPALIVE_属性常量不可用
System.setProperty(“kotlin.environment.keepalive”、“true”)
//只需使用此脚本中定义的kotlin版本
project.extensions.extraProperties?.set(“kotlin.gradle.plugin.version”,project.property(“kotlin_version”))
//使用当前构建脚本获取插件
def plugin=getPlugin(this.class.classLoader,project.buildscript)
plugin.apply(项目)
def cleanubuildlistener=new cleanubuildlistener(this.class.classLoader,项目)
CleanuBuildListener.buildStarted()
project.gradle.addBuildListener(CleanuBuildListener)
}
@凌驾
Plugin getPlugin(类加载器pluginClassLoader,脚本处理程序ScriptHandler){
返回新的KotlinPlugin(scriptHandler,新的KotlinTasksProvider(pluginClassLoader));
}
}
然后,这可以应用于任何项目(即从“kotlin.gradle”应用),您就可以开始运行kotlin开发了


它是有效的,我还没有遇到任何问题,但我想知道是否有更好的方法?我并不是真的喜欢每次有新版本的Kotlin时都合并插件的更改。

请查看。这似乎非常接近您试图在那里实现的目标。

这里的问题是无法通过init脚本中的id应用插件。这就是为什么需要使用完全限定的类名作为解决方法

例如,我在init脚本中有以下内容,并且可以正常工作:

应用插件:org.jetbrains.kotlin.gradle.plugin.KotlinPlatformJvmPlugin
顺便说一句,我创建了一个gradle插件,用于准备定制的gradle发行版,其公共设置在init script-中定义。它非常适合我的项目,例如,库项目的build.gradle如下所示(这是一个完整的文件,所有存储库、应用插件、依赖项等的设置都在init脚本中定义):

依赖项{
编译“org.springframework.kafka:springkafka”
}

这太搞笑了——我已经在使用一些Netflix的nebula插件(项目、发行版等)……kotlin插件在我上次查看时并不存在。我会试试看,谢谢!