首页 > 解决方案 > How to configure Gradle Java plugin from a custom Gradle plugin

问题描述

I've written a custom Gradle plugin in Kotlin 1.2.50 for use with Gradle 4.8.

I've successfully applied the Java plugin from my plugin's apply method:

override fun apply(project: Project) {
    project.pluginManager.apply(JavaPlugin::class.java)
    // configure Java plugin here
}

How do I configure the Java plugin?

e.g., I want to achieve the equivalent of the following that would normally be in a build.gradle.kts:

java {
    sourceCompatibility = VERSION_1_10
    targetCompatibility = VERSION_1_10
}

标签: gradlegradle-plugingradle-custom-plugin

解决方案


我挖掘了 Gradle 代码并找到了解决方案:

override fun apply(project: Project) {
    project.pluginManager.apply(JavaPlugin::class.java)

    val javaPlugin = project.convention.getPlugin(JavaPluginConvention::class.java)

    javaPlugin.sourceCompatibility = VERSION_1_10
    javaPlugin.targetCompatibility = VERSION_1_10
}

推荐阅读