Gradle dependency for subprojects does not work - spring

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.

Couple things:
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.
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)

Related

Error creating bean with name 'configurationPropertiesBeans' defined in class path resource /ConfigurationPropertiesRebinderAutoConfiguration.class]

I can build the application (Kotlin+Spring+Spring Cloud) but I can't start it.
Based on what I searched around it is related to incompability among Spring dependencies. I found someone facing similar issue as mine but after applying its solution I keep getting same issue other question
I tried also the the trick suggested with Spring Initializr but I got nothing when I type spring-cloud-starter
I guess the issue will fix when I set correct versions for:
id("org.springframework.boot") version "2.4.7"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
implementation("org.springframework.boot:spring-boot-dependencies:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-web:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:2.2.9.RELEASE")
implementation("io.github.openfeign:feign-okhttp:10.2.0")
Here is my gradle.build.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.jetbrains.kotlin.jvm") version "1.4.10"
id("org.jetbrains.kotlin.kapt") version "1.4.10"
kotlin("plugin.spring") version "1.5.20"
id("org.springframework.boot") version "2.4.7"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
}
val kotlinVersion: String by project
val springVersion: String by project
val projectGroupId: String by project
val projectVersion: String by project
val jacocoVersion: String by project
group = projectGroupId
version = projectVersion
repositories {
mavenLocal()
...
mavenCentral()
}
// add dependencies
dependencies {
kapt(kotlin("stdlib", kotlinVersion))
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect", kotlinVersion))
compile("br.com.mycomp:lib-log:3.2.0-74")
implementation("org.springframework.boot:spring-boot-dependencies:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-web:${springVersion}")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.cloud:spring-cloud-starter-openfeign:2.2.9.RELEASE")
implementation("io.github.openfeign:feign-okhttp:10.2.0")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.11.2")
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
}
springBoot {
mainClass.set("com.examplo.demo.DemoApplication.kt")
}
sourceSets {
main {
java {
srcDirs("build/generated/source/avro/main/java")
}
}
}
apply {
tasks.test {
useJUnitPlatform()
}
configurations {
all {
exclude(group = "junit", module = "junit")
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "org.slf4j", module = "slf4j-log4j12")
}
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
javaParameters = true
}
}
}
}
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
and its gradle.properties
springVersion=2.3.4.RELEASE
kotlinVersion=1.4.10
projectGroupId=com.mycomp
projectVersion=0.0.1
jacocoVersion=0.8.7
artifactoryContextUrl=xxx
The version you have declared for your spring cloud dependency (2.2.9.RELEASE) is not compatible with spring boot 2.4. You need at least 3.0.0 See the table on https://spring.io/projects/spring-cloud -- specifically follow the link to the spring cloud 2020 releases since that's what corresponds to spring boot 2.4. I think this is likely the cause of your issue, but I have a few other suggestions to help keep things in sync.
I see kotlin version 1.4.10 but spring.kotlin version 1.5.20 and they usually keep lock step with one another, so it's best to at least use the same major version of both (in this case, 1.5.x) i.e. in your gradle.properties update the kotlin version to 1.5.x, and then in the build.gradle use
plugins {
id("io.spring.dependency-management") version "1.0.11.RELEASE"
id("org.springframework.boot") version "2.5.4"
kotlin("jvm") version "1.5.30"
kotlin("kapt") version "1.5.30"
kotlin("plugin.spring") version "1.5.30"
}
Also note the updated spring dependency management version. Since you've declared the spring boot version, and you're using the dependency management plugin, you should let it pull in the versions that it wants -- you shouldn't declare the $springVersion on your dependencies, and you don't need the springVersion variable in your gradle.properties. The dependency management will pull in the proper version, which is already being done for your webflux dependency. This part is the second most likely cause of your problem.
If you're absolutely sure that the versions you have declared are compatible with one another, make sure an older version of cloud isn't being unexpectedly pulled in.
./gradlew dependencies --configuration compileClasspath
or
./gradlew -q dependencyInsight --dependency spring-cloud-starter-openfeign --configuration compileClasspath
If you're able to tease out the culprit that's bringing in the older version, you can prevent the older version from being pulled in transitively. In the snippet below, I'm omitting logger because I prefer log4j2, but you can substitute the old package, and if you're declaring the same package with a newer version, it'll be preferred
configurations {
all {
exclude(group = "org.springframework.boot", module = "spring-boot-starter-logging") // Prefer log4j2
}
}

Use kapt with spring-boot-configuration-processor in a gradle multi project

I am trying to create a gradle multi project in spring boot and kotlin. Using gradle kotlin scripts to write build file. This is my root project's build file (build.gradle.kts).
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlinVersion = "1.4.10"
val springBootVersion = "2.3.4.RELEASE"
val springDependencyManagementVersion = "1.0.10.RELEASE"
id("org.springframework.boot") version springBootVersion apply false
id("io.spring.dependency-management") version springDependencyManagementVersion apply false
kotlin("jvm") version kotlinVersion
kotlin("kapt") version kotlinVersion
kotlin("plugin.spring") version kotlinVersion apply false
}
allprojects {
repositories {
jcenter()
}
group = "org.example"
version = "1.0-SNAPSHOT"
}
subprojects {
apply {
plugin("org.jetbrains.kotlin.jvm")
plugin("org.jetbrains.kotlin.kapt")
plugin("org.jetbrains.kotlin.plugin.spring")
}
configure<JavaPluginExtension> {
sourceCompatibility = JavaVersion.VERSION_11
}
val developmentOnly by configurations.creating
configurations.runtimeClasspath.get().extendsFrom(developmentOnly)
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
api(platform(org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES))
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
kapt("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
}
}
configurations {
compileOnly {
extendsFrom(configurations.annotationProcessor.get())
}
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "11"
}
}
When building (./gradlew build) I'm getting this error:
> Task :adapters:kaptKotlin FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':adapters:kaptKotlin'.
> Could not resolve all files for configuration ':adapters:kapt'.
> Could not find org.springframework.boot:spring-boot-configuration-processor:.
Required by:
project :adapters
adapters:drivers:web is one of my subprojects and it is declared in settings.gradle.kts like this.
include("adapters:drivers:web")
PS: If I use annotationprocessor in place of kapt the build is successful. But I'm not sure if annotationprocessor can be used interchangeably with kapt since this will be a kotlin project.
kapt("org.springframework.boot:spring-boot-configuration-processor:$springBootVersion")
It seems kapt doesn't under the dependencies version management..
There is no repositories{} section is defined for you subprojects.
subprojects {
...
repositories {
mavenCentral()
}
}

Not able download dependencies in build.gradle.kts file using intellij

I am doing a Spring Boot Kotlin Project. Here is my build.gradle.kts file.
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("org.springframework.boot") version "2.1.8.RELEASE"
id("io.spring.dependency-management") version "1.0.8.RELEASE"
kotlin("jvm") version "1.2.71"
kotlin("plugin.spring") version "1.2.71"
}
group = "com.axis"
version = "0.0.1-SNAPSHOT"
//java.sourceCompatibility = JavaVersion.VERSION_1_8
val compileKotlin: KotlinCompile by tasks
val compileTestKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "1.8"
}
compileTestKotlin.kotlinOptions {
jvmTarget = "1.8"
}
repositories {
mavenCentral()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-mongodb-reactive")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("org.springframework.kafka:spring-kafka")
implementation("com.google.code.gson:gson")
implementation("com.itextpdf:itextpdf:5.0.6")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")
testImplementation("io.projectreactor:reactor-test")
testImplementation("io.mockk:mockk:1.9.3")
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I am adding the mockk dependency but it is not getting downloaded. Neither of the dependencies are getting downloaded. The build is successful but the dependency which I add don't get downloaded. I have disabled my proxy and tried.
I have checked the below link and tried out. But it doesn't work
Gradle build doesn't download dependencies
Any help will be appreciated.
I was able to resolve this. When i switched to a different network i was able to download. Not sure as to why i was being blocked to download dependencies in some network.

Kotlin: Cannot inline bytecode built with JVM target 1.8 into byte code with JVM target 1.6

I have an issue when trying to add a few dependencies to my kotlin spring project. I used the spring boot initializer to get a basic project running.
My problem: If I uncomment jackson or either Koin dependency then my build fails with the mentioned in the titile
Here is the build.gradle.kts file :
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("plugin.jpa") version "1.3.31"
id("org.springframework.boot") version "2.2.0.M4"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
kotlin("jvm") version "1.3.31"
kotlin("plugin.spring") version "1.3.31"
}
group = "com.example"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven { url = uri("https://repo.spring.io/snapshot") }
maven { url = uri("https://repo.spring.io/milestone") }
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("com.h2database:h2")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
// jackson
//implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
// Koin
//implementation("org.koin:koin-core:2.0.1")
// Koin Test
//implementation("org.koin:koin-test:2.0.1")
}
tasks.withType<Test> {
useJUnitPlatform()
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
I Have already tried all the solutions from this previous question someone else asked: Cannot inline bytecode built with JVM target 1.8 into bytecode that is being built with JVM target 1.6
I already had the KotlinCompile jvmTarget option in there and it has been set in my build settings as well. Any help is appreciated thanks!
A lot of the answers to this are android specific - using kotlin to write a spring boot app isn't that well documented or supported but still awesome. This setting in intelij cleared this error for me - hope it helps someone, my kotlin compiler had a target set to 1.6:

Spring complains #EnableWebMvc when I haven't used that annotation nor any dependency related to MVC

I am working on a reactive spring boot api server. I first wanted to use MVC pattern, but I thought reactor would be a good idea.
So I have deleted all spring dependencies on MVC (I believe).
But spring keeps complaining that I can't use #EnableWebMvc along #EnableWebFlux.
Following is my error log
Caused by: java.lang.IllegalStateException: The Java/XML config for Spring MVC and Spring WebFlux cannot both be enabled, e.g. via #EnableWebMvc and #EnableWebFlux, in the same application.
What possibly could be the problem? I sure did updated my dependencies.
And following is my build.gradle.kts file
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
buildscript {
repositories {
mavenCentral()
}
dependencies {
"classpath"(group = "gradle.plugin.com.palantir.gradle.docker", name = "gradle-docker")
}
}
plugins {
kotlin("plugin.jpa") version "1.3.40"
id("org.springframework.boot") version "2.1.6.RELEASE"
id("io.spring.dependency-management") version "1.0.7.RELEASE"
id("com.palantir.docker") version "0.22.1"
kotlin("jvm") version "1.3.40"
kotlin("plugin.spring") version "1.3.40"
}
group = "com.mycompany"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_1_8
val developmentOnly by configurations.creating
configurations {
runtimeClasspath {
extendsFrom(developmentOnly)
}
}
repositories {
mavenCentral()
maven(url = "https://repo.spring.io/snapshot")
maven(url = "https://repo.spring.io/milestone")
}
extra["springCloudVersion"] = "Greenwich.SR1"
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-batch")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
//reactor
implementation("io.projectreactor:reactor-core")
implementation("org.springframework.boot:spring-boot-starter-webflux")
implementation("org.springframework.data:spring-data-jdbc:1.0.0.r2dbc-SNAPSHOT")
implementation("org.springframework.data:spring-data-r2dbc:1.0.0.M1")
implementation("io.r2dbc:r2dbc-spi:1.0.0.M5")
implementation("io.r2dbc:r2dbc-postgresql:1.0.0.M6")
developmentOnly("org.springframework.boot:spring-boot-devtools")
runtimeOnly("org.postgresql:postgresql")
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("io.projectreactor:reactor-test")
testImplementation("org.springframework.batch:spring-batch-test")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
exclude(group = "junit", module = "junit")
}
}
dependencyManagement {
imports {
mavenBom("org.springframework.cloud:spring-cloud-dependencies:${property("springCloudVersion")}")
mavenBom("io.projectreactor:reactor-bom:Bismuth-RELEASE")
}
}
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
}
}
task<Copy>("unpack") {
dependsOn(tasks.getByName("bootJar"))
from(zipTree(tasks.getByName("bootJar").outputs.files.singleFile))
into("build/dependency")
}
I figured it out by deleting each dependencies I have.
The problem was
implementation("org.springframework.boot:spring-boot-starter-web")
After deleting this, It worked. Maybe above dependency has it's own dependencies on WebMvc but I'm not sure.

Resources