繁体   English   中英

从 WSDL 使用 jaxb2-maven-plugin 生成类

[英]Generate classes with jaxb2-maven-plugin from WSDL

我在配置jaxb2-maven-plugin以从 WSDL 和多个 XSD 文件生成 Java 类时遇到问题,这些文件都存在于同一标准目录src/main/xsd

如何使用带有内联 XSD 的 jaxb2 maven 插件? 只是因为答案正确地建议在插件配置中使用wsdl参数,但该问题确实与内联 XSD 相关,而我的 XSD 是外部的。

此处列出插件目标参数。

我的插件配置是:

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

我正在使用mvn -X clean jaxb2:xjc对此进行测试,但插件忽略了调试输出中的.wsdl

[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

通过检查传递给 JAXB XJC 的参数的 Maven 调试输出(以及一些反复试验),我发现我需要为插件提供另外 2 个配置参数。

这会停止插件扫描 XSD 文件,并仅使用.wsdl作为源。 例如,XSD 文件作为<xsd:include schemaLocation="datatypes.xsd" />指令包含在 WSDL 中,这些指令在本地解析,导致来自 WSDL 和 XSD 的所有类型都生成为 Java 类。

对我有用的配置部分是:

<configuration>
    <packageName>com.x.y.model</packageName>
    <wsdl>true</wsdl>
    <xmlschema>false</xmlschema>
    <schemaFiles>service.wsdl</schemaFiles>
</configuration>

没有<xmlschema>false</xmlschema> Maven 错误:

org.apache.maven.lifecycle.LifecycleExecutionException:无法在项目 foo 上执行目标 org.codehaus.mojo:jaxb2-maven-plugin:1.5:xjc (default-cli):无法处理架构:/c:/projects/foo /src/main/xsd/service.wsdl

如果您正在生成 wsdl 和 xsd 以及尝试放入不同的执行配置:它可能没有相同的schemaDirectory或插件将无法成功运行第二次执行,导致它基于此变量缓存执行。 我建议这样做,例如

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

我创建了一个xsd和一个wsdl文件夹来分隔配置。

我对 jaxb2-maven-plugin 2.5.0 有这个问题。 这是我的解决方案:

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

您可以在配置中使用以下代码:

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

我试过jaxb2-maven-plugin生成 java 文件

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

要运行它,我使用了命令

mvn jaxb2:xjc

试试这个,它会在你的 src 文件夹中生成 jaxb 类。 希望你正在寻找这个。

对于 2.5 版,配置如下:

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

暂无
暂无

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

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