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

问题描述 投票:3回答:2

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

在1.5版本中,Generate classes with jaxb2-maven-plugin from WSDL中的此代码起作用:

        <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文件吗?

java web-services maven soap wsdl
2个回答
5
投票

我已经找到解决方法。

当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)中创建类,而是在您在outputDirectory中编写的目录和packageName包中创建。

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


0
投票

如果要以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>
© www.soinside.com 2019 - 2024. All rights reserved.