You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Quarkus-Kotlin跨项目读取配置及本地依赖添加咨询

问题解答

一、在common-lib中读取notifications项目的数据库配置

基于Spring Boot的配置绑定机制(从application.yaml配置文件推断使用Spring Boot),可以按以下步骤实现:

  1. 给common-lib添加必要依赖
    在common-lib的build.gradle.kts中引入Spring Boot基础配置相关依赖:
dependencies {
    implementation("org.springframework.boot:spring-boot-starter")
    // 可选,用于IDE提供配置项自动提示
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}
  1. 创建配置绑定类
    在common-lib中定义数据类,通过@ConfigurationProperties绑定datasource前缀的配置项:
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.stereotype.Component

@Component
@ConfigurationProperties(prefix = "datasource")
data class DataSourceProperties(
    var url: String = "",
    var driver: String = "",
    var username: String = "",
    var password: String = ""
)

该类会自动从notifications项目的application.yaml中读取对应配置值。

  1. 在common-lib中使用配置
    在common-lib的业务类中通过构造函数注入DataSourceProperties即可使用配置:
import org.springframework.stereotype.Service

@Service
class CommonDbService(
    private val dataSourceProps: DataSourceProperties
) {
    fun printDbInfo() {
        println("数据库地址: ${dataSourceProps.url}")
        println("用户名: ${dataSourceProps.username}")
    }
}

二、本地开发时给notifications项目添加common-lib依赖

有两种常用方式,可根据开发场景选择:

方式1:发布到本地Maven仓库(推荐稳定开发场景)

  • 给common-lib配置Maven发布插件:
    在common-lib的build.gradle.kts中添加:
    plugins {
        `maven-publish`
        kotlin("jvm") version "你的Kotlin版本"
        id("org.springframework.boot") version "你的Spring Boot版本"
    }
    
    publishing {
        publications {
            create<MavenPublication>("maven") {
                from(components["java"])
            }
        }
    }
    
  • 执行Gradle任务publishToMavenLocal,将common-lib发布到本地Maven仓库。
  • 在notifications的build.gradle.kts中添加依赖:
    dependencies {
        implementation("你的项目groupId:common-lib:你的版本号")
    }
    
    注意groupId和version要与common-lib中group、version的配置一致。

方式2:直接引入本地项目(适合频繁调试common-lib)

如果需要频繁修改common-lib并实时同步到notifications项目:

  • 在notifications的settings.gradle.kts中添加common-lib的本地路径:
    include(":common-lib")
    project(":common-lib").projectDir = file("../common-lib") // 替换为你的common-lib实际路径
    
  • 在notifications的build.gradle.kts中添加项目依赖:
    dependencies {
        implementation(project(":common-lib"))
    }
    
    修改common-lib的代码后,notifications项目会自动同步依赖变更,无需手动发布。

内容的提问来源于stack exchange,提问作者Anshul Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 21:10:27