07-20-2023, 09:08 AM
I want to split my project into subprojects. The default Gradle setting from the IntelliJ IDE is:
```kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
```
That setting compiles. ButI don't want repeat that code in every subproject. So I changed the *build.gradle.kts* to
```kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
subprojects {
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
```
but I get the exception:
> e: C:\[...]\build.gradle.kts:1:12: Unresolved reference: jetbrains e:
> C:\[...]\build.gradle.kts:16:9: Unresolved reference: implementation
> e: C:\[...]\build.gradle.kts:19:20: Unresolved reference:
> KotlinCompile e: C:\[...]\build.gradle.kts:19:35: Type mismatch:
> inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
> e: C:\[...]\build.gradle.kts:20:9: Unresolved reference: kotlinOptions
>
> FAILURE: Build failed with an exception.
>
> * Where: Build file 'C:\[...]\build.gradle.kts' line: 1
>
> * What went wrong: Script compilation errors:
>
> Line 01: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
> ^ Unresolved reference: jetbrains
>
> Line 16: implementation(kotlin("stdlib-jdk8"))
> ^ Unresolved reference: implementation
>
> Line 19: tasks.withType<KotlinCompile> {
> ^ Unresolved reference: KotlinCompile
>
> Line 19: tasks.withType<KotlinCompile> {
> ^ Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
>
> Line 20: kotlinOptions.jvmTarget = "1.8"
> ^ Unresolved reference: kotlinOptions
>
> 5 errors
>
> * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
>
> * Get more help at
> BUILD FAILED in 1s
I think there is an easy syntax error, but I can't find it.
```kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
```
That setting compiles. ButI don't want repeat that code in every subproject. So I changed the *build.gradle.kts* to
```kotlin
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
subprojects {
plugins {
kotlin("jvm") version "1.3.50"
}
group = "project"
version = "0.0.1-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.8"
}
}
```
but I get the exception:
> e: C:\[...]\build.gradle.kts:1:12: Unresolved reference: jetbrains e:
> C:\[...]\build.gradle.kts:16:9: Unresolved reference: implementation
> e: C:\[...]\build.gradle.kts:19:20: Unresolved reference:
> KotlinCompile e: C:\[...]\build.gradle.kts:19:35: Type mismatch:
> inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
> e: C:\[...]\build.gradle.kts:20:9: Unresolved reference: kotlinOptions
>
> FAILURE: Build failed with an exception.
>
> * Where: Build file 'C:\[...]\build.gradle.kts' line: 1
>
> * What went wrong: Script compilation errors:
>
> Line 01: import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
> ^ Unresolved reference: jetbrains
>
> Line 16: implementation(kotlin("stdlib-jdk8"))
> ^ Unresolved reference: implementation
>
> Line 19: tasks.withType<KotlinCompile> {
> ^ Unresolved reference: KotlinCompile
>
> Line 19: tasks.withType<KotlinCompile> {
> ^ Type mismatch: inferred type is () -> Unit but Class<TypeVariable(S)!>! was expected
>
> Line 20: kotlinOptions.jvmTarget = "1.8"
> ^ Unresolved reference: kotlinOptions
>
> 5 errors
>
> * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
>
> * Get more help at
[To see links please register here]
>> BUILD FAILED in 1s
I think there is an easy syntax error, but I can't find it.