简体   繁体   English

如何使用 jaxb2-maven-plugin 2.x 从 WSDL 生成 Java 类?

[英]How to generate Java classes from WSDL with jaxb2-maven-plugin 2.x?

I am trying using the pluggin jaxb2-maven-plugin to create the Java class from the wsdl.我正在尝试使用jaxb2-maven-plugin从 wsdl 创建 Java 类。

With the version 1.5 this code from Generate classes with jaxb2-maven-plugin from WSDL works:在 1.5 版中, 来自 WSDL 的 Generate classes with jaxb2-maven-plugin 的代码有效:

        <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>
                    <!-- 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>
            </plugin>

But when use plugin version 2.3.1, I get this error:但是当使用插件版本 2.3.1 时,我收到此错误:

Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc (xjc) on project demo: MojoExecutionException: NoSchemasException -> [Help 1]

Does someone know how use WSDL file with this new plugin version?有人知道如何在这个新的插件版本中使用 WSDL 文件吗?

I have already found the solution.我已经找到了解决方案。

When the jaxb2-maven-plugin version is >= 2.0 you have to use the follow configuration:当 jaxb2-maven-plugin 版本 >= 2.0 时,您必须使用以下配置:

             <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <packageName>com.example.demo.wsdl</packageName>
                    <sourceType>wsdl</sourceType>
                    <sources>
                        <source>src/main/resources/horarios.wsdl</source>
                    </sources>
                    <outputDirectory>target/generated-sources/</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
            </plugin>

The difference is not only the syntax.区别不仅在于语法。 That version does not create the class within the project (src/main/java), it creates in the directory that you wrote in outputDirectory and in the package of packageName .该版本不会在项目 (src/main/java) 中创建类,而是在您在outputDirectorypackageName包中编写的目录中创建。

When you use the class generated it is transparent like if it would be in the same project.当您使用生成的类时,它是透明的,就像它在同一个项目中一样。

If you want to begin with a XSD:如果您想从 XSD 开始:

              <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.3.1</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>

                <configuration>
                    <xjbSources>
                        <xjbSource>src/main/resources/global.xjb</xjbSource>
                    </xjbSources>
                    <sources>
                        <source>src/main/resources/Ventas.xsd</source>
                    </sources>
                    <outputDirectory>${basedir}/src/main/java</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                </configuration>
            </plugin>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM