Home > Blockchain >  Failed to configure data source from application.yml in SpringBoot Kotlin
Failed to configure data source from application.yml in SpringBoot Kotlin

Time:02-05

I am using application.yml file to configure data source. When I run the jar file, I get the exception. 



It works fine if I run project in IntelliJ (run button)

Command I use to build and run jar

./gradlew build -x check   

java -jar build/libs/UrlShortener-0.0.1-SNAPSHOT-plain.jar

application.yml

spring:
  datasource:
    url: jdbc:postgresql://localhost:5332/dbName
    username: username
    password: password
    driverClassName: org.postgresql.Driver

Exception

05:13:50.339 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class

build.gradle.kts

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
    id("org.springframework.boot") version "3.0.2"
    id("io.spring.dependency-management") version "1.1.0"
    kotlin("jvm") version "1.7.22"
    kotlin("plugin.spring") version "1.7.22"
    kotlin("plugin.jpa") version "1.7.22"
}

group = "url.shortener"
version = "0.0.1-SNAPSHOT"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
    mavenCentral()
}

tasks.withType<Jar> {
    manifest {
        attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
    }
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    from({
        configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
    })

}

dependencies {
    implementation("org.springframework.boot:spring-boot-starter-data-jpa")
    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")
    implementation("org.postgresql:postgresql")
    val kotlinxHtmlVersion = "0.8.0"
    implementation("org.jetbrains.kotlinx:kotlinx-html-jvm:$kotlinxHtmlVersion")
    testImplementation("org.springframework.boot:spring-boot-starter-test")
}

tasks.withType<KotlinCompile> {
    kotlinOptions {
        freeCompilerArgs = listOf("-Xjsr305=strict")
        jvmTarget = "17"
    }
}

tasks.withType<Test> {
    useJUnitPlatform()
}

But if I configure through kotlin, it works

@Configuration
class Config {

    @Bean
    fun getDataSource(): DataSource {
        val dataSourceBuilder = DataSourceBuilder.create()
        dataSourceBuilder.driverClassName("org.postgresql.Driver")
        dataSourceBuilder.url("jdbc:postgresql://localhost:5332/dbName")
        dataSourceBuilder.username("username")
        dataSourceBuilder.password("password")
        return dataSourceBuilder.build()

    }
}

CodePudding user response:

taking out your customisation for jar seems to resolve the issue.

//tasks.withType<Jar> {
//  manifest {
//      attributes["Main-Class"] = "url.shortener.UrlShortenerApplicationKt"
//  }
//  duplicatesStrategy = DuplicatesStrategy.EXCLUDE
//  from({
//      configurations.runtimeClasspath.get().filter { it.name.endsWith("jar") }.map { zipTree(it) }
//  })
//
//}
  • Related