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

blu*_*sky 30 wsimport maven

当我尝试运行"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] ------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

我没有收到任何错误,但没有从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>
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么 ?该包com存在于项目'gensourcesfromwsdl'中,并且wsdl位置有效.

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

小智 36

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

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

target/generated/src/main/java/com/raps/code/generate/ws.
Run Code Online (Sandbox Code Playgroud)

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

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

Cas*_*ian 26

下面是一个如何使用来自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>  
Run Code Online (Sandbox Code Playgroud)

  • 使用目标jaxws运行maven:从IDE运行wsimport或在命令提示符下运行mvn jaxws:wsimport (3认同)

小智 6

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

它应该在

<build>
<plugins>           
        <plugin> 
Run Code Online (Sandbox Code Playgroud)