如何使用Maven和wsimport从wsdl生成类?

时间:2013-08-20 14:50:49

标签: maven wsimport

当我尝试运行“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时:&gt; wsimport -keep -verbose http://mysite/firstwsdl.asmx?wsdl生成该类。

7 个答案:

答案 0 :(得分:35)

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

C:/Project root directory > mvn generate-sources

生成的java类可以位于文件夹

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

pom.xml片段

<basedir>
    C:/Project root directory
</basedir>

<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>${basedir}/target/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>${basedir}/src/main/resources/wsdl</wsdlDirectory>
        <packageName>com.raps.code.generate.ws</packageName>
        <keep>true</keep>
        <sourceDestDir>${basedir}/target/generated/src/main/java</sourceDestDir>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>wsimport</goal>
            </goals>
        </execution>
    </executions>
</plugin>

答案 1 :(得分:24)

以下是如何使用来自url或文件位置的jaxws maven插件从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>  

答案 2 :(得分:4)

即使这有点迟到,也可能对某人有所帮助。看起来你已经使用了pluginManagement。如果您使用pluginManagement ,它不会选择插件执行。

应该在

之下
<build>
<plugins>           
        <plugin> 

答案 3 :(得分:3)

尝试在wsdlUrls中包装wsdlLocation

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

答案 4 :(得分:1)

我看到有人喜欢通过jaxws-maven-plugin向目标生成源,并通过build-helper-maven-plugin使此类在源中可见。作为该结构的一个论证

  

版本管理系统(svn / etc。)总是会注意到已更改   来源

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

      <plugin>
        <groupId>org.jvnet.jax-ws-commons</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.3</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> 
        </configuration>
      </execution>
    </executions>
  </plugin>

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

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

答案 5 :(得分:0)

这里的关键是保留wsimport的选项。它使用元素配置 关于保持wsimport文档:

-keep                     keep generated files

答案 6 :(得分:0)

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