Trying to create beans from XSD files with toString methid using JAXB2 basics plugin but receiving error "unknown parameter -XtoString"
Hello,
I would like to generate java classes with toString-methods with the JAXB2 plugin. My code is currently looking like this:
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")
}
}
and an example XSD looks like this
<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>
I am receiving the error
Execution failed for task ':generateBeans'.
> unknown parameter -XtoString
I already consulted the documentation as found here but I couldn't find any intro on how to use the plugin with gradle.
EDIT: I wonder why arg(line: "-Xlocator")
does work though.
We are migrating from a far older version of JAXB. Is it outdated?
Thanks and best wishes
Solution found. I hope this helps:
The jaxb2-basics plugin is also deprecated. The detailed migration can be found here.
The code now looks like this:
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'
implementation 'org.jvnet.jaxb:jaxb-plugins:4.0.8' // Required for the generated Java classes' dependencies (e.g. strategies)
// 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")
}
}