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

Gradle:约定插件共享时因缺失类导致构建失败

问题:Gradle插件项目中jsonschema2pojo依赖缺失导致编译失败

错误原因

你的问题出在插件构建项目的依赖范围上。jsonschema2pojo-gradle-plugin仅包含插件的Gradle集成代码,但其依赖的核心库jsonschema2pojo-core未被加入插件构建类路径,导致编译插件项目时找不到org.jsonschema2pojo.GenerationConfig父类。

解决方案

1. 修复插件项目的依赖配置

修改gradle/plugins/micronaut-plugins/build.gradle.kts,添加jsonschema2pojo-core依赖,确保编译插件时能获取到所有必要类:

plugins {
    `kotlin-dsl`
}

repositories {
    gradlePluginPortal()
    mavenCentral() // jsonschema2pojo-core发布在Maven Central
}

dependencies {
    implementation("io.micronaut.gradle:micronaut-gradle-plugin:3.7.0")
    implementation("org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:1.1.3")
    // 添加核心库依赖,解决父类找不到的问题
    implementation("org.jsonschema2pojo:jsonschema2pojo-core:1.1.3")
    implementation("com.github.spotbugs.snom:spotbugs-gradle-plugin:5.0.13")
}

2. 在my-app插件中统一配置jsonschema2pojo

修改gradle/plugins/micronaut-plugins/src/main/kotlin/my-app.gradle.kts,启用插件并定义通用配置,减少子项目重复代码:

import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.project
import org.jsonschema2pojo.gradle.JsonSchemaExtension

plugins {
    id("my-base")
    id("io.micronaut.application")
    id("org.jsonschema2pojo")
}

repositories {
    mavenCentral()
}

// 统一配置通用属性,子项目可按需覆盖
configure<JsonSchemaExtension> {
    setAnnotationStyle("jackson2")
    formatTypeMapping = hashMapOf(
        "uuid" to "java.lang.String"
    )
}

dependencies {
    implementation(project(":core"))
    implementation("io.micronaut:micronaut-jackson-databind")
    implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.14.2")

    testImplementation("org.testcontainers:junit-jupiter:1.17.6")
    testImplementation("org.testcontainers:testcontainers:1.17.6")
    testImplementation("org.testcontainers:kafka:1.17.6")
    testImplementation("org.awaitility:awaitility:4.1.1")
    testImplementation("io.micronaut:micronaut-http-client")
}

tasks.dockerfile {
    baseImage.set("ghcr.io/graalvm/jdk:ol9-java17-22.3.0")
}

tasks.dockerBuild {
    images.add("${project.name}:${project.version}")
}

3. 简化app子项目的配置

修改app1/build.gradle.kts,移除重复插件声明,只保留项目特有配置:

plugins {
    id("my-app")
    id("io.micronaut.test-resources") version "3.7.0"
}

application {
    mainClass.set("app1.Application")
}

// 仅配置当前项目特有的属性,通用配置已在my-app中定义
jsonSchema2Pojo {
    setSource(files("src/main/resources/jsonschema"))
    targetPackage = "app1.outgoing"
    targetDirectory = file(layout.buildDirectory.dir("generated-sources/jsonschema2pojo"))
}

关键说明

  • 插件项目编译时需要直接访问jsonschema2pojo-core中的类,因此必须将其添加到implementation依赖
  • 在my-app插件中统一配置通用属性,符合Gradle约定优于配置的设计思路,减少子项目重复代码
  • 子项目仅需覆盖自身特有的配置,保持代码简洁易维护

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 02:35:26