use*_*057 6 java jaxb xjc maven maven-jaxb2-plugin
我正在使用maven jaxb2插件生成Java类,它是从jar中的模式构建的.但是,我不确定如何从绑定文件中正确定位这些模式.如果从jar中提取模式并将它们放在与绑定相同的目录中,那么一切都很好.但是,这不是一个实际的长期解决方案.
pom.xml中:
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.8.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemas>
<schema>
<dependencyResource>
<groupId>com.test</groupId>
<artifactId>schemas</artifactId>
<version>1.10-SNAPSHOT</version>
<resource>schemas/schema.xsd</resource>
</dependencyResource>
</schema>
</schemas>
<bindingDirectory>bindings</bindingDirectory>
<generatePackage>test.package</generatePackage>
<bindingIncludes>
<include>*.xml</include>
</bindingIncludes>
<extension>true</extension>
</configuration>
</plugin>
Run Code Online (Sandbox Code Playgroud)
bindings.xml:
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb ./bindingschema_2_1.xsd"
version="2.1">
<jxb:bindings schemaLocation="classpath:/schemas/schema.xsd" node="/xs:schema">
<jxb:bindings node="//xs:complexType[@name='AbstractChangeable']">
<jxb:class implClass="com.test.AbstractEntity" />
</jxb:bindings>
</jxb:bindings>
Run Code Online (Sandbox Code Playgroud)
您需要使用maven-dependency-plugin:unpack再点maven-jaxb2-plugin到outputDirectory.在这种情况下,在绑定文件中,您需要说出类似的内容schemaLocation="../target/schemas/schema.xsd"
我想在这里工作的是:
<jaxb:bindings schemaLocation="maven:org.jvnet.jaxb2.maven2:maven-jaxb2-plugin-tests-po!/purchaseorder.xsd" node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
</jaxb:schemaBindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)
但目前还没有.请提出问题,我会尽力解决.
现在工作的是基于SCD的绑定:
<jaxb:bindings scd="x-schema::po" xmlns:po="urn:po">
<jaxb:schemaBindings>
<jaxb:package name="org.jvnet.jaxb2.maven2.tests.po"/>
</jaxb:schemaBindings>
</jaxb:bindings>
Run Code Online (Sandbox Code Playgroud)
因此,您实际上不需要基于特定架构位置进行绑定,您可以基于命名空间URI进行绑定,这在理论上更好.
实际上,我有一种经验,即SCD绑定并不总是可靠地工作.
UPDATE
有关JAXB中SCD使用的更多信息,请参阅此链接.