How to use jaxb2-annotate-plugin with XJC in command line

空扰寡人 提交于 2019-11-28 09:08:47

问题


I'm building java classes based on an xsd using "jaxb-ri" in command line (xjc.bat). I want to set a namespace in an XmlType annotation using jaxb2-annotate_plugin but I don't know how to add this plugin to xjc classpath and acitvate it.

here is my external bindings.xjb file :

<jxb:bindings xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
              xmlns:xs="http://www.w3.org/2001/XMLSchema"
              xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
              xmlns:annox="http://annox.dev.java.net"
              jxb:extensionBindingPrefixes="xjc annox"
              version="2.1">
    <jxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
        <jxb:bindings node="xs:complexType[@name='MyType]">
            <annox:annotateClass>@javax.xml.bind.annotation.XmlType(namespace="urn:myschematwo")</annox:annotateClass>
        </jxb:bindings>
    </jxb:bindings>
</jxb:bindings>

My command line is :

D:/jaxb-ri/bin/xjc.bat -p "com.jaxb.mypackage" -d
"D:/myproject/src/main/java" D:/myschema.xsd -b "D:/bindings.xjb"
-extension

When I execute this command, I have an error message saying that "http://annox.dev.java.net" is not supported.

Thanks in advance.


回答1:


Disclaimer: I'm the author of jaxb2-annotate-plugin.

First, I don't provide sample for command line as (as far as I know) nobody uses command line or batch scripts for schema compilation. At least up to date there were no requests for that. There are samples for Ant and Maven, the one for Ant is the closes to the command line usage.

Next, I wonder what is in xjc.bat.

If it calls xjc.exe from Java distibution, this most probably won't work with third-party plugins at all, not just with the jaxb2-annotate-plugin. The reason is that the XJC version which is distributed with Java has a renamed package: com.sun.tools.** -> com.sun.tools.internal.**. This breaks, naturally compagtibility with plugins. So please make sure xjc.bat does not call the xjc.exe contained in the Java distro.

I hope xjc.bat calls com.sun.tools.xjc.XJCFacade from JAXB RI Jars.

To execute the plugin you'll need two things:

  • Include all the required dependencies to the classpath.
  • Turn the plugin on using -Xannotate command line parameter.

These dependencies are:

                <!-- JAXB2 Basics library -->
                <include name="jaxb2-basics-*.jar"/>
                <include name="jaxb2-basics-annotate*.jar"/>
                <!-- JAXB2 Basics library dependencies -->
                <include name="annox-*.jar"/>
                <include name="javaparser-*.jar"/>
                <include name="commons-beanutils-*.jar"/>
                <include name="commons-logging-*.jar"/>
                <include name="commons-lang3-*.jar"/>

Here's the full dependency tree (with versions):

[INFO] org.jvnet.jaxb2_commons:jaxb2-basics-annotate:jar:1.0.3-SNAPSHOT
[INFO] +- org.jvnet.jaxb2_commons:jaxb2-basics-tools:jar:0.8.1:compile
[INFO] |  +- commons-beanutils:commons-beanutils:jar:1.7.0:compile
[INFO] |  \- commons-logging:commons-logging:jar:1.1.1:compile
[INFO] +- com.sun.xml.bind:jaxb-impl:jar:2.2.7:provided
[INFO] |  +- com.sun.xml.bind:jaxb-core:jar:2.2.7:provided
[INFO] |  |  +- javax.xml.bind:jaxb-api:jar:2.2.11:provided
[INFO] |  |  \- com.sun.istack:istack-commons-runtime:jar:2.16:provided
[INFO] |  \- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.12:provided
[INFO] |     \- javax.xml.bind:jsr173_api:jar:1.0:provided
[INFO] +- com.sun.xml.bind:jaxb-xjc:jar:2.2.7:provided
[INFO] +- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-testing:jar:0.9.0:test
[INFO] |  \- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:jar:0.9.0:test
[INFO] |     +- org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-core:jar:0.9.0:test
[INFO] |     |  +- com.sun.org.apache.xml.internal:resolver:jar:20050927:test
[INFO] |     |  +- org.apache.maven:maven-plugin-api:jar:2.0.9:test
[INFO] |     |  +- org.sonatype.plexus:plexus-build-api:jar:0.0.7:test
[INFO] |     |  \- org.apache.maven:maven-project:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-settings:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-profile:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-model:jar:2.0.9:test
[INFO] |     |     +- org.apache.maven:maven-artifact-manager:jar:2.0.9:test
[INFO] |     |     |  +- org.apache.maven:maven-repository-metadata:jar:2.0.9:test
[INFO] |     |     |  \- org.apache.maven.wagon:wagon-provider-api:jar:1.0-beta-2:test
[INFO] |     |     +- org.apache.maven:maven-plugin-registry:jar:2.0.9:test
[INFO] |     |     \- org.codehaus.plexus:plexus-container-default:jar:1.0-alpha-9-stable-1:test
[INFO] |     |        \- classworlds:classworlds:jar:1.1-alpha-2:test
[INFO] |     +- org.codehaus.plexus:plexus-utils:jar:1.5.15:test
[INFO] |     +- org.jvnet.jaxb2.maven2:maven-jaxb22-plugin:jar:0.9.0:test
[INFO] |     \- org.apache.maven.plugin-tools:maven-plugin-annotations:jar:3.2:test
[INFO] |        \- org.apache.maven:maven-artifact:jar:3.0:test
[INFO] +- org.jvnet.annox:annox:jar:1.0.1:compile
[INFO] |  +- org.apache.commons:commons-lang3:jar:3.2.1:compile
[INFO] |  +- commons-io:commons-io:jar:1.2:compile
[INFO] |  \- com.google.code.javaparser:javaparser:jar:1.0.11:compile
[INFO] \- junit:junit:jar:4.11:test
[INFO]    \- org.hamcrest:hamcrest-core:jar:1.3:test

Maven stuff is unrelated, otherwise you should be able to see the versions.

Hope this helps.

To be honest, I am a bit reluctant to invest time in batch/command-line sample. But if you'll get one working, I'd add it to samples.

Why do you actually need it in the command line?



来源:https://stackoverflow.com/questions/33525642/how-to-use-jaxb2-annotate-plugin-with-xjc-in-command-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!