有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java渐变wsdl生成

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

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:

我有一个定义插件的主梯度,有三个子项目,我想在那里使用这个插件

我在settings.gradle中定义了子项目:

include 'project1', 'project2', 'project3'

我为每个项目制作了一个文件夹和build.gradle文件

如果我注释掉了主build.gradle中的apply plugin: 'no.nils.wsdl2java'和子项目中的wsdl2java方法,gradle工作得很好


共 (5) 个答案

  1. # 1 楼答案

    要解决这个问题,请尝试在插件声明之前添加buildScript,并在插件块之后应用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'
    
  2. # 2 楼答案

    我使用thisgit存储库完成了这个任务。建造。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. # 3 楼答案

    我能够使用jaxwsant任务使wsdl2java在我的gradle构建中工作。给你

    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
    
  4. # 4 楼答案

    subprojects-closure中添加buildscript,这是不受支持的,请参见this Gradle discussion (Buildscript {} in subprojects {} ignored?)

    您不必为每个项目添加构建脚本,只需在根构建中声明即可。格拉德尔

    buildscript{
        repositories{
            jcenter()
            mavenCentral()
        }
        dependencies {
            classpath 'no.nils:wsdl2java:0.10'
        }
    }
    
    subprojects {
        apply plugin: 'java'
        apply plugin: 'eclipse'
        apply plugin: 'maven-publish'
        apply plugin: 'application'
        apply plugin: 'no.nils.wsdl2java'
        ....
    }
    
  5. # 5 楼答案

    适用于任何希望使用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
                }
            }
        }
    }
    

    这只是简单地调用ApacheCXF类的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
                    }
            }
        }
    }
    

    我把这些都写进了gist,如果你想看到它的完整性,gist是here