繁体   English   中英

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

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

我正在尝试使用jaxb2-maven-plugin从 wsdl 创建 Java 类。

在 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>

但是当使用插件版本 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]

有人知道如何在这个新的插件版本中使用 WSDL 文件吗?

我已经找到了解决方案。

当 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>

区别不仅在于语法。 该版本不会在项目 (src/main/java) 中创建类,而是在您在outputDirectorypackageName包中编写的目录中创建。

当您使用生成的类时,它是透明的,就像它在同一个项目中一样。

如果您想从 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