使用Gradle时,Apache cxf WSDLToJava生成的源代码将无法编译-缺少jakarta包,但引用了依赖项

goqiplq2  于 2023-03-13  发布在  Apache
关注(0)|答案(1)|浏览(204)

编译生成的Java时找不到Jakarta包。Jakarta依赖项在build.gradle中为模块引用:

plugins {
    id 'java-library'
}
configurations {
    wsdl2java
}

dependencies {
    wsdl2java 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    wsdl2java 'com.sun.xml.bind:jaxb-ri:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-xjc:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-core:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-impl:4.0.2'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-core:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:4.0.0'
}

生成代码的Gradle包括

classpath configurations.wsdl2java

生成的示例代码包括未解析的导入

import jakarta.xml.bind.annotation.XmlAccessType;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlType;

当任务尝试编译时,错误为:

error: package jakarta.xml.bind.annotation does not exist
import jakarta.xml.bind.annotation.XmlAccessType;

然而,所有这些都应该包括在

jakarta.xml.bind:jakarta.xml.bind-api:4.0.0

完整的build.gradle文件为:

plugins {
    id 'java-library'
}
configurations {
    wsdl2java
}

dependencies {
    wsdl2java 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    wsdl2java 'com.sun.xml.bind:jaxb-ri:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-xjc:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-core:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-impl:4.0.2'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-core:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:4.0.0'
}

def allmyWsdl = task allmyWsdl {
    String[] wsdls = ["one", "two", "three"];

    wsdls.each {name->
        def thing = task "myWsdl${name}" (type: JavaExec) {
            // Defining these mean the task can run incrementally
            inputs.files("${projectDir}/src/main/resources")
            outputs.dir("${projectDir}/out/generated-sources/cxf-ws")

            String wsdl = "${projectDir}/src/main/resources/wsdls/${name}.wsdl"
            String genSrcDir = "${projectDir}/out/generated-sources/cxf-ws"

            inputs.file wsdl
            outputs.dir genSrcDir

            classpath configurations.wsdl2java
            main "org.apache.cxf.tools.wsdlto.WSDLToJava"

            args '-client', '-impl', '-exsh', 'true', '-noAddressBinding', '-autoNameResolution', "-wsdlLocation", "classpath:wsdls/${name}.wsdl", '-p', "com.my.webservices.${name}", '-encoding', 'UTF-8', '-d', genSrcDir, wsdl

            final OutputStream baos = new ByteArrayOutputStream()
            errorOutput = new OutputStream() {
                void write(int b) {System.err.write(b); baos.write(b) }
                void flush() { System.err.flush(); baos.flush() }
                void close() { System.err.close(); baos.close() }
            }

            doLast {
                def str = baos.toString()
                if (str.contains('Usage : wsdl2java') || str.contains('WSDLToJava Error')) {
                    throw new TaskExecutionException(tasks[name],
                            new IOException('Apache CXF WSDLToJava has failed. Please see System.err output.'))
                }
            }
        }
    }
}

allmyWsdl.dependsOn(tasks.matching { task -> task.name.startsWith( 'myWsdl' ) } )
compileJava.dependsOn += allmyWsdl
sourceSets.main.java.srcDirs += 'out/generated-sources/cxf-ws'
noj0wjuj

noj0wjuj1#

问题是当从wsdl生成java时,你把所有的库都添加到类路径classpath configurations.wsdl2java中,但是当编译这些库时,它们不在类路径中,所以你会得到这些错误。
您需要调整dependencies截面,如下所示。

dependencies {
    implementation 'jakarta.xml.bind:jakarta.xml.bind-api:4.0.0'
    implementation 'jakarta.xml.ws:jakarta.xml.ws-api:4.0.0'
    wsdl2java 'com.sun.xml.bind:jaxb-ri:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-xjc:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-core:4.0.2'
    wsdl2java 'com.sun.xml.bind:jaxb-impl:4.0.2'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-core:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:4.0.0'
    wsdl2java 'org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:4.0.0'
}

由于jakarta.xml.bind-apijakarta.xml.ws-api库在生成Java类时不需要。但在编译时需要。因此添加了这两个库作为实现依赖项,这增加了Java编译和运行时类路径。

相关问题