Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate classes with jaxb2-maven-plugin from WSDL

Tags:

I am having trouble configuring the jaxb2-maven-plugin to generate Java classes from a WSDL and multiple XSD files that all exist in the same standard directory src/main/xsd.

how to use jaxb2 maven plugin with inline XSD? is related only in that the answers correctly suggest using the wsdl parameter in the plugin config but that question is really concerned with inline XSDs and my XSDs are external.

The plugin goal parameters are listed here.

My plugin configuration is:

<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>jaxb2-maven-plugin</artifactId>     <version>1.5</version>     <executions>         <execution>             <id>xjc</id>             <goals>                 <goal>xjc</goal>             </goals>         </execution>     </executions>     <configuration>         <packageName>com.x.y.model</packageName>         <wsdl>true</wsdl>     </configuration> </plugin> 

I am testing this with mvn -X clean jaxb2:xjc but the plugin is ignoring the .wsdl as seen in the debug output

[DEBUG] accept false for file c:\projects\foo\src\main\xsd\service.wsdl [DEBUG] accept true for file c:\projects\foo\src\main\xsd\datatypes.xsd [DEBUG] accept true for file c:\projects\foo\src\main\xsd\more-datatypes.xsd 
like image 213
andyb Avatar asked Apr 09 '13 13:04

andyb


People also ask

How do I create a class in WSDL?

java . Using OEPE, you can generate a WSDL file from a Java class for your project by following this procedure: Create new or use an existing Web service project. Right-click a Java class in your Web service project in the Project Explorer, and select WebLogic Web Service > Generate WSDL from the drop-down menu.

How do I generate a class from WSDL using Apache CXF?

You will have to make sure that you create an appropriate directory structure for your project and add the earlier shown hello. wsdl file to the specified folder. The wsdl2java plugin will compile this wsdl and create Apache CXF classes in a pre-defined folder.


2 Answers

By examining the Maven debug output of the arguments being passed to the JAXB XJC (and a bit of trial and error) I found that I needed to supply 2 more configuration parameters to the plugin.

This stops the plugin scanning for XSD files and just uses the .wsdl as the source. The XSD files are included in the WSDL as<xsd:include schemaLocation="datatypes.xsd" /> directives, for example, which are resolved locally resulting in all types from the WSDL and XSD being generated as Java classes.

The configuration section that worked for me is:

<configuration>     <packageName>com.x.y.model</packageName>     <wsdl>true</wsdl>     <xmlschema>false</xmlschema>     <schemaFiles>service.wsdl</schemaFiles> </configuration> 

Without the <xmlschema>false</xmlschema> Maven errors with:

org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:1.5:xjc (default-cli) on project foo: Could not process schema: /c:/projects/foo/src/main/xsd/service.wsdl

like image 125
andyb Avatar answered Oct 19 '22 05:10

andyb


If you are generating wsdl and xsd as well try to put in a different execution configuration: It may not have the same schemaDirectory or the plugin will not run successfully the second execution, cause it caches executions based on this variable. I advice to do it such as

        <plugin>             <groupId>org.codehaus.mojo</groupId>             <artifactId>jaxb2-maven-plugin</artifactId>             <version>1.6</version>             <executions>                 <execution>                     <id>generate-sri-facturas</id>                     <phase>generate-sources</phase>                     <goals>                         <goal>xjc</goal>                      </goals>                      <configuration>                          <outputDirectory>target/generated-sources/sri</outputDirectory>                         <packageName>${commonsource.packageName}</packageName>                          <schemaDirectory>src/main/resources/schema/xsd</schemaDirectory>                         <schemaFiles>factura_v1.1.0.xsd</schemaFiles>                     </configuration>                  </execution>                  <execution>                     <id>generate-sri-autorizacion-comprobantes</id>                     <phase>generate-sources</phase>                     <goals>                         <goal>xjc</goal>                      </goals>                      <configuration>                          <outputDirectory>target/generated-sources/sri/autorizacion</outputDirectory>                         <packageName>${commonsource.packageName}.autorizacion</packageName>                         <wsdl>true</wsdl>                         <xmlschema>false</xmlschema>                         <schemaDirectory>src/main/resources/schema/wsdl</schemaDirectory>                         <schemaFiles>AutorizacionComprobantes.wsdl</schemaFiles>                     </configuration>                  </execution>              </executions>          </plugin>  

I created a xsd and a wsdl folder to separate configurations.

like image 32
EliuX Avatar answered Oct 19 '22 07:10

EliuX