1

I created a project with spring initializr with kotlin and gradle to study hexagonal architecture in microservices. I'm using IntelliJ with modules to dividing the code but the spring-jpa dependency doesn't work in module (or subproject).

The start build.gradle.kts is:

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
    
    plugins {
        id("org.springframework.boot") version "2.5.9-SNAPSHOT"
        id("io.spring.dependency-management") version "1.0.11.RELEASE"
        kotlin("jvm") version "1.5.32"
        kotlin("plugin.spring") version "1.5.32"
        kotlin("plugin.jpa") version "1.5.32"
    }
    
    group = "com.donadon.studyhexagonal"
    version = "0.0.1-SNAPSHOT"
    java.sourceCompatibility = JavaVersion.VERSION_11
    
    repositories {
        mavenCentral()
        maven { url = uri("https://repo.spring.io/milestone") }
        maven { url = uri("https://repo.spring.io/snapshot") }
    }
    
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        implementation("org.springframework.boot:spring-boot-starter-data-redis-reactive")
        implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
        implementation("org.springframework.boot:spring-boot-starter-security")
        implementation("org.springframework.boot:spring-boot-starter-web")
        implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
        implementation("io.projectreactor.kotlin:reactor-kotlin-extensions")
        implementation("org.jetbrains.kotlin:kotlin-reflect")
        implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
        implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor")
        runtimeOnly("org.postgresql:postgresql")
        testImplementation("org.springframework.boot:spring-boot-starter-test")
        testImplementation("io.projectreactor:reactor-test")
        testImplementation("org.springframework.security:spring-security-test")
    }
    
    tasks.withType<KotlinCompile> {
        kotlinOptions {
            freeCompilerArgs = listOf("-Xjsr305=strict")
            jvmTarget = "11"
        }
    }
    
    tasks.withType<Test> {
        useJUnitPlatform()
    }

I move the repositories and dependencies to subprojects method e I put some plugins together but the follow error happened:

subprojects {
    apply {
        plugin("kotlin")
        plugin("kotlin-jpa")
        plugin("io.spring.dependency-management")
    }

    repositoreis { ... }
    dependencies {
        implementation("org.springframework.boot:spring-boot-starter-data-jpa")
        ...
    }
}

The error:

Configuration with name 'implementation' not found.
    at Program.execute(Unknown Source)
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'implementation' not found.
    at Build_gradle$2$2.invoke(build.gradle.kts:29)

How I make for dependencies work to all subprojects? I read the gradle doc and others questions here but nothing helped me. Thanks for while.

EDIT

I created a module with name database and when I try to use @Entity, it is not found but if I use in some class in main src, it founds the annotation.

Donadon
  • 113
  • 11

1 Answers1

1

Couple things:

  1. Some gradle configurations cannot be shared using the subprojects method. This includes dependencies. Check out this StackOverflow question for information on how to share dependency versions.
  2. According to gradle, reusing logic through subprojects is discouraged: Another, discouraged, way to share build logic between subproject is cross project configuration via the subprojects {} and allprojects {} DSL constructs. (Link)
Gabriel Pizarro
  • 400
  • 4
  • 15
  • Thank you for answer Gabriel, Is that why when we use "gradle init" to create a project with submodules, it creates a build.gradle for each subproject and does not create the root build.gradle? – Donadon Jan 07 '22 at 02:35
  • 1
    @Donadon No, the root build file is still useful in many situations: plugin version syncing between subprojects, defining dependency versions in `ext`, defining common artifact attributes, etc. Additionally, some IDEs like IntelliJ require a root gradle build file to import subprojects. – Gabriel Pizarro Jan 07 '22 at 04:00
  • Please also accept this answer if it answered your question. – Gabriel Pizarro Jan 07 '22 at 04:00
  • 1
    I got it and marked the answer! I found some links that the examples didn't have the parent build file and the own gradle init also doesn't generate this file. Your answer help me to find the way to continue, I still didn't test but I got the concept. – Donadon Jan 07 '22 at 15:11