【问题标题】:Gradle wsdl generatingGradle wsdl 生成
【发布时间】:2017-04-06 15:46:49
【问题描述】:

我想从 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.

我检查了语法(很多时候都很好)。我搜索了这个插件,很多人都在使用它。

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

更新:

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

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

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

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

如果我注释掉主build.gradle 中的apply plugin: 'no.nils.wsdl2java' 和子项目中的wsdl2java 方法,则gradle 工作正常。

【问题讨论】:

  • 您的代码运行良好,请提供更多信息。
  • 嗨!谢谢回复,我更新了描述。

标签: java gradle wsdl wsdl2java


【解决方案1】:

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

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

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'
    ....
}

【讨论】:

    【解决方案2】:

    我能够让 wsdl2 java 在我的 gradle 构建中使用 jaxwsant 任务。这里是

    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
    

    【讨论】:

      【解决方案3】:

      我使用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
      

      【讨论】:

      • 非常感谢您帮我节省了 20 天即将到来的努力!
      【解决方案4】:

      要解决这个问题,请尝试在插件声明之前添加 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'
      

      【讨论】:

        【解决方案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
                    }
                }
            }
        }
        

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

        我把它全部放在gist中,如果你想看到它完整,gist是here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2010-09-09
          • 2023-03-18
          • 1970-01-01
          • 2014-02-01
          • 2010-09-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多