如何使用springboot + gradle + convention + libs.versions.toml + kts构建多模块项目?

xxhby3vn  于 2023-03-30  发布在  Spring
关注(0)|答案(1)|浏览(290)

我的模块结构:

--project
  --build-logic
    --convention
  --feature
    --article
      --database

build-logic/convention/build.gradle.kts

plugins {
    `kotlin-dsl`
}

dependencies{
    compileOnly(libs.kotlin.gradle.plugin)
}

gradlePlugin {
    plugins {
        register("springboot"){
            id = "sage.springboot"
            implementationClass = "SpringbootConventionPlugin"
        }
    }
}

build-logic/convention/src/main/kotlin/com/sage/server/SpringbootConventionPlugin.kt

package com.sage.server

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.artifacts.VersionCatalogsExtension
import org.gradle.kotlin.dsl.getByType

class SpringbootConventionPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            val libs = project.extensions.getByType<VersionCatalogsExtension>().named("libs")
            pluginManager.apply(libs.findPlugin("kotlin-jvm").get().get().pluginId)
            pluginManager.apply(libs.findPlugin("kotlin-spring").get().get().pluginId)
            pluginManager.apply(libs.findPlugin("spring-framework-boot").get().get().pluginId)
            pluginManager.apply(libs.findPlugin("spring-dependency-management").get().get().pluginId)
        }
    }
}

但我在使用自定义插件时收到一个错误:feature/article/database/build.gradle.kts

plugins {
    id("sage.springboot")
}

错误:

Caused by: org.gradle.api.plugins.InvalidPluginException: Could not find implementation class 'SpringbootConventionPlugin' for plugin 'sage.springboot' specified in jar:file:/D:/Software/.gradle/caches/jars-9/ceb3f2c77f3429950687bbe83d9848a4/convention.jar!/META-INF/gradle-plugins/sage.springboot.properties.

我哪里做错了?
我在网上搜索了很久,没有一个例子使用下面的技术栈springboot + gradle + convention + libs.versions.toml + kts,能不能提供一些资源或者代码示例?对于刚接触gradle的人来说,没有相关资源确实很难继续。

第一次编辑

build-logic/convention/src/main/kotlin/com/sage/server/SpringbootConventionPlugin.kt更改为build-logic/convention/src/main/kotlin/SpringbootConventionPlugin.kt
固定Could not find implementation class 'SpringbootConventionPlugin'
模块core:api

plugins {
    id("sage.springboot")
}

dependencies {
    api(libs.spring.boot.starter.web)
}

模块feature:article:api

plugins {
    id("sage.springboot")
}

dependencies{
    api(project(":core:api"))
    implementation(project(":feature:article:service"))
}

我使用了api()而不是implementation()但依赖项没有传递
feature:article:api依赖于core:apicore:api依赖于libs.spring.boot.starter.web,但feature:article:api不依赖于libs.spring.boot.starter.web
您可以在这里查看完整的code(也许您需要禁用maven镜像存储库)

kqlmhetl

kqlmhetl1#

在@aSemy的帮助下,我的springboot项目已经能够成功运行。
我通过重命名子项目解决了dependencies were not passed问题。

rootProject.name = "server"
include("core:api")
project(":core:api").name = "core-api"
include("feature:article:api")
project(":feature:article:api").name = "article-api"

这不是最佳实践。2您可以优化此子项目的名称。
您可以在这里查看完整的code(可能需要禁用maven镜像库)。

相关问题