Generate classes with jaxb2-maven-plugin from WSDL
Asked Answered
P

7

26

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
Particularism answered 9/4, 2013 at 13:26 Comment(0)
P
27

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

Particularism answered 9/4, 2013 at 13:26 Comment(1)
omg, thank you this works when I don't specify a directory and use the default xsd directoryTriaxial
A
11

I had this question for jaxb2-maven-plugin 2.5.0. Here is my solution:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <phase>generate-resources</phase>
            <id>xjc</id>
            <goals>
                <goal>xjc</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <sourceType>wsdl</sourceType>
        <sources>
            <source>${project.basedir}/src/main/resources/wsdl</source>
        </sources>
        <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
        <clearOutputDir>false</clearOutputDir>
        <packageName>com.project.generated</packageName>
        <noPackageLevelAnnotations>true</noPackageLevelAnnotations>
    </configuration>
</plugin>
Acrocarpous answered 9/6, 2020 at 23:19 Comment(1)
It's a wrong configuration, configuration tag must be inside execution tag.Tindle
L
8

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.

Liatris answered 17/7, 2015 at 18:17 Comment(0)
C
6

You can use the follows code in the configuration:

              <configuration>
                    <!-- Package to store the generated file -->
                    <packageName>com.example.demo.wsdl</packageName>
                    <!-- Treat the input as WSDL -->
                    <wsdl>true</wsdl>
                    <!-- Input is not XML schema -->
                    <xmlschema>false</xmlschema>
                    <!-- The WSDL file that you saved earlier -->
                    <schemaFiles>horarios.wsdl</schemaFiles>
                    <!-- The location of the WSDL file -->
                    <schemaDirectory>${project.basedir}/src/main/resources</schemaDirectory>
                    <!-- The output directory to store the generated Java files -->
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <!-- Don't clear output directory on each run -->
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
Canorous answered 6/7, 2017 at 13:13 Comment(0)
N
5

I have tried jaxb2-maven-plugin which generate the java file

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <schemaDirectory>src/main/webapp/schemas/</schemaDirectory>
                    <wsdl>true</wsdl>
                    <outputDirectory>src/main/java</outputDirectory>
                </configuration>
            </plugin>

To run this i have used the command

mvn jaxb2:xjc

Try this it will generate the jaxb classes into your src folder. Hope you are looking for this.

Nescience answered 9/4, 2013 at 13:27 Comment(0)
T
2

For version 2.5 the configuration would be the following:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <executions>
        <execution>
            <id>xjc</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>xjc</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                <packageName>com.packagename</packageName>
                <sources>
                    <source>${project.basedir}/src/main/resources/wsdl</source>
                </sources>
                <sourceType>wsdl</sourceType>
                <clearOutputDir>false</clearOutputDir>
                <noPackageLevelAnnotations>true</noPackageLevelAnnotations>
            </configuration>
        </execution>
    </executions>
</plugin>
Tindle answered 15/12, 2021 at 14:55 Comment(0)
R
0
  1. plugin com.sun.xml.ws:jaxws-maven-plugin:4.0.2 goal wsimport when generating soap client (jakarta namespace)
  2. very important dependency com.sun.xml.ws:jaxws-ri:4.0.2 type pom
Rummel answered 26/10, 2023 at 7:54 Comment(1)
<plugin> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-maven-plugin</artifactId> <version>${jaxws.maven.plugin.version}</version> <executions> <execution> <id>soap-client</id> <goals> <goal>wsimport</goal> </goals> <configuration> <wsdlUrls> <wsdlUrl>${wsdl.url}</wsdlUrl> </wsdlUrls> <packageName>xx.xx.client</packageName> <sourceDestDir>${basedir}/src/main/java/</sourceDestDir> </configuration> </execution> </executions> </plugin>Harrell

© 2022 - 2024 — McMap. All rights reserved.