JAXB - 创建模块以供重用
JAXB 是否支持模块化代码生成?
我的大部分背景都是使用 JibX 进行 XML 编组,但由于遗留原因,我们公司正在使用 JAXB。
JIBX 可用的一项功能是模块化代码生成。假设我有一个主模式,但该模式有几个不同的信封。使用 JibX,我可以从 JibX 的核心模式创建一个 jar 文件,然后在单独的项目中,我可以 JibX 我的信封模式并简单地指向共享 jar,而不必为每个信封重复生成核心模式的代码。
我还没有看到 JAXB 处理这个问题的方法 - 有人成功地做了这样的事情吗?
提前致谢, 罗伊
Does JAXB support modular code generation?
Most of my background is with JibX for XML marshalling, but for legacy reasons our firm is using JAXB.
One feature that was available for JIBX was modular code generation. Say I have a main schema but I have several different envelopes for that schema. With JibX I could create a jar file out of the JibX'ed core schema, and then in separate projects I could JibX my envelope schemas and simply point to the shared jar instead of having to duplicate the code generation of the core schemas for each envelope.
I don't yet see a way for JAXB to handle this - has anyone been successful doing something like this?
Thanks in advance,
Roy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 JAXB RI,这是通过“episode”文件进行处理的(这些实际上只是自定义文件)。首先处理核心模式,确保 xjc 使用
-episode
arg。将该处理的结果打包到一个 JAR 文件中,其中包含META-INF/sun-jaxb.episode
中的剧集文件。然后,在处理其他模式时,将该 JAR 文件作为参数传递给 xjc。For the JAXB RI, that's handled with "episode" files (these are really just customization files). Process the core schema first, making sure to have xjc use the
-episode <file>
arg. Package the results of that processing into a JAR file with the episode file inMETA-INF/sun-jaxb.episode
. Then, pass that JAR file as an arg to xjc when processing the other schemas.使用 JAXB 2.1 实现(Metro、EclipseLink MOXy、Apache JaxMe 等),您可以指定模式类型与现有类相对应,以防止生成它们。
例如:
root.xsd
imported.xsd
问题陈述
如果您使用 XJC 工具从 XML 模式生成 java 类:
您将执行以下操作生成:
imported-bindings.xml
您可以使用 JAXB 绑定文件来指定 import.xsd 中的类型指向现有类:
运行 XJC
现在,如果我们运行 XJC和out 绑定文件:
不会生成绑定文件中指定的任何文件:
替代方法
直接从导入的架构 (xjc import.xsd) 和间接 (xjc root.xsd) 生成的代码是相同的。您只需删除间接生成的代码并指向包含直接生成的代码的项目即可。
Using a JAXB 2.1 implementation (Metro, EclipseLink MOXy, Apache JaxMe, etc), you can specify that schema types correspond to existing classes in order to prevent them from being generated.
For example:
root.xsd
imported.xsd
Problem Statement
If you use the XJC tool to generate java classes from the XML schema:
You the following is generated:
imported-bindings.xml
You can use a JAXB bindings file to specify that types from imported.xsd point to existing classes:
Running the XJC
Now if we run XJC with out bindings file:
None of the files specified in the bindings file will be generated:
Alternative Approach
The code generated from the imported schema directly (xjc imported.xsd) and indirectly (xjc root.xsd) is the same. You can simply drop the code generated indirectly and point at the project containing the code that was generated directly.