以编程方式将WsImport与没有Maven或ANT的JAXB插件一起使用?

pat*_*rit 11 java jaxb jaxb2 wsimport jaxb2-basics

WsImport用来从远程WSDL文件生成一些Java源代码.请注意,这是来自常规Scala项目,即它不是在Maven或Ant构建中完成的:

import com.sun.tools.ws.WsImport

def run(wsdlFile: File, destination: File, packageName: String = "generated"): Seq[File] = {        
  sys.props("javax.xml.accessExternalDTD") = "all"
  sys.props("javax.xml.accessExternalSchema") = "all"
  val xjcArgs = "" //TODO
  val args = s"-Xnocompile -XadditionalHeaders $xjcArgs -J-Djavax.xml.accessExternalDTD=all -b http://www.w3.org/2001/XMLSchema.xsd -p $packageName -s $destination $wsdlFile"
  WsImport.doMain(args.split(' '))
}
Run Code Online (Sandbox Code Playgroud)

上面的代码工作得很好,我用它从上面的Scala代码以编程方式生成Java WSDL客户端.

但是,现在,我还想使用一些WsImport插件(例如thisthis):

val xjcArgs = "-B-Xequals -B-XhashCode -B-Xvalue-constructor"
Run Code Online (Sandbox Code Playgroud)

我收到此错误:no such JAXB option: -Xequals即使我将以下内容添加到我的类路径中:

"org.jvnet.jaxb2_commons" % "jaxb2-basics" % "1.11.1",
"org.jvnet.jaxb2_commons" % "jaxb2-value-constructor" % "3.0",
Run Code Online (Sandbox Code Playgroud)

如何强制WsImport使用这些插件?或者我是否直接使用除WsImport(如ANT)之外的其他工具?

尝试使用ANT

我在com.sun.tools.ws.WsImport上面使用,但也有另一个com.sun.tools.ws.ant.WsImport,我不能确定如何使用它.我试过这个:

val task = new com.sun.tools.ws.ant.WsImport2()
task.setPackage(packageName)
task.setWsdl(wsdlFile.getAbsolutePath)
task.setDestdir(destination.getAbsoluteFile)
task.setGenerateJWS(true)
task.setXadditionalHeaders(true)
task.setXnocompile(true)
task.setBinding("http://www.w3.org/2001/XMLSchema.xsd")
task.execute()
Run Code Online (Sandbox Code Playgroud)

以上"几乎可行"但我无法弄清楚如何在ant任务中设置绑定.wsimport接受一个-b http://www.w3.org/2001/XMLSchema.xsd但是ant任务只接受文件作为参数:(

Opt*_*nal 3

您可以修改您的val xjcArgs以包含类路径吗?例如

val xjcArgs = "-cp _path_ofjaxb2-basics.jar:other_plugin_jar_with_path -B-Xequals -B-XhashCode -B-Xvalue-constructor"确保在 WSImport 调用上设置类路径?

编辑

基于评论:这是我的 ant 命令。基本上你还需要所有的支持罐子。

<project name="jaxws-stack" default="wsimport">
<property name="jaxws.home" location="D:/tmp/wsimport"/>
<path id="wsimport.classpath">
    <fileset dir="${basedir}/lib">
            <include name="*.jar"/>
        </fileset>
</path>

<taskdef name="wsimport" classname="com.sun.tools.ws.ant.WsImport" classpathref="wsimport.classpath"/>

<target name="wsimport">
    <wsimport keep="true" verbose="true" wsdl="mywsdlurl_path">
        <xjcarg value="-Xequals"/>
        <xjcarg value="-XhashCode"/>
        <xjcarg value="-Xvalue-constructor"/>
    </wsimport>
</target>
Run Code Online (Sandbox Code Playgroud)

在我的 D:/tmp.wsimport 中,我有以下罐子:

       233,859 commons-beanutils-1.9.2.jar
       315,805 commons-lang3-3.1.jar
       293,543 commons-lang3-3.1.jar.zip
        60,686 commons-logging-1.1.1.jar
        23,205 istack-commons-runtime-2.21.jar
         7,595 istack-commons-tools-1.1.jar
        25,446 istack-commons-tools-2.21.jar
       866,992 jaxb-impl-2.1.10.jar
     3,147,690 jaxb-xjc-2.1.11.jar
       141,087 jaxb2-basics-1.11.1.jar
       166,736 jaxb2-basics-runtime-1.11.1.jar
       141,604 jaxb2-basics-tools-1.11.1.jar
         5,756 jaxb2-value-constructor-3.0.jar
     1,284,131 jaxws-rt-2.1.4.jar
       510,892 jaxws-tools-2.1.4.jar
        33,739 stax-ex-1.7.7.jar
       130,161 streambuffer-0.9.jar
Run Code Online (Sandbox Code Playgroud)

只需调用 Ant 的默认目标,您将获得正确的 java 文件。

编辑2以支持命令行/编程调用。

选项1:

仅调用 WSImport。

"jdk_location\bin\java.exe"    -cp "lib\*" com.sun.tools.ws.WsImport -keep -verbose -B-Xequals -B-XhashCode -B-Xvalue-constructor  http://www.webservicex.com/globalweather.asmx?WSDL
Run Code Online (Sandbox Code Playgroud)

选项2

通过自定义类调用。

Java类:

package test;

import com.sun.tools.ws.WsImport;

public class Test
{
    public static void main(String args []) throws Throwable
    {
        WsImport.doMain(args); 
    }
}
Run Code Online (Sandbox Code Playgroud)

命令提示符调用

"jdk_location\bin\java.exe" -cp ".;lib\*" test.Test  -keep -verbose -B-Xequals -B-XhashCode -B-Xvalue-constructo
r  -Xnocompile http://www.webservicex.com/globalweather.asmx?WSDL
Run Code Online (Sandbox Code Playgroud)

选项2c

再次是一个自定义的 java 类/方法,您可以在 scala 中使用它。确保您已使用我列出的 jar 正确设置了类路径。

package test;

import com.sun.tools.ws.WsImport;
public class MyWSImport {

    public static void main(String[] args) throws Throwable {

        String [] input = new String[] {"-keep",
                                "-verbose","-B-Xequals",
                                "-B-XhashCode",
                                "-B-Xvalue-constructor","-Xnocompile",
                                "http://www.webservicex.com/globalweather.asmx?WSDL"};
        WsImport.doMain(input); 

    }

    public void execute() throws Throwable
    {

        String [] input = new String[] {"-keep",
                                "-verbose","-B-Xequals",
                                "-B-XhashCode",
                                "-B-Xvalue-constructor","-Xnocompile",
                                "http://www.webservicex.com/globalweather.asmx?WSDL"};
        WsImport.doMain(input); 
    }
}
Run Code Online (Sandbox Code Playgroud)