Java xsd create class

Java xsd create class

Recently I was struggling with a task of generating model classes from XML schema (XSD) using Java 11 and Maven. I was really annoyed that jaxb2-maven-plugin was not updated for so long to cooperate properly with Java versions like 9, 10 or 11 (see my older posts: “jaxb2-maven-plugin 2.4 and Java 10” and “jaxb2-maven-plugin 2.4 and Java 11”). Obviously guys from MojoHaus Project don’t have enough capacity. Anyway I found a great alternative for jaxb2-maven-plugin which is fully ready for Java 11:

I will repeat: this is the only Maven plugin right now able to generate Java classes from XSD file under Java 11 right out of the box. No workarounds needed!

pom.xml

  4.0.0 . . 1.0-SNAPSHOT jar UTF-8 11 11  . javax.xml.bind jaxb-api 2.3.0  org.glassfish.jaxb jaxb-runtime 2.3.2 runtime  com.sun.activation jakarta.activation 1.2.1 runtime     org.apache.maven.plugins maven-compiler-plugin 3.8.0 11   org.apache.maven.plugins maven-surefire-plugin 2.22.0 --illegal-access=permit   org.apache.cxf cxf-xjc-plugin 3.3.0  xjc generate-sources xsdtojava    $/src/main/xsd/your.xsd your.package          

It works great using Java 11 (openjdk 11.0.2 2019-01-15) and Maven 3.5.2! It’s a drop-in replacement for jaxb2-maven-plugin but it needs no workarounds for Java 11! Good bye jaxb2-maven-plugin, welcome cxf-xjc-plugin! 🙂

P.S.

Those used to jaxb2-maven-plugin should note one difference in configuration of cxf-xjc-plugin: paths to XSD files are relative to the build directory (target). Thus the easiest approach is to start XSD files paths with $ .

Share this:

Like this:

About krzysztoftomaszewski

I’ve got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I’m working on computer software design and engineering continuously since 2004.

Читайте также:  Java jdk development tools

23 Responses to Generating classes from XSD under Java 11 – the right way

Thank you man Its working for me.

org.apache.maven.plugins
maven-surefire-plugin
2.22.0 ”
the above plugin not required in my code

Hi, am getting Error inside systemOut parser (org.apache.cxf:cxf-xjc-plugin:3.3.0:xsdtojava:xjc:generate-sources)
org.apache.maven.plugin.MojoExecutionException: Error inside systemOut parser
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo.execute(AbstractXSDToJavaMojo.java:289)
at org.apache.cxf.maven_plugin.XSDToJavaMojo.execute(XSDToJavaMojo.java:41)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:333)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.lambda$7(MavenImpl.java:1380)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112)
at org.eclipse.m2e.core.internal.embedder.MavenImpl.execute(MavenImpl.java:1379)
at org.eclipse.m2e.core.project.configurator.MojoExecutionBuildParticipant.build(MojoExecutionBuildParticipant.java:52)
at org.eclipse.m2e.core.internal.builder.MavenBuilderImpl.build(MavenBuilderImpl.java:137)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:173)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$1.method(MavenBuilder.java:1)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1$1.call(MavenBuilder.java:116)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:112)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod$1.call(MavenBuilder.java:106)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.executeBare(MavenExecutionContext.java:177)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:151)
at org.eclipse.m2e.core.internal.embedder.MavenExecutionContext.execute(MavenExecutionContext.java:99)
at org.eclipse.m2e.core.internal.builder.MavenBuilder$BuildMethod.execute(MavenBuilder.java:87)
at org.eclipse.m2e.core.internal.builder.MavenBuilder.build(MavenBuilder.java:201)
at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:833)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager._basicBuild(BuildManager.java:220)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:147)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:263)
at org.eclipse.core.internal.events.BuildManager$1.run(BuildManager.java:316)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:319)
at org.eclipse.core.internal.events.BuildManager.basicBuildLoop(BuildManager.java:371)
at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:392)
at org.eclipse.core.internal.events.AutoBuildJob.doBuild(AutoBuildJob.java:154)
at org.eclipse.core.internal.events.AutoBuildJob.run(AutoBuildJob.java:244)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error inside systemOut parser
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo.runForked(AbstractXSDToJavaMojo.java:593)
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo.run(AbstractXSDToJavaMojo.java:374)
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo.execute(AbstractXSDToJavaMojo.java:276)
… 33 more
Caused by: org.codehaus.plexus.util.cli.CommandLineException: Error inside systemOut parser
at org.codehaus.plexus.util.cli.CommandLineUtils.executeCommandLine(CommandLineUtils.java:222)
at org.codehaus.plexus.util.cli.CommandLineUtils.executeCommandLine(CommandLineUtils.java:107)
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo.runForked(AbstractXSDToJavaMojo.java:590)
… 35 more
Caused by: java.lang.NullPointerException
at org.sonatype.plexus.build.incremental.DefaultBuildContext.addMessage(DefaultBuildContext.java:113)
at org.sonatype.plexus.build.incremental.ThreadBuildContext.addMessage(ThreadBuildContext.java:95)
at org.apache.cxf.maven_plugin.AbstractXSDToJavaMojo$2.consumeLine(AbstractXSDToJavaMojo.java:561)

I see that the last “caused by” exception is NullPointerException. I guess something is missing in your configuration of this plugin. I can see in GitHub repository for this plugin there was a fix for NPE related to the same source file (AbstractXSDToJavaMojo.java):
https://github.com/apache/cxf-xjc-utils/commit/069b82bca041e06f051342886f0082a8534a26b4#diff-f4577ef9208bf060d987738171540a2083a24ed6e3090a74feb044e7038d4a9d And now there is a newer version of the plugin available: 3.3.1
https://mvnrepository.com/artifact/org.apache.cxf/cxf-xjc-plugin/3.3.1 You should try this newer version.

I have 700+ xsd files to convert, divided in some subfolders. It doesn’t seems practical to list everyone manually in the pom.xml. I already tried the “xsdDir”, but it’s bugged. Any tips?

But still, the generated classes import classes from the javax.xml package instead of jakarta.xml. Is there the expected behaviour or did I miss something?

Читайте также:  Python parameter self unfilled

This is just about generating Java classes that represent XML data structures according to the given schema. These generated classes have annotations which come from packages like `javax.xml.bind`. So yes, there are imports from such packages and this was expected. These classes are prepared to cooperate with JAXB.

Thanks for your answer. I used maven-antrun-plugin to search-and-replace “javax.xml.bind” with “jakarta.xml.bind”. Really not elegant but it works.

Hi, it works fine with your configuration. when i add bindings.xml $/jaxbbindings.xml I get the below error –add-exports=jdk.xml.dom/org.w3c.dom.html=ALL-UNNAMED –add-exports=java.xml/com.sun.org.apache.xerces.internal.impl
.xs=ALL-UNNAMED –add-opens java.base/java.security=ALL-UNNAMED –add-opens java.base/java.net=ALL-UNNAMED –add-opens java.base/java.lang=ALL-UNNAMED –add-opens java.base/java.util=ALL-UNNAMED –add-opens
java.base/java.util.concurrent=ALL-UNNAMED

This is probably a silly question, but after generating the java classes which represent the XML structure, how do I then use them to output XML documents? In the rootXml class generated I don’t see an “exportToXml” method or something equivalent. I am a newbie to JAXB so pardon my ignorance.

4.0.0
LDR
LDR
1.0-SNAPSHOT
jar UTF-8
11
11 org.apache.cxf
cxf-xjc-plugin
3.3.1 javax.xml.bind
jaxb-api
2.3.0 org.glassfish.jaxb
jaxb-runtime
2.3.2
runtime com.sun.activation
jakarta.activation
1.2.1
runtime org.apache.cxf
cxf-xjc-plugin
3.3.1 generate-sources
generate-sources xsdtojava $/src/main/java $/src/main/xsd/cmms.xsd
com.cdc.cpc.v2 FOR THE ABOVE XSD: I AM GETTING THE BELOW ERROR: C:\PROJECT\Caterpillar\WS\CXF>mvn -e cxf-xjc-plugin:xsdtojava
[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects…
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-metadata.xml (14 kB at 14 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/maven-metadata.xml (20 kB at 15 kB/s)
[INFO] ————————————————————————
[INFO] BUILD FAILURE
[INFO] ————————————————————————
[INFO] Total time: 2.108 s
[INFO] Finished at: 2021-11-20T15:05:59+05:30
[INFO] ————————————————————————
[ERROR] No plugin found for prefix ‘cxf-xjc-plugin’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\ladevendrareddy\.m2\repository), central (https://repo.maven.apache.org/maven2)%5D -> [Help 1]
org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException: No plugin found for prefix ‘cxf-xjc-plugin’ in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (C:\Users\ladevendrareddy\.m2\repository), central (https://repo.maven.apache.org/maven2)%5D
at org.apache.maven.plugin.prefix.internal.DefaultPluginPrefixResolver.resolve (DefaultPluginPrefixResolver.java:95)
at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.findPluginForPrefix (MojoDescriptorCreator.java:266)
at org.apache.maven.lifecycle.internal.MojoDescriptorCreator.getMojoDescriptor (MojoDescriptorCreator.java:220)
at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments (DefaultLifecycleTaskSegmentCalculator.java:104)
at org.apache.maven.lifecycle.internal.DefaultLifecycleTaskSegmentCalculator.calculateTaskSegments (DefaultLifecycleTaskSegmentCalculator.java:83)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:89)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
at org.apache.maven.cli.MavenCli.execute (MavenCli.java:972)
at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
[ERROR]
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException

Читайте также:  Stored procedure execute java

This is a pretty bad solution as it does not support importing many xsd files. One would have to list them all one by one.

Источник

Оцените статью