使用Gradle Kotlin DSL构建时,如何配置Scala插件的指定参数?
If you're switching from Groovy to Kotlin DSL for your Scala project, here's how to translate those two configurations correctly:
1. Configure IDEA Target Version
In Groovy, you’d use idea { targetVersion = "13" }, but Kotlin DSL uses setter methods for mutable properties instead of direct assignment. Here’s the equivalent Kotlin code:
idea { targetVersion.set("13") }
This tells the IDEA plugin to generate project files compatible with IntelliJ IDEA 13.
2. Set Source Compatibility to Java 8
The sourceCompatibility setting is managed by the Java plugin (which the Scala plugin automatically applies). In Kotlin DSL, you configure this via the java extension. It’s best practice to use the JavaVersion enum for type safety, but a string-based approach is also possible:
Using Enum (Recommended)
java { sourceCompatibility = JavaVersion.VERSION_1_8 // It’s often wise to match targetCompatibility to avoid runtime issues targetCompatibility = JavaVersion.VERSION_1_8 }
Using String (Alternative)
If you prefer using the version string directly:
java { sourceCompatibility.set(JavaVersion.toVersion("1.8")) targetCompatibility.set(JavaVersion.toVersion("1.8")) }
Full Example Build Script
Here’s how these configurations fit into a complete build.gradle.kts file:
plugins { scala idea // Required to enable the IDEA configuration block } idea { targetVersion.set("13") } java { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } dependencies { // Add your Scala library dependency (adjust version as needed) implementation("org.scala-lang:scala-library:2.13.10") }
内容的提问来源于stack exchange,提问作者Georg Heiler




