使用WSDL中的jaxb2-maven-plugin生成类

and*_*dyb 17 java jaxb maven jaxb2-maven-plugin

我无法配置jaxb2-maven-plugin从WSDL生成Java类以及所有存在于同一标准目录中的多个XSD文件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>
Run Code Online (Sandbox Code Playgroud)

我正在测试它,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
Run Code Online (Sandbox Code Playgroud)

and*_*dyb 25

通过检查传递给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>
Run Code Online (Sandbox Code Playgroud)

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

  • omg,谢谢你,当我没有指定目录并使用默认的xsd目录时,这是有效的 (2认同)

Eli*_*iuX 6

如果你正在生成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> 
Run Code Online (Sandbox Code Playgroud)

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


ord*_*lex 6

我对 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

我试过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>
Run Code Online (Sandbox Code Playgroud)

为了运行这个我使用了命令

mvn jaxb2:xjc
Run Code Online (Sandbox Code Playgroud)

尝试一下,它会将 jaxb 类生成到您的 src 文件夹中。希望您正在寻找这个。


J. *_*bel 5

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

              <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>
Run Code Online (Sandbox Code Playgroud)