How can I tell jaxb / maven to create multiple schema packages?

Example:

</plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>schema1.xsd</include> </schemaIncludes> <generatePackage>schema1.package</generatePackage> </configuration> </plugin> <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>schema2.xsd</include> </schemaIncludes> <generatePackage>schema2.package</generatePackage> </configuration> </plugin> </plugins> 

What happened: The first Maven runs the first plugin. It then deletes the target folder and creates a second package, which is then displayed.

I tried setting target / somedir1 for the first configuration and target / somedir2 for the second configuration. But the behavior does not change? Any ideas? I do not want to create packages directly in the src / main / java folder, because these packages are genereated and should not be mixed with manually created classes.

+57
java maven-2 schema xsd jaxb
May 18 '10 at 12:12
source share
9 answers

I had to specify different generateDirectory (without this, the plugin believed that the files were updated and did not generate anything during the second execution). And I recommend following the target/generated-sources/<tool> convention for generated sources so that they are automatically imported into your favorite IDE. I also recommend declaring multiple execution instead of declaring the plugin twice (and moving the configuration inside each execution element):

 <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <id>schema1-generate</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>shiporder.xsd</include> </schemaIncludes> <generatePackage>com.stackoverflow.package1</generatePackage> <generateDirectory>${project.build.directory}/generated-sources/xjc1</generateDirectory> </configuration> </execution> <execution> <id>schema2-generate</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>books.xsd</include> </schemaIncludes> <generatePackage>com.stackoverflow.package2</generatePackage> <generateDirectory>${project.build.directory}/generated-sources/xjc2</generateDirectory> </configuration> </execution> </executions> </plugin> 

With this setting, I get the following result after mvn clean compile

 $ tree target /
 target /
 β”œβ”€β”€ classes
 β”‚ β”œβ”€β”€ com
 β”‚ β”‚ └── stackoverflow
 β”‚ β”‚ β”œβ”€β”€ App.class
 β”‚ β”‚ β”œβ”€β”€ package1
 β”‚ β”‚ β”‚ β”œβ”€β”€ ObjectFactory.class
 β”‚ β”‚ β”‚ β”œβ”€β”€ Shiporder.class
 β”‚ β”‚ β”‚ β”œβ”€β”€ Shiporder $ Item.class
 β”‚ β”‚ β”‚ └── Shiporder $ Shipto.class
 β”‚ β”‚ └── package2
 β”‚ β”‚ β”œβ”€β”€ BookForm.class
 β”‚ β”‚ β”œβ”€β”€ BooksForm.class
 β”‚ β”‚ β”œβ”€β”€ ObjectFactory.class
 β”‚ β”‚ └── package-info.class
 β”‚ β”œβ”€β”€ dir1
 β”‚ β”‚ └── shiporder.xsd
 β”‚ └── dir2
 β”‚ └── books.xsd
 └── generated-sources
     β”œβ”€β”€ xjc
     β”‚ └── META-INF
     β”‚ └── sun-jaxb.episode
     β”œβ”€β”€ xjc1
     β”‚ └── com
     β”‚ └── stackoverflow
     β”‚ └── package1
     β”‚ β”œβ”€β”€ ObjectFactory.java
     β”‚ └── Shiporder.java
     └── xjc2
         └── com
             └── stackoverflow
                 └── package2
                     β”œβ”€β”€ BookForm.java
                     β”œβ”€β”€ BooksForm.java
                     β”œβ”€β”€ ObjectFactory.java
                     └── package-info.java

This is apparently the expected result.

+78
May 18 '10 at 13:21
source share

You can also use JAXB bindings to specify different packages for each schema, for example.

 <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0" schemaLocation="book.xsd"> <jaxb:globalBindings> <xjc:serializable uid="1" /> </jaxb:globalBindings> <jaxb:schemaBindings> <jaxb:package name="com.stackoverflow.book" /> </jaxb:schemaBindings> </jaxb:bindings> 

Then just use the new maven-jaxb2-plugin 0.8.0 <schemas> and <bindings> pom.xml in pom.xml . Or specify the topmost directory in <schemaDirectory> and <bindingDirectory> and <include> your schemas and bindings:

 <schemaDirectory>src/main/resources/xsd</schemaDirectory> <schemaIncludes> <include>book/*.xsd</include> <include>person/*.xsd</include> </schemaIncludes> <bindingDirectory>src/main/resources</bindingDirectory> <bindingIncludes> <include>book/*.xjb</include> <include>person/*.xjb</include> </bindingIncludes> 

I think this is a more convenient solution , because when you add a new XSD, you do not need to change Maven pom.xml , just add a new XJB binding file to the same directory.

+10
Dec 22 '11 at 14:44
source share

you must change this to define the plugin only once and execute the execution area twice ... like the following ... and the generateDirectory file must be installed (based on documents).

 <plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <version>0.7.1</version> <executions> <execution> <id>firstrun</id> <goals> <goal>generate</goal> </goals> <configuration> <generateDirectory>target/gen1</generateDirectory> <schemaDirectory>src/main/resources/dir1</schemaDirectory> <schemaIncludes> <include>schema1.xsd</include> </schemaIncludes> <generatePackage>schema1.package</generatePackage> </configuration> </execution> <execution> <id>secondrun</id> <goals> <goal>generate</goal> </goals> <configuration> <generateDirectory>target/gen2</generateDirectory> <schemaDirectory>src/main/resources/dir2</schemaDirectory> <schemaIncludes> <include>schema2.xsd</include> </schemaIncludes> <generatePackage>schema2.package</generatePackage> </configuration> </execution> </executions> </plugin> 

It seemed to me that you are struggling with one rule of maven artifacts ... maybe you should think about it.

+6
May 18 '10 at
source share

I decided with:

  <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> <forceRegenerate>true</forceRegenerate> 

add this to each configuration;)

+3
Feb 11 '11 at 11:28
source share

This can also be achieved by specifying a legacy file name for the schemas and not cleaning the output directory. The default out put directive is automatically included in the classpath, which is not very convenient. If we specify a different output directory, we need to take care of the classpath method to use this code in the IDE. For example -

 <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.3.1</version> <configuration> <quiet>true</quiet> <verbose>false</verbose> <clearOutputDir>false</clearOutputDir> <readOnly>true</readOnly> <arguments>-mark-generated</arguments> </configuration> <executions> <execution> <id>reportingSchema</id> <goals> <goal>xjc</goal> </goals> <configuration> <schemaDirectory>src/main/resources/schema/r17/schemaReporting</schemaDirectory> <schemaIncludes> <include>OCISchemaReporting.xsd</include> </schemaIncludes> <packageName>com.broadsoft.oci.r17.reporting</packageName> <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-reporting</staleFile> </configuration> </execution> <execution> <id>schemaAS</id> <goals> <goal>xjc</goal> </goals> <configuration> <schemaDirectory>src/main/resources/schema/r17/schemaAS</schemaDirectory> <schemaIncludes> <include>OCISchemaAS.xsd</include> </schemaIncludes> <packageName>com.broadsoft.oci.r17.as</packageName> <staleFile>${build.directory}/generated-sources/.jaxb-staleFlag-as</staleFile> </configuration> </execution> </executions> </plugin> </plugins> 

Source: Generating Code with the JAXB Plugin

+3
Jan 11 2018-12-12T00:
source share

The following works for me, after much testing

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.1</version> <executions> <execution> <id>xjc1</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.clientSummary</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetClientSummary.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>xjc2</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.wsclient.employerProfile</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetEmployerProfile.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>xjc3</id> <goals> <goal>xjc</goal> </goals> <configuration> <packageName>com.mycompany.wsclient.producersLicenseData</packageName> <sourceType>wsdl</sourceType> <sources> <source>src/main/resources/wsdl/GetProducersLicenseData.wsdl</source> </sources> <outputDirectory>target/generated-sources/xjb</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </execution> </executions> </plugin> 
+2
May 20 '15 at 11:01
source share

This is fixed in version 1.6 of the plugin .

  <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> 

Quickly notice that I noticed that the first result of the iteration was deleted. I fixed this by adding the following to each of the executions.

  <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> 

Here is my complete working example with the correct output of each iteration. BTW I had to do this due to duplicating the namespace problem with xsd that I received. This seems to solve my problem.

  <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>1.6</version> <executions> <execution> <id>submitOrderRequest</id> <goals> <goal>xjc</goal> </goals> <configuration> <extension>true</extension> <schemaDirectory>src/main/resources/xsd/</schemaDirectory> <!-- <schemaFiles>getOrderStatusResponse.xsd,quoteShippingRequest.xsd,quoteShippingResponse.xsd,submitOrderRequest.xsd,submitOrderResponse.xsd</schemaFiles> --> <schemaFiles>submitOrderRequest.xsd</schemaFiles> <bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory> <bindingFiles>submitOrderRequest.xjb</bindingFiles> <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> </configuration> </execution> <execution> <id>submitOrderResponse</id> <goals> <goal>xjc</goal> </goals> <configuration> <extension>true</extension> <schemaDirectory>src/main/resources/xsd/</schemaDirectory> <!-- <schemaFiles>getOrderStatusResponse.xsd,quoteShippingRequest.xsd,quoteShippingResponse.xsd,submitOrderRequest.xsd,submitOrderResponse.xsd</schemaFiles> --> <schemaFiles>submitOrderResponse.xsd</schemaFiles> <bindingDirectory>${project.basedir}/src/main/resources/xjb</bindingDirectory> <bindingFiles>submitOrderResponse.xjb</bindingFiles> <removeOldOutput>false</removeOldOutput> <clearOutputDir>false</clearOutputDir> </configuration> </execution> </executions> </plugin> 
+1
Oct 07 '14 at 19:31
source share

I ran into a lot of problems when using jaxb in Maven, but I managed to solve your problem by doing the following

First create a schema.xjc file

 <?xml version="1.0" encoding="UTF-8"?> <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xsd="http://www.w3.org/2001/XMLSchema" jaxb:version="2.0"> <jaxb:bindings schemaLocation="YOUR_URL?wsdl#types?schema1"> <jaxb:schemaBindings> <jaxb:package name="your.package.name.schema1"/> </jaxb:schemaBindings> </jaxb:bindings> <jaxb:bindings schemaLocation="YOUR_URL??wsdl#types?schema2"> <jaxb:schemaBindings> <jaxb:package name="your.package.name.schema2"/> </jaxb:schemaBindings> </jaxb:bindings> </jaxb:bindings> 

A package name can be anything you want if it does not contain reserved keywords in Java

Then you need to create a wsimport.bat script in order to generate your package and code in your preferred location.

 cd C:\YOUR\PATH\TO\PLACE\THE\PACKAGES wsimport -keep -verbose -b "C:\YOUR\PATH\TO\schema.xjb" YOUR_URL?wsdl pause 



If you do not want to use cd, you can put the wsimport.bat file in the folder "C: \ YOUR \ PATH \ TO \ PLACE \ THE \ PACKAGES"

If you run it without -keep -verbose, it will only generate packages, but not .java files.

-b will verify that schema.xjc is used when generating

+1
Sep 22 '15 at 9:17
source share

There is another, clear (IMO) solution for this. There is the "staleFile" parameter, which uses as a flag to not generate material again. Just change it in every performance.

0
Oct 22 '13 at 12:26
source share



All Articles