尝试使用 JAXB2 基础插件使用 toString 方法从 XSD 文件创建 bean,但收到错误“未知参数 -XtoString”
你好,
我想使用 JAXB2 插件生成带有 toString 方法的 java 类。我的代码目前看起来像这样:
plugins {
id 'java'
id 'org.unbroken-dome.xjc' version '2.0.0'
}
group = 'com.example'
version = '1.0.0'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21)) // Set Java version
}
}
repositories {
mavenCentral()
}
dependencies {
// Jakarta XML Bind API
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.2'
// XJC for generating beans
annotationProcessor 'org.glassfish.jaxb:jaxb-xjc:4.0.2'
// https://mvnrepository.com/artifact/org.jvnet.jaxb2_commons/jaxb2-basics
implementation 'org.jvnet.jaxb2_commons:jaxb2-basics:0.13.1'
implementation 'org.jvnet.jaxb2_commons:jaxb2-basics-runtime:0.13.1'
}
def schemaDir = file("src/main/resources/xsd") // Directory for XSD files
def outputDir = file("$buildDir/generated-sources/xjc") // Directory for generated classes
// New version of legacy task (see below)
// Task to generate Java beans from XSDs
task generateBeans {
group = "code generation"
description = "Generates Java Beans from XSD schemas"
doFirst {
delete outputDir
mkdir outputDir
println("Before XJC task")
}
doLast {
println("Start processing XSD files")
ant.taskdef(name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.annotationProcessor.asPath)
fileTree(schemaDir).matching { include '**/*.xsd' }.files.each { xsdFile ->
logger.lifecycle("Generating Java classes for schema: ${xsdFile}")
println("Generating Java classes for schema: ${xsdFile}")
println "Processing XSD: ${xsdFile}"
def targetPackage = 'com.example.generated'
ant.xjc(destdir: outputDir,
package: targetPackage,
extension: true) {
schema(file: xsdFile)
arg(value: '-XtoString') // Aktiviert das ToString-Plugin
}
}
println("Finished processing XSD files")
}
}
XSD 示例如下所示
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="person">
<xs:complexType>
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:int"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我收到错误消息
Execution failed for task ':generateBeans'.
> unknown parameter -XtoString
我已经查阅了here找到的文档,但我找不到任何关于如何在 gradle 中使用该插件的介绍。
编辑:我想知道为什么
arg(line: "-Xlocator")
确实有效。
我们正在从 JAXB 的旧版本迁移。已经过时了吗
谢谢并致以最美好的祝愿
找到解决方案。我希望这有帮助:
jaxb2-basics 插件也已弃用。详细的迁移可以在这里找到。
代码现在看起来像这样:
plugins {
id 'java'
}
group = 'com.example'
version = '1.0.0'
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(21))
}
}
repositories {
mavenCentral()
}
configurations {
annotationProcessor
}
dependencies {
// Jakarta XML Bind API
implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.2'
// annotationProcessor configuration:
// XJC for generating beans
annotationProcessor 'org.glassfish.jaxb:jaxb-xjc:4.0.2'
// JAXB2 plugins (formerly: JAXB2 basics plugin) (used for ant task below)
annotationProcessor 'org.jvnet.jaxb:jaxb-plugins:4.0.8'
// requires
annotationProcessor 'org.jvnet.jaxb:jaxb-plugins-runtime:4.0.8'
//annotationProcessor 'org.jvnet.jaxb:jaxb-plugins-tools:4.0.8'
//requires:
annotationProcessor 'org.glassfish.jaxb:jaxb-runtime:4.0.2'
//Without jaxb-runtime, you'll get: Caused by: java.lang.ClassNotFoundException: org.glassfish.jaxb.runtime.v2.ContextFactory
//annotationProcessor 'con.sun.xml.bind:jaxb-impl:4.0.4'
}
def schemaDir = file("src/main/resources/xsd") // Directory for XSD files
def outputDir = file("$buildDir/generated-sources/xjc") // Directory for generated classes
task generateBeans {
group = "code generation"
description = "Generates Java Beans from XSD schemas"
doFirst {
delete outputDir
mkdir outputDir
println("Before XJC task") // Debugging
}
doLast {
println("Start processing XSD files") // Debugging
ant.taskdef(name: 'xjc',
classname: 'com.sun.tools.xjc.XJCTask',
classpath: configurations.annotationProcessor.asPath)
fileTree(schemaDir).matching { include '**/*.xsd' }.files.each { xsdFile ->
logger.lifecycle("Generating Java classes for schema: ${xsdFile}")
println("Generating Java classes for schema: ${xsdFile}")
println "Processing XSD: ${xsdFile}"
def targetPackage = 'com.example.generated'
ant.xjc(destdir: outputDir,
package: targetPackage,
extension: true) {
schema(file: xsdFile)
arg(line: "-XtoString")
arg(line: "-Xequals")
arg(line: "-XhashCode")
arg(line: "-Xlocator")
//arg(value: '-XtoString') // Never use an argument twice, the generated class will have redundant methods!
}
}
println("Finished processing XSD files")
}
}