How to configure Gradle Java plugin from a custom Gradle plugin

巧了我就是萌 提交于 2019-12-11 07:59:26

问题


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
}

回答1:


I dug through the Gradle code and found a solution:

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
}


来源:https://stackoverflow.com/questions/50860095/how-to-configure-gradle-java-plugin-from-a-custom-gradle-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!