XSD和WSDL在不同的目录中

Ant*_*rev 7 wsdl java-ws wsimport maven

在我的工作中使用jaxws-maven-plugin进行代码生成.我有两个项目是"普通"和"客户".结构大致如下:

app/
  common/
    resource/
      some.xsd
  client/
    resource/
      some.wsdl
Run Code Online (Sandbox Code Playgroud)

如何使用项目"common"中的xsd在项目"client"中从wsdl生成类?

pom.xml中:

            <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                    <configuration>
                        <verbose>true</verbose>
                        <bindingFiles>
                            <bindingFile>${project.parent.basedir}/common/resource/some.xsd</bindingFile>
                        </bindingFiles>
                        <wsdlFiles>
                            <wsdlFile>/resource/some.wsdl</wsdlFile>
                        </wsdlFiles>
                    </configuration>
                </execution>
            </executions>
        </plugin>
Run Code Online (Sandbox Code Playgroud)

mab*_*aba 6

首先,您应该坚持maven约定,使用src/main/resources/目录获取资源.

完成后,您可以使用maven-dependency-plugin:unpack-dependencies解压缩commonjar文件来访问some.xsd:

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

    <parent>
        <groupId>com.stackoverflow.Q13155047</groupId>
        <artifactId>app</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>client</artifactId>

    <name>${project.artifactId}-${project.version}</name>

    <properties>
        <schema.location>${project.build.directory}/schemas</schema.location>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.stackoverflow.Q13155047</groupId>
            <artifactId>common</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.5.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includes>**/*.xsd</includes>
                            <outputDirectory>${schema.location}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                        <configuration>
                            <verbose>true</verbose>
                            <bindingDirectory>${schema.location}</bindingDirectory>
                            <bindingFiles>
                                <bindingFile>some.xsd</bindingFile>
                            </bindingFiles>
                            <wsdlDirectory>src/main/resources</wsdlDirectory>
                            <wsdlFiles>
                                <wsdlFile>some.wsdl</wsdlFile>
                            </wsdlFiles>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
Run Code Online (Sandbox Code Playgroud)

jaxws-maven-plugin被绑定到generate-sources阶段,因此加入maven-dependency-pluginjaxws-maven-plugin和同相确保它应用前解压一切wsimport的目标.

确保<bindingDirectory/>并且<wsdlDirectory/>正确无误.


如果你有*.xsd另一个项目中的文件,这就是你应该怎么做.切勿访问具有相对路径的其他项目.每个项目应该只使用依赖机制访问其他资源.

  • 根据此链接 - maven wsimport插件使用的默认位置是src/wsdl.我推断.wsdl不应该保存在src/main/resources中.https://jax-ws-commons.java.net/jaxws-maven-plugin/wsimport-mojo.html (2认同)
  • 当您解压到 ${schema.location} 目录时,如何引用 WSDL 中的 XSD?WSDL 如何知道在哪里可以找到 XSD? (2认同)