使用jaxb 2-maven-plugin从WSDL生成类

zi8p0yeb  于 2023-11-17  发布在  Maven
关注(0)|答案(7)|浏览(138)

我在配置jaxb2-maven-plugin以从WSDL和多个XSD文件生成Java类时遇到了问题,这些文件都存在于同一个标准目录src/main/xsd中。
how to use jaxb2 maven plugin with inline XSD?的相关性仅在于答案正确地建议在插件配置中使用wsdl参数,但该问题实际上与内联XSD有关,而我的XSD是外部的。
插件的目标参数是here
我的插件配置是:

<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

nnt7mjpx

nnt7mjpx1#

通过检查传递给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. Lifecycle ExecutionException:无法在项目foo上执行目标org.codehaus.mojo:jaxb 2-maven-plugin:1.5:xjc(default-configuration):无法处理架构:/c:/projects/foo/src/main/xsd/service.wsdl

tgabmvqs

tgabmvqs2#

我对jaxb 2-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>

字符串

xfb7svmp

xfb7svmp3#

如果你也在生成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文件夹来分隔配置。

bd1hkmkf

bd1hkmkf4#

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

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

字符串

ddrv8njm

ddrv8njm5#

我试过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


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

jm81lzqq

jm81lzqq6#

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

字符串

fumotvh3

fumotvh37#

  1. plugin com.sun.xml.ws:jaxws-maven-plugin:4.0.2 goal wsimport when generating soap client(jakworldnamespace)
  2. very important dependency com.sun.xml.ws:jaxws-ri:4.0.2 type pom

相关问题