使用OpenJDK 11运行JAXB xjc编译器

如文章“ 要从Java 11中删除的API ”所述,JDK 11不再包含 JAXB实现。 在本文中,我将结合使用JAXB ( 用于XML绑定的Java体系结构 ) 参考实现提供的xjc编译器和OpenJDK 11,将XML模式文件编译为Java类。

在Java SE 6之前,想要与Java SE应用程序一起使用JAXB的开发人员需要单独获取JAXB实现,因为Java发行版未提供该实现。 从Java SE 6开始,Java包含一个JAXB实现。 这在许多情况下都很方便,但是当开发人员希望使用比JDK所提供的版本更高或不同的JAXB 实现时,事情会变得有些困难 。 当OpenJDK 9引入了模块化时, JAXB实现被移到了java.xml.bind模块中 ,并标记为不推荐 删除 。 JAXB实现 与JDK 11一起全部删除 。 这篇文章探讨了在OpenJDK 11中使用JAXB的xjc编译器 。

由于JDK 11不再包含JAXB的实现,因此必须单独购买一个。 在本文中,我将使用2.3.0版的JAXB参考实现 。 本文中使用的JDK版本是JDK 11.0.2 General-Availability Release 。

使用OpenJDK 11运行JAXB xjc编译器_第1张图片

在不带参数的情况下运行xjc脚本会导致将帮助/用法呈现到标准输出中。

Usage: xjc [-options ...]  ... [-b ] ...
If dir is specified, all schema files in it will be compiled.
If jar is specified, /META-INF/sun-jaxb.episode binding file will be compiled.
Options:
  -nv                :  do not perform strict validation of the input schema(s)
  -extension         :  allow vendor extensions - do not strictly follow the
                        Compatibility Rules and App E.2 from the JAXB Spec
  -b       :  specify external bindings files (each  must have its own -b)
                        If a directory is given, **/*.xjb is searched
  -d            :  generated files will go into this directory
  -p            :  specifies the target package
  -m           :  generate module-info.java with given Java module name
  -httpproxy  :  set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort
  -httpproxyfile  :  Works like -httpproxy but takes the argument in a file to protect password 
  -classpath    :  specify where to find user class files
  -catalog     :  specify catalog files to resolve external entity references
                        support TR9401, XCatalog, and OASIS XML Catalog format.
  -readOnly          :  generated files will be in read-only mode
  -npa               :  suppress generation of package level annotations (**/package-info.java)
  -no-header         :  suppress generation of a file header with timestamp
  -target (2.0|2.1)  :  behave like XJC 2.0 or 2.1 and generate code that doesnt use any 2.2 features.
  -encoding  :  specify character encoding for generated source files
  -enableIntrospection :  enable correct generation of Boolean getters/setters to enable Bean Introspection apis 
  -disableXmlSecurity  :  disables XML security features when parsing XML documents 
  -contentForWildcard  :  generates content property for types with multiple xs:any derived elements 
  -xmlschema         :  treat input as W3C XML Schema (default)
  -dtd               :  treat input as XML DTD (experimental,unsupported)
  -wsdl              :  treat input as WSDL and compile schemas inside it (experimental,unsupported)
  -verbose           :  be extra verbose
  -quiet             :  suppress compiler output
  -help              :  display this help message
  -version           :  display version information
  -fullversion       :  display full version information


Extensions:
  -Xinject-code      :  inject specified Java code fragments into the generated code
  -Xlocator          :  enable source location support for generated code
  -Xsync-methods     :  generate accessor methods with the 'synchronized' keyword
  -mark-generated    :  mark the generated code as @javax.annotation.Generated
  -episode     :  generate the episode file for separate compilation
  -Xpropertyaccessors :  Use XmlAccessType PROPERTY instead of FIELD for generated classes

xjc编译器脚本(bash文件和DOS批处理文件)很方便调用jaxb-xjc.jar 脚本将其作为可执行JAR ( java -jar )调用,如以下摘录所示:

  • Windows版本( xjc.bat ):
    %JAVA% %XJC_OPTS% -jar "%JAXB_HOME%\lib\jaxb-xjc.jar" %*
  • Linux版本( xjc.sh ):
    exec "$JAVA" $XJC_OPTS -jar "$JAXB_HOME/lib/jaxb-xjc.jar" "$@"

如上面的脚本摘录所示,Java启动器的调用中包含一个环境变量XJC_OPTS 不幸的是,不能简单地通过-classpath将JAXB参考实现JAR添加到类路径中,因为使用java -jar 运行可执行的JAR仅遵循通过MANIFEST.MFClass-Path (该条目存在于jaxb-ri-2.3.0.jar作为“ Class-Path: jaxb-core.jar jaxb-impl.jar ”)。

一种解决方法是修改脚本以将JAR用作常规JAR(不带-jar )并显式执行类XJCFacade ,以便可以将类路径显式提供给Java启动器 。 Windows xjc.bat脚本xjc.bat进行了演示:

%JAVA% -cp C:\lib\javax.activation-api-1.2.0.jar;C:\jaxb-ri-2.3.0\lib\jaxb-xjc.jar com.sun.tools.xjc.XJCFacade %*

除了JAXB参考实现JAR javax.activation-api-1.2.0.jar ,我还需要在类路径中包括javax.activation-api-1.2.0.jar JAR,因为JavaBeans Application Framework ( JAF )是JDK也不再提供的依赖项(通过删除JAXB的同一JEP 320删除)。

当然,也可能根本不使用XJC脚本,而是直接运行Java启动器。 该脚本确保设置了环境变量JAXB_HOME 该环境变量应指向JAXB参考实现扩展到的目录。

通过这些更改,可以使用JDK 11在命令行上针对XSD执行JAXB xjc编译器。

翻译自: https://www.javacodegeeks.com/2019/01/running-jaxb-xjc-compiler-with-openjdk-11.html

你可能感兴趣的:(使用OpenJDK 11运行JAXB xjc编译器)