maven-jaxb2-plugin to Generate WSDL Schema

Maven-jaxb2-plugin to Generate WSDL Schema

To generate Java classes from WSDL (Web Services Description Language) using the maven-jaxb2-plugin, follow these steps:

Step 1: Add the Plugin to Your pom.xml

First, add the maven-jaxb2-plugin to your pom.xml file. This plugin will generate the Java classes from the WSDL schema.

<project> <!-- Your other project configurations --> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <!-- Choose the latest version --> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> </configuration> </plugin> </plugins> </build> </project> 

Step 2: Place the WSDL Files in the Schema Directory

Place your WSDL files in the specified schemaDirectory, for example, src/main/resources/wsdl.

Step 3: Run the Plugin

Run the plugin to generate the Java classes by executing the following Maven command:

mvn jaxb2:xjc 

Complete Example

Here's a more complete example with comments explaining each part:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>jaxb-wsdl-example</artifactId> <version>1.0-SNAPSHOT</version> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> <!-- Additional configuration options can be added here --> </configuration> </plugin> </plugins> </build> <dependencies> <!-- Add your dependencies here --> <!-- For example, the JAX-WS dependency for the generated code --> <dependency> <groupId>javax.xml.ws</groupId> <artifactId>jaxws-api</artifactId> <version>2.3.1</version> </dependency> </dependencies> </project> 

Additional Configuration Options

You can further customize the plugin configuration:

  • bindingDirectory: Specify a directory containing JAXB binding files.
  • verbose: Set to true to enable verbose output.
  • args: Pass additional arguments to the XJC tool.

Example with Additional Options

<configuration> <schemaDirectory>${basedir}/src/main/resources/wsdl</schemaDirectory> <bindingDirectory>${basedir}/src/main/resources/bindings</bindingDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> <clearOutputDir>false</clearOutputDir> <verbose>true</verbose> <args> <arg>-XautoNameResolution</arg> </args> </configuration> 

Step 4: Verify the Generated Code

After running the plugin, check the src/main/java directory for the generated Java classes. They should correspond to the XML types defined in your WSDL files.

Conclusion

By following these steps, you can use the maven-jaxb2-plugin to generate Java classes from WSDL files, making it easier to work with web services in your Java applications.

Examples

  1. How to use maven-jaxb2-plugin to generate Java classes from WSDL?

    • Description: Configure maven-jaxb2-plugin to generate Java classes from a WSDL file in your Maven project.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <schemas> <schema> <file>myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  2. How to specify WSDL location in maven-jaxb2-plugin configuration?

    • Description: Define the WSDL file location within the plugin configuration to generate Java classes.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <schemas> <schema> <file>${project.basedir}/src/main/resources/wsdl/myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  3. How to generate Java classes from multiple WSDL files using maven-jaxb2-plugin?

    • Description: Configure the plugin to handle multiple WSDL files for generating Java classes.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <schemas> <schema> <file>myService1.wsdl</file> </schema> <schema> <file>myService2.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  4. How to customize Java class package name in maven-jaxb2-plugin?

    • Description: Customize the package name for generated Java classes using the packageName configuration.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <packageName>com.example.generated</packageName> <schemas> <schema> <file>myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  5. How to include binding files in maven-jaxb2-plugin for WSDL generation?

    • Description: Use external binding files to customize the generation of Java classes from WSDL.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <bindingDirectory>${project.basedir}/src/main/resources/bindings</bindingDirectory> <bindingFiles> <bindingFile>custom-bindings.xjb</bindingFile> </bindingFiles> <schemas> <schema> <file>myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  6. How to skip WSDL code generation in maven-jaxb2-plugin?

    • Description: Configure the plugin to skip code generation under certain conditions, such as during specific Maven build phases.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <id>default</id> <goals> <goal>xjc</goal> </goals> <configuration> <skip>true</skip> </configuration> </execution> </executions> </plugin> 
  7. How to configure output directory for generated sources in maven-jaxb2-plugin?

    • Description: Specify a custom output directory for generated Java sources from WSDL files.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <outputDirectory>${project.basedir}/custom/generated-sources/jaxb</outputDirectory> <schemas> <schema> <file>myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 
  8. How to use maven-jaxb2-plugin with a specific JAXB version?

    • Description: Specify a particular JAXB version to be used by the maven-jaxb2-plugin.
    • Code:
    <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.1</version> </dependency> </dependencies> <configuration> <schemaDirectory>${project.basedir}/src/main/resources/wsdl</schemaDirectory> <outputDirectory>${project.basedir}/target/generated-sources/jaxb</outputDirectory> <schemas> <schema> <file>myService.wsdl</file> </schema> </schemas> </configuration> </plugin> 

More Tags

bitwise-operators apache-pig varchar datetimeoffset quoting dbcontext metal disabled-input generate facelets

More Programming Questions

More Date and Time Calculators

More Chemical reactions Calculators

More Organic chemistry Calculators

More Gardening and crops Calculators