在jar包装后,Spring-WS with Boot无法正常工作

时间:2016-09-21 12:47:32

标签: gradle spring-boot

我的项目分为几个模块:

包含实际网络服务的子模块。

依赖于子模块并定义自己的@SpringBootApplication类的几个模块:

version '1.0'

apply plugin: 'java'
apply plugin: 'spring-boot'

sourceCompatibility = 1.8

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile project(':submodule')
}

jar {
    baseName = 'jarfilename'
}

问题是,当运行jar文件时,子模块的JAXB任务生成的类无法访问:

javax.xml.bind.JAXBException: "bws.wsdl" doesnt contain ObjectFactory.class or jaxb.index
        at com.sun.xml.internal.bind.v2.ContextFactory.createContext(ContextFactory.java:266) ~[na:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_91]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_91]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_91]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:171) ~[na:1.8.0_91]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:131) ~[na:1.8.0_91]
        ... 31 common frames omitted

该项目从IDE(Intellij Idea)运行良好。

子模块的build.gradle如下所示:

version '1.0'

apply plugin: 'java'

sourceCompatibility = 1.8

configurations {
    jaxb
}

repositories {
    mavenCentral()
}

// tag::wsdl[]
task genJaxb {
    ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
    ext.classesDir = "${buildDir}/classes/jaxb"
    ext.schema = "path-to-wsdl.xml"
    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,
                    package: "wsdl") {
                arg(value: "-wsdl")
                produces(dir: sourcesDir, includes: "**/*.java")
            }

            javac(destdir: classesDir, source: 1.8, target: 1.8, 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")
                }
            }
        }
    }
}

dependencies {
    compile(files(genJaxb.classesDir).builtBy(genJaxb))

    jaxb "com.sun.xml.bind:jaxb-xjc:2.1.7"
}

task afterEclipseImport {
    dependsOn genJaxb
}

1 个答案:

答案 0 :(得分:0)

原来我只是忘了包含生成的类:

jar {
    baseName = 'jarfilename'
    from "${rootDir}/submodule/build/classes/jaxb"
}