【问题标题】:How to generate classes from wsdl using Maven and wsimport?如何使用 Maven 和 wsimport 从 wsdl 生成类?
【发布时间】:2013-08-20 14:50:49
【问题描述】:

当我尝试运行“mvn generate-sources”时,这是我的输出:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gensourcesfromwsdl 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.104s
[INFO] Finished at: Tue Aug 20 15:41:10 BST 2013
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

我没有收到任何错误,但是没有从 wsdl 文件生成的 java 类。

这是我运行插件的 pom.xml 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>gensourcesfromwsdl</groupId>
    <artifactId>gensourcesfromwsdl</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxws-maven-plugin</artifactId>
                    <version>1.12</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>wsimport</goal>
                            </goals>
                            <configuration>
                                <wsdlLocation>http://mysite/firstwsdl.asmx?wsdl</wsdlLocation>
                                <packageName>com</packageName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

我做错了什么?包com存在于项目'gensourcesfromwsdl'中,wsdl位置有效。

当我通过命令行运行wsimport 时:>wsimport -keep -verbose http://mysite/firstwsdl.asmx?wsdl 生成类。

【问题讨论】:

  • 你有没有找到解决这个问题的方法?我遇到了同样的问题
  • @Oliver Watkins 恐怕不是,我最终在 Maven 之外生成了这些类。这可能会对您有所帮助:stackoverflow.com/questions/6920175/…

标签: maven wsimport


【解决方案1】:

要从 WSDL 生成类,您只需要在 pom.xml 中构建-helper-maven-plugin 和 jaxws-maven-plugin
确保您已将 wsdl 放在文件夹 src/main/resources/wsdl 和 src/main/resources/schema 中的相应架构下,从项目根目录运行命令“mvn generate-sources”。

C:/Project root directory > mvn generate-sources

生成的java类可以放在文件夹下

target/generated/src/main/java/com/raps/code/generate/ws.

pom.xml sn-p

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals><goal>add-source</goal></goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated/src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.12</version>
    <configuration>
        <wsdlDirectory>${project.basedir}/src/main/resources/wsdl</wsdlDirectory>
        <packageName>com.raps.code.generate.ws</packageName>
        <keep>true</keep>
        <sourceDestDir>${project.build.directory}/generated/src/main/java</sourceDestDir>
    </configuration>
    <executions>
        <execution> 
            <id>myImport</id>
            <goals><goal>wsimport</goal></goals>
        </execution>
    </executions>
</plugin>

【讨论】:

  • 这可能是一个愚蠢的问题,但是 build-helper-maven-plugin 是干什么用的?那和 jaxws-maven-plugin 有什么不同?
  • @SillySally jaxws-maven-plugin 执行 wsimport 步骤以从 Web 服务定义语言(wsdl、xsd 文件)生成 java 代码。此代码生成到专用文件夹中,/target/generated... build-helper-maven-plugin 是一个插件,它将声明附加源文件夹(除了旧的 src/main/java 一个之外添加/target/generated...)。 (愚蠢的问题不存在!^:p)
  • 啊。好的。但是还有一个问题,我们为什么不直接在 src/main/java 下生成代码?
  • @SillySally 这样做是为了避免您正在编写的代码和必须保持不变的生成代码之间的混淆;)
  • @SillySally 否则版本管理系统 (svn/etc.) 总是会在生成后注意到更改的源,这在自动构建环境中会很脏
【解决方案2】:

这是一个示例,说明如何使用 jaxws maven 插件从 url 或文件位置(从 wsdl 文件位置被注释)从 wsdl 生成类。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>  
    <plugins>           
        <!-- usage of jax-ws maven plugin-->
        <plugin> 
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>1.12</version>
            <executions> 
                <execution> 
                    <id>wsimport-from-jdk</id>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- using wsdl from an url -->
                <wsdlUrls>
                    <wsdlUrl>
                        http://myWSDLurl?wsdl
                    </wsdlUrl>
                </wsdlUrls>
                <!-- or using wsdls file directory -->
                    <!-- <wsdlDirectory>src/wsdl</wsdlDirectory> -->
                <!-- which wsdl file -->
                <!-- <wsdlFiles> -->
                    <!-- <wsdlFile>myWSDL.wsdl</wsdlFile> -->
                <!--</wsdlFiles> -->
                <!-- Keep generated files -->
                <keep>true</keep> 
                <!-- Package name --> 
                <packageName>com.organization.name</packageName> 
                <!-- generated source files destination-->
                <sourceDestDir>target/generatedclasses</sourceDestDir>
            </configuration>
        </plugin>
    </plugins>  
</build>  

【讨论】:

  • 以及如何在 maven 中运行它?
  • 从你的 IDE 运行带有目标 jaxws:wsimport 的 maven 或在命令提示符中运行 mvn jaxws:wsimport
  • 只需添加必要的 以定期使用 mvn 构建它
  • Kadiri 也使用build-helper-maven-plugin - 你为什么不使用它,有什么功能区别?
【解决方案3】:

尽管这反应有点晚,但可能对某人有所帮助。看起来你已经使用了 pluginManagement。如果你使用 pluginManagement ,它不会选择插件执行。

应该在

<build>
<plugins>           
        <plugin> 

【讨论】:

  • 我刚刚为此提交了一个编辑,正如您指出的那样,这可能不是每个人都清楚,尤其是 maven 初学者,可能会找到答案
【解决方案4】:

尝试将 wsdlLocation 包装在 wsdlUrls 中

            <wsdlUrls>
                <wsdlLocation>http://url</wsdlLocation>
            </wsdlUrls>

【讨论】:

  • 确实,没有 阻止 mvn install 仍然有效.......但是没有生成文件这个问题阻止了我一段时间
  • 你是最棒的,伙计!我已经浪费了两个小时来解决这个问题
【解决方案5】:

我看到有些人喜欢通过 jaxws-maven-plugin 将源代码生成到目标中,并通过 build-helper-maven-plugin 使这些类在源代码中可见。作为这种结构的论据

版本管理系统 (svn/etc.) 总是会注意到变化 来源

使用 git 是不正确的。因此,您只需配置 jaxws-maven-plugin 将它们放入您的源中,而不是在目标文件夹下。下次构建项目时,git 不会将这些生成的文件标记为已更改。这是只有一个插件的简单解决方案:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.6</version>

    <dependencies>
      <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-fluent-api</artifactId>
        <version>3.0</version>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-tools</artifactId>
        <version>2.3.0</version>
      </dependency>
    </dependencies>

    <executions>
      <execution>
        <goals>
          <goal>wsimport</goal>
        </goals>
        <configuration>
          <packageName>som.path.generated</packageName>
          <xjcArgs>
            <xjcArg>-Xfluent-api</xjcArg>
          </xjcArgs>
          <verbose>true</verbose>
          <keep>true</keep> <!--used by default-->
          <sourceDestDir>${project.build.sourceDirectory}</sourceDestDir>
          <wsdlDirectory>src/main/resources/META-INF/wsdl</wsdlDirectory>
          <wsdlLocation>META-INF/wsdl/soap.wsdl</wsdlLocation>
        </configuration>
      </execution>
    </executions>
  </plugin>

另外(请注意)在本示例中 SOAP 类是使用 Fluent API 生成的,因此您可以像这样创建它们:

A a = new A()
  .withField1(value1)
  .withField2(value2);

【讨论】:

    【解决方案6】:

    这里的关键是wsimport的keep选项。它是使用 element in 配置的 关于从 wsimport 文档中保留:

    -keep                     keep generated files
    

    【讨论】:

      【解决方案7】:

      我在从 wsimport 目标生成类时遇到了同样的问题。我没有在 Eclipse Maven Build 中使用 jaxws:wsimport 目标,而是使用无法从 wsdl 文件生成代码的干净编译安装。感谢上面的例子。 从 Eclipse ide 运行 jaxws:wsimport 目标,它会工作

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-03-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多