Eclipse Gradle项目:JAXB不会从嵌套的WSDL文件创建Java类

时间:2018-11-27 11:20:17

标签: java gradle jaxb wsdl xjc

我有2个.wsdl文件和4个.xsd文件。 wsdl1导入wsdl2wsdl2导入所有4个xsd文件。 我在Eclipse Neon中有一个Gradle项目,想使用

task genJaxb {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = "src/main/resources/wsdl1.wsdl"

    outputs.dir classesDir

    doLast() {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir(dir: sourcesDir)
            mkdir(dir: classesDir)

            xjc(destdir: sourcesDir, schema: schema) {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }

            javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
                    debugLevel: "lines,vars,source",
                    classpath: configurations.jaxb.asPath) {
                src(path: sourcesDir)
                include(name: "**/*.java")
                include(name: "*.java")
            }

            copy(todir: classesDir) {
                fileset(dir: sourcesDir, erroronmissingdir: false) {
                    exclude(name: "**/*.java")
                }
            }
        }
    }
}

从wsdl文件生成Java类。它不起作用,但是Gradle将jaxb任务显示为绿色。如果我改为执行ext.schema = "src/main/resources/wsdl2.wsdl",则它会成功生成类,但是我缺少wsdl1中的内容。

答案后编辑:

task generateJava {
    inputs.file = "src/main/resources/wsdl1.wsdl"
    outputs.dir = "${buildDir}/generated/java"
    doLast {
        project.ant {
            taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
                    classpath: configurations.jaxb.asPath
            mkdir" ${buildDir}/generated/java"

            xjc(destdir: file(${buildDir}/generated/java).absolutePath, schema: inputs) {
                arg(value: "-wsdl")
                produces(dir: outputs, includes: "**/*.java")
            }
        }
    }
} 
sourceSets.main.java {
    srcDir "{$buildDir}/generated/java"
}
compileJava.dependsOn generateJava

抛出以下错误:No such property: file for class: org.gradle.api.internal.tasks.DefaultTaskInputs Possible solutions: files

2 个答案:

答案 0 :(得分:0)

您并不是在以Gradle的方式思考。这是您应该做的:

  1. 在$ buildDir下的目录中生成Java文件,以便通过“清理”任务对其进行清理(永远不要在src / main / java中生成)
  2. 将生成的目录添加到“主” SourceSet(因此由“ java”插件中的compileJava任务编译)
  3. 使“ compileJava”任务取决于您的任务
  4. 确保在任务上配置任务输入/输出以支持UP-TO-DATE跳过

例如:

apply plugin: 'java' 
task generateJava {
    inputs.file 'src/main/resources/mywsdl.wsdl'
    outputs.dir "$buildDir/generated/java"
    doLast {
        mkdir "$buildDir/generated/java"
        ant {
           taskdef(name: 'xjc', ...)
           xjc(destdir: "$buildDir/generated/java", ...) {
             ... 
           } 
        } 
    }
} 
sourceSets.main.java {
    srcDir "$buildDir/generated/java"
}
compileJava.dependsOn generateJava

答案 1 :(得分:0)

我遇到了完全相同的问题。多亏了本教程,它才有效! https://jaxenter.com/tutorial-gradle-soap-features-revealed-104493.html

简而言之,我没有使用xjc插件来创建Java类,而是使用了wsimport,它就像一个魅力。

configurations {
    jaxws
}

task genWs {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = ""//source to your wsdl file here

    outputs.dir classesDir

    doLast() {
        project.ant {
            taskdef (name:'wsimport',
                classname:'com.sun.tools.ws.ant.WsImport',
                classpath:configurations.jaxws.asPath)
            mkdir(dir: sourcesDir)
            mkdir(dir: classesDir)

            wsimport(keep:true,
                destdir: classesDir,
                sourcedestdir: sourcesDir,
                wsdl: schema)
        }
    }
}

dependencies {

    jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
}

compileJava {
    dependsOn genWs
}