Defining package names for shared XSD with xjc and bindings

I am trying to create Java classes from several specific xjc with xjc . These schemes have some common definitions, so they import a number of common XSDs. In particular, they can include from zero to all common XSDs.

I would like to generate all the classes from a specific XSD in a specific package, but keep the generated classes for common schemes in a common package so that they do not repeat for each specific scheme in the source tree.

I found out that custom bindings can be used to specify packages based on each scheme, for example:

 <jxb:bindings schemaLocation="common1.xsd" node="/xsd:schema"> <jxb:schemaBindings> <jxb:package name="mypackage.commonclasses"/> </jxb:schemaBindings> </jxb:bindings> 

I have the following structure:

 schemas | - common | | - common1.xsd --> XSD with common types #1 | | - ... | | - commonN.xsd --> XSD with common types #N | | - commonBindings.xjb --> Defines package "mypackage.commons" for common*.xsd | - specific1 | | - specific1.xsd --> Includes ../common/common{1-N}.xsd | | - specific1.xjb --> Defines package "mypackage.specific1" for specific1.xsd | - specificN | | - specificN.xsd --> Includes only ../common/common1.xsd | | - specificN.xjb --> Defines package "mypackage.specificN" for specificN.xsd 

Everything works perfectly:

 xjc -b schemas/specific1 -b schemas/common schemas/specific1/specific1.xsd 

It generates classes for specific1.xsd in mypackage.specific1 and general classes in mypackage.commons . But when I try to generate classes for specificN , xjc throws the following error:

 [ERROR] "file:/drive/dir/schemas/common/common1.xsd" is not a part of this compilation. Is this a mistake for "/drive/dir/schemas/common/commonBindings.xjb"? line 2 of file:/drive/dir/schemas/common/commonBindings.xjb 

I repeat this error for every common XSD not imported into any particular xsd.

Is there any way to make xjc ignore bindings in commonBindings.xjb that are not used in XSD. Am I generating classes for?

Or am I heading in the wrong direction using this approach, and should, for example, use annotations in a particular xsd? I would like to avoid modifying the circuits if possible.

+6
source share
1 answer

I think you need to use the JAXB episode. See this SO post , look at @BlaiseDoughan answer.

In your case, start xjc first, using your own bind file to configure the package name, for all common XSDs, to create the episode file and your common Java classes.

Then use xjc again, with the other XSDs you want, by referencing the episode file from the first run.

+3
source

Source: https://habr.com/ru/post/916584/


All Articles