我想从avro schemas/avsc文件转换Java类,为此,我尝试从Baeldung的文章和StackOverflow的答案实现SpecificCompilerTool的解决方案,但面临依赖问题。
以下是“gradle build”命令时的异常。 (在构建之前执行了“gradle clean”,成功了,也尝试了无效缓存并重新启动 IDE)
例外:
2024-06-17T11:02:12.836+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] FAILURE: Build failed with an exception.
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * What went wrong:
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] Execution failed for task ':bootJar'.
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > 'void org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.putArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)'
2024-06-17T11:02:12.837+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter]
2024-06-17T11:02:12.838+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] * Try:
2024-06-17T11:02:12.838+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Run with --stacktrace option to get the stack trace.
2024-06-17T11:02:12.838+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Run with --scan to get full insights.
2024-06-17T11:02:12.838+0530 [ERROR] [org.gradle.internal.buildevents.BuildExceptionReporter] > Get more help at https://help.gradle.org.
2024-06-17T11:02:12.839+0530 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger]
2024-06-17T11:02:12.839+0530 [ERROR] [org.gradle.internal.buildevents.BuildResultLogger] BUILD FAILED in 15s
无法找到方法“void org.apache.commons.compress.archivers.zip。ZipArchiveOutputStream.putArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)” '无效 org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream.putArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry)'
Gradle 的依赖项缓存可能已损坏(这有时会在网络连接超时后发生。)
重新下载依赖并同步项目(需要网络) Gradle 构建进程(守护进程)的状态可能已损坏。停止所有 Gradle 守护进程可能会解决这个问题。
停止 Gradle 构建进程(需要重新启动) 您的项目可能使用了第三方插件,该插件与项目中的其他插件或项目请求的 Gradle 版本不兼容。
build.gradle:
buildscript {
dependencies {
// Add the Avro code generation to the build dependencies so that it can be used in a Gradle task.
classpath group: 'org.apache.avro', name: 'avro-tools', version: '1.11.0'
}
}
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.0'
id 'io.spring.dependency-management' version '1.1.5'
id 'application'
id "org.sonarqube" version "5.0.0.4638"
id "jacoco"
}
group = 'com.sample'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21
repositories {
mavenCentral()
maven {
url "https://packages.confluent.io/maven"
}
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
//implementation 'org.apache.commons:commons-compress:1.20' // Downgrade commons-compress version
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.kafka:spring-kafka'
implementation ('org.apache.avro:avro:1.11.0') {
//exclude group: 'org.apache.commons', module: 'commons-compress'
} // Exclude commons-compress from avro
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.kafka:spring-kafka-test'
testImplementation 'org.springframework.security:spring-security-test'
}
tasks.register('avroCodeGeneration') {
inputs.dir("src/main/resources/avro")
outputs.dir("src/main/java/avro")
doLast {
new org.apache.avro.tool.SpecificCompilerTool().run(System.in, System.out, System.err, List.of(
"-encoding", "UTF-8",
"-string",
"-fieldVisibility", "private",
"-noSetters",
"schema", "${projectDir}/src/main/resources/avro".toString(), "${projectDir}/src/main/java/avro".toString()
))
}
}
tasks.withType(JavaCompile) {
dependsOn('avroCodeGeneration')
}
tasks.named('test') {
useJUnitPlatform()
}
我能够在 IDE 的“外部库”部分下找到 org.apache.commons 库。另外,我能够在其下找到类“ZipArchiveOutputStream”和方法“putArchiveEntry”,这意味着依赖项已下载。 但不知何故,它在构建过程中显示错误:“找不到方法”。
当我删除 avro 依赖项时,我的项目获得 成功构建。但是在生成的类中,有 org.apache.avro 的导入,例如,
import org.apache.avro.generic.GenericArray;
import org.apache.avro.util.Utf8;
因此,我的代码在这些行上给出了错误,因为它无法找到该依赖项,因为它不存在于 build.gradle 中。 我尝试过在 avro 依赖项下“排除压缩”,认为它可能来自 avro 和 avro-tools,但它不起作用。
我尝试检查依赖关系树,并且“avro compress”依赖关系仅存在于avro下。它不是任何其他依赖项下的子依赖项。
我还在依赖项下添加了“org.apache.commons:commons-compress:1.20”,假设它将覆盖所有其他“压缩子依赖项”(如果有),但出现相同的错误。
我不确定,是否是因为某种含糊不清。
请帮忙。预先感谢。
IntelliJ IDE。 等级:8.7 Java 21
注意:我无法使用 OpenSource lib,因为它终止了对 java 版本 19 及更高版本的支持。
我遇到了类似的问题,可能是由于 Spring Boot 和 Avro Tools 之间的冲突造成的。 虽然我找不到直接的解决方案,但以下是对我有帮助的解决方法:
为生成的 Avro 类创建库: 在您的库项目中,添加 Avro Tools 依赖项:
仅编译“org.apache.avro:avro-tools:1.11.3”
将库包含在您的 Spring Boot 应用程序中:
在库中生成 Avro 类后,将此库添加为 Spring Boot 应用程序中的依赖项。
这种方法将 Avro 代码生成与主应用程序分开,避免潜在的冲突并使构建过程更清晰。