I am struggling to update an existing Java 8 and JAXB Gradle build that generates Java objects from an XSD to Java 17 and Jakarta. How would I update the following Gradle task to use Jakarta? Is there an XJC Jakarta task? I can't seem to find a reference to one.
Here is my build.gradle:
plugins {
id 'java'
}
defaultTasks 'clean', 'compileJava'
project.ext {
jaxbTargetDir = "xsd_src"
schemaSrc = "xsd_v1.0"
schemaName = "schema_20180419.xsd"
}
sourceSets {
main {
// use ant generated src and extra source
java {
srcDir "${jaxbTargetDir}"
srcDir 'extra_src'
}
// include the src in the jar
resources {
srcDir "${schemaSrc}"
}
}
}
tasks.withType(JavaCompile).configureEach {
options.fork = true // Fork your compilation into a child process
options.forkOptions.setMemoryMaximumSize("4g") // Set maximum memory to 4g
}
configurations {
xsd2Java
}
dependencies {
// Java EE Dependencies
xsd2Java('jakarta.xml.bind:jakarta.xml.bind-api:4.0.0')
xsd2Java('jakarta.activation:jakarta.activation-api:2.0.0')
xsd2Java('jakarta.annotation:jakarta.annotation-api:2.1.0-B1')
implementation 'org.javassist:javassist:3.29.0-GA'
// Versioning comes from Maven Jaxb2 Plugin
implementation 'org.jvnet.jaxb:jaxb2-basics:2.0.5'
implementation 'org.jvnet.jaxb:jaxb2-basics-ant:2.0.5'
implementation 'org.jvnet.jaxb:jaxb-basics-annotate:2.0.5'
implementation 'org.jvnet.jaxb:jaxb2-basics-runtime:2.0.5'
implementation 'org.glassfish.jaxb:jaxb-runtime:4.0.1'
implementation 'org.glassfish.jaxb:jaxb-xjc:4.0.0-M4'
compileOnly 'org.slf4j:slf4j-simple:1.7.28'
}
task xsd2Java() {
doLast {
ant.taskdef(name: 'xjc', classname: 'org.jvnet.jaxb2_commons.xjc.XJC2Task', classpath: configurations.compileClasspath.asPath)
ant.jaxbTargetDir = file("${jaxbTargetDir}")
ant.xjc(
extension: true,
destdir: '${jaxbTargetDir}',
binding: file("${schemaSrc}/bindingsv.xjb"),
) {
schema(dir: "${schemaSrc}") {
include(name: "${schemaName}")
}
arg(line: "-nv -extension -xmlschema -Xannotate -XtoString -Xequals -XhashCode -Xcopyable")
// check the timestamps on xsds and java files to see if we need to regenerate
depends(dir: "${schemaSrc}") {
include(name: "**/*.xsd")
}
produces(dir: "${jaxbTargetDir}") {
include(name: "**/*.java")
}
}
}
}
compileJava.dependsOn 'xsd2Java'
I've also updated the bindingsv.xjb to reference Jakarta:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
xmlns:jxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="https://jakarta.ee/xml/ns/jaxb/xjc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:annox="http://annox.dev.java.net"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
http://annox.dev.java.net"
jxb:extensionBindingPrefixes="annox xjc" version="3.0">
However when I run the build I get this error:
Execution failed for task ':Services:messages:xsd2Java'.
> java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException
How can I solve this error?
I don't know if that would totally solve your problem but you're using jaxb2-basics v2 with jakarta build, which is unsupported.
You should migrate to v4 version (4.0.8 latest at this moment). See migration guide on how to go from v2 to v3 (and then from v3 to v4) here
// Versioning comes from Maven Jaxb2 Plugin, replaced by v4 equivalent in same order
implementation 'org.jvnet.jaxb:jaxb-plugins:4.0.8'
implementation 'org.jvnet.jaxb:jaxb-plugins-ant:4.0.8'
implementation 'org.jvnet.jaxb:jaxb-plugin-annotate:4.0.8'
implementation 'org.jvnet.jaxb:jaxb-plugins-runtime:4.0.8'