Gradle wsdl生成

时间:2017-04-06 15:46:50

标签: java gradle wsdl wsdl2java

我想从wsdl生成java文件。我尝试使用wsdl2java gradle插件。我定义了插件:

subprojects {
buildscript{
    repositories{
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}


apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'application'
apply plugin: 'no.nils.wsdl2java'
....
}

但是我收到了这个错误:

> Plugin with id 'no.nils.wsdl2java:no.nils:wsdl2java' not found.

我检查了语法(很多次都很好)。我用谷歌搜索插件,它被许多人使用。

有人知道出了什么问题吗?

UPD:

我有一个插件定义的主gradle,并且有三个子项目,我想使用这个插件。

我在settings.gradle

中定义了子项目
include 'project1', 'project2', 'project3'

我为每个项目创建了一个文件夹和build.gradle文件。

如果我在主要apply plugin: 'no.nils.wsdl2java'中注明了build.gradle,并在子项目中注释了wsdl2java方法,则gradle可以正常工作。

5 个答案:

答案 0 :(得分:5)

您在public interface WindowFunction<IN, OUT, KEY, W extends Window> extends Function, Serializable { /** * Evaluates the window and outputs none or several elements. * * @param key The key for which this window is evaluated. * @param window The window that is being evaluated. * @param input The elements in the window being evaluated. * @param out A collector for emitting elements. * * @throws Exception The function may throw exceptions to fail the program and trigger recovery. */ void apply(KEY key, W window, Iterable<IN> input, Collector<OUT> out) throws Exception; } - 封闭内添加buildscript,不支持,请参阅this Gradle discussion (Buildscript {} in subprojects {} ignored?)

您不必为每个项目添加buildscript,只需在root-build.gradle上声明它就足够了

subprojects

答案 1 :(得分:2)

我能够使用 jaxws ant 任务在 gradle 构建中使用 wsdl2 java。这是

apply plugin: 'java'

repositories {
    mavenCentral()
    flatDir {
       dirs 'lib'
   }
}

configurations { jaxws }
dependencies { jaxws 'com.sun.xml.ws:jaxws-tools:2.2.6' }

dependencies {
    compile 'com.sun.xml.bind:jaxb-impl:2.2.6'
}


task generateSCMOrderImportServiceClient{
    if(!file("./lib/employee-services-client.jar").exists()) {

        def rootDir = file("build/wsdlToJava/employee-services-client");
        def javaDir = file("${rootDir}/java");
        def wsdlJarDir = file("${projectDir}/lib");
        def classesDir = file("${rootDir}/classes");
        def wsdlDir=file("${projectDir}/src/main/resources/wsdl");
        def wsdlFile = file("${wsdlDir}/employee-services.wsdl")

        doLast{
            classesDir.mkdirs()
            javaDir.mkdirs()
            wsdlJarDir.mkdirs()
            copy {
                from "${wsdlFile}"
                into "${classesDir}"
            }

            ant {
                    taskdef(name: 'wsimport',
                            classname: 'com.sun.tools.ws.ant.WsImport',
                            classpath: configurations.jaxws.asPath)
                    wsimport(keep: true,
                            destdir: classesDir,
                            sourcedestdir: javaDir,
                            extension: "true",
                            verbose: "true",
                            quiet: "false",
                            xnocompile: "false",
                            xendorsed: true,
                            wsdlLocation: "EmployeeServices.wsdl",
                            wsdl: "${wsdlFile}") 
                    {
                        binding(dir:"${wsdlDir}", includes:"jaxb-bindings.xml,jaxws-bindings.xml")
                        xjcarg(value: "-XautoNameResolution")
                    }
            }

            ant.jar(
                    destfile: wsdlJarDir.path + "/employee-services-client.jar",
                    basedir: classesDir
            )
        }
     }

}



compileJava.dependsOn generateSCMOrderImportServiceClient

答案 2 :(得分:0)

我已经使用this git存储库完成了此任务。 build.gradle文件看起来像这样。

buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}

plugins {
    id 'org.springframework.boot' version '2.2.0.M6'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'no.nils.wsdl2java' version '0.10'
}

group = 'your.application.groupname'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
    maven { url 'https://repo.spring.io/milestone' }
}

dependencies {
    wsdl2java(
            'com.sun.xml.bind:jaxb-xjc:2.3.0.1',
            'javax.xml.bind:jaxb-api:2.3.1',
            'javax.xml.ws:jaxws-api:2.3.1',
            'org.apache.cxf:cxf-rt-wsdl:3.2.7',
            'javax.jws:javax.jws-api:1.1',

            'com.sun.xml.bind:jaxb-core:2.3.0.1',
            'com.sun.xml.bind:jaxb-xjc:2.3.2',
            'com.sun.xml.bind:jaxb-impl:2.3.2',
            'javax.xml.bind:jaxb-api:2.3.1'
    )

    implementation 'com.sun.xml.bind:jaxb-core:2.3.0.1'
    implementation 'com.sun.xml.bind:jaxb-xjc:2.3.0.1'
    implementation 'com.sun.xml.bind:jaxb-impl:2.3.2'
    implementation 'javax.xml.bind:jaxb-api:2.3.1'
    implementation 'javax.xml.ws:jaxws-api:2.3.1'
    implementation 'org.apache.cxf:cxf-rt-wsdl:3.2.7'
    implementation 'javax.jws:javax.jws-api:1.1'
}

test {
    useJUnitPlatform()
}

wsdl2java {
    wsdlsToGenerate = [
            ['-p', 'your.package.name',
             '-autoNameResolution', "$projectDir/src/main/resources/wsdl/some_wsdl_file.wsdl"]
    ]
    generatedWsdlDir = file("$projectDir/src/main/java")
    wsdlDir = file("$projectDir/src/main/resources/wsdl")
    locale = Locale.ENGLISH
}

wsdl2javaExt {
    cxfVersion = "2.5.1"
}

要生成Java代码,我们需要运行gradle任务,如下所示。

$ gradlew wsdl2java

答案 3 :(得分:0)

要解决此问题,请尝试在插件声明之前添加buildScript,并在 plugin块之后应用wsdl插件,例如:

    buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'no.nils:wsdl2java:0.10'
    }
}

plugins {
    id 'org.springframework.boot' version '2.1.7.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'war'
    id "org.sonarqube" version "2.7"
}

apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'

答案 4 :(得分:0)

对于任何希望使用Apache CXF生成的人。我试图避免使用任何插件。我通过创建具有依赖项的配置,带有wsdl文件名的字符串的外部化数组来完成配置,然后像这样使用它:

ext {
    wsdlDir = file("${projectDir}/src/main/resources/wsdl")
    outputDir = file("$buildDir/generated-sources")
    sourceWsdls = [
            "$wsdlDir/MyWsdlFile1.wsdl",
            "$wsdlDir/MyWsdlFile2.wsdl",
            "$wsdlDir/MyWsdlFile3.wsdl",
            "$wsdlDir/MyWsdlFile4.wsdl"
    ]
}

sourceSets.main.java.srcDirs += "$outputDir"

dependencies {
    cxf (
            'org.apache.cxf:cxf-tools-wsdlto-core:3.3.6',
            'org.apache.cxf:cxf-tools-wsdlto-frontend-jaxws:3.3.6',
            'org.apache.cxf:cxf-tools-wsdlto-databinding-jaxb:3.3.6'
    )
}

task generateJavaClasses {
    doLast{
        sourceWsdls.each { wsdlFile ->
            javaexec {
                classpath configurations.cxf
                main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
                args '-d', outputDir
                args '-b', 'PATH/TO/BINDING/FILE.xjb'
                args wsdlFile
            }
        }
    }
}

这只是简单地调用Apache CXF类的javaexec-有关所有参数和选项,请参见docs

注意:如果您有一个包含wsdl文件的文件夹,并且想从那里的所有wsdl文件中生成类,它甚至会更容易使用,就像这样使用它:

task generateJavaClassesAllWsdlFiles {
    doLast{
        // Find all wsdl files in directory defined in ext
        fileTree(wsdlDir).matching {
            include "*.wsdl"
        }.each {
            wsdlFile ->
                println "Generating " + wsdlFile
                javaexec {
                    classpath configurations.cxf
                    main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
                    args '-d', outputDir
                    args '-b', 'PATH/TO/BINDING/FILE.xjb'
                    args wsdlFile
                }
        }
    }
}

我把所有内容都放入了摘要中,如果您想看完整的话,摘要是here