Skip to content
Advertisement

Gradle Kotlin DSL – Build script fails when java.sourceCompatibility defined in Parent but build is successful when added to child module

Am trying to setup a new repository for a multi module springBoot application using gradle (Kotlin DSL for build scripts)

As part of the same I am trying to declare generic configuration and dependencies needed for all subProjects. In doing so, I am trying to define sourceCompatility for all child projects in the subprojects block of parent build.gradle.kts file

When I try to compile my project with above config then build is failing with the following exception

* What went wrong:
Extension with name 'java' does not exist. Currently registered extension names: [ext]

But if I move the line java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11 to the child module’s build.gradle.kts file then it is compiling successfully and application is coming up as expected.

I cannot understand what I’m missing here. Please help me understand this.

Parent build.gradle.kts

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("java")
    id("idea")
    id("war")
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

subprojects {
    group = "com.company.example"
    version = "0.0.1"

    java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

    repositories {
        mavenCentral()
        maven { url = uri("http://nexus.pentaho.org/content/groups/omni/") }
    }

    apply() {
        plugin("java")
        plugin("idea")
        plugin("io.spring.dependency-management")
    }

    dependencies {
        implementation("io.jsonwebtoken:jjwt-api:0.10.7")
        implementation("io.jsonwebtoken:jjwt-impl:0.10.7")
        implementation("com.auth0:java-jwt:3.10.3")

        implementation(group= "org.mockito", name= "mockito-core", version= "3.1.0")

        implementation(group= "javax.inject", name= "javax.inject", version= "1")
        implementation(group= "org.springframework", name= "spring-context", version= "5.2.6.RELEASE")
        implementation(group= "org.springframework.security", name= "spring-security-core", version= "4.2.3.RELEASE")

        implementation(group= "com.google.protobuf", name= "protobuf-java", version= "3.12.1")
        implementation("com.google.api.grpc:proto-google-common-protos:1.16.0")
        implementation("com.google.api.grpc:grpc-google-longrunning-v1:0.1.8")
        implementation(group= "org.apache.commons", name= "commons-lang3", version= "3.7")
        implementation(group="com.google.guava", name= "guava", version= "29.0-jre")
        implementation(group= "commons-io", name= "commons-io", version= "2.7")

        testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    }
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}

Child build.gradle.kts

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("org.springframework.boot") version "2.3.0.RELEASE"
}

//java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

dependencies {
    
    implementation("org.springframework.boot:spring-boot-starter-actuator")
    implementation("org.springframework.boot:spring-boot-starter-web")

}

val jar by tasks.getting(Jar::class) {
    manifest {
        attributes["Main-Class"] = "com.company.example.module.ExampleApplication"
    }

    from(sourceSets.main.get().output)

    dependsOn(configurations.runtimeClasspath)
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })
}

settings.gradle.kts for the project:

rootProject.name = "project_name"
include(":module_name")

P.S: I also tried using

configure<JavaPluginConvention>{
    sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11
}

But the same pattern is observed. Error thrown in this case is

* What went wrong:
Extension of type 'JavaPluginConvention' does not exist. Currently registered extension types: [ExtraPropertiesExtension]

Advertisement

Answer

buildscript {
    repositories {
        mavenCentral()
    }
}

plugins {
    id("java")
    id("idea")
    id("war")
    id("io.spring.dependency-management") version "1.0.9.RELEASE"
}

subprojects {
    group = "com.company.example"
    version = "0.0.1"

    // Changed the position of the apply block from below the
    // source captibility to above 
    apply {
        plugin("java")
        plugin("idea")
        plugin("io.spring.dependency-management")
    }

    java.sourceCompatibility = org.gradle.api.JavaVersion.VERSION_11

    repositories {
        mavenCentral()
        maven { url = uri("http://nexus.pentaho.org/content/groups/omni/") }
    }

    dependencies {
        implementation("io.jsonwebtoken:jjwt-api:0.10.7")
        implementation("io.jsonwebtoken:jjwt-impl:0.10.7")
        implementation("com.auth0:java-jwt:3.10.3")

        implementation(group= "org.mockito", name= "mockito-core", version= "3.1.0")

        implementation(group= "javax.inject", name= "javax.inject", version= "1")
        implementation(group= "org.springframework", name= "spring-context", version= "5.2.6.RELEASE")
        implementation(group= "org.springframework.security", name= "spring-security-core", version= "4.2.3.RELEASE")

        implementation(group= "com.google.protobuf", name= "protobuf-java", version= "3.12.1")
        implementation("com.google.api.grpc:proto-google-common-protos:1.16.0")
        implementation("com.google.api.grpc:grpc-google-longrunning-v1:0.1.8")
        implementation(group= "org.apache.commons", name= "commons-lang3", version= "3.7")
        implementation(group="com.google.guava", name= "guava", version= "29.0-jre")
        implementation(group= "commons-io", name= "commons-io", version= "2.7")

        testImplementation("org.junit.jupiter:junit-jupiter-api:5.3.1")
        testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.3.1")
    }
}

tasks.getByName<Test>("test") {
    useJUnitPlatform()
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement