Search code examples
javagradlesoapwsdl2javacxf-codegen-plugin

Replace deprecated no.nils.wsdl2java Plugin in Gradle 7+ for WSDL class generation?


updating a Java application from Java 11 to 17, and as part of the upgrade I updated Gradle from version 6.8.3 to 8.0.2. The application uses Spring Boot version 2.7.18.

The application previously relied on the no.nils.wsdl2java Gradle plugin (version 0.12) to generate Java classes from a WSDL file. this plugin is no longer supported in Gradle 7+ so I need to replace it with another method.

previous Gradle configuration

buildscript {
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

plugins {
    id 'org.springframework.boot' version '2.7.12'
    id "org.sonarqube" version "4.3.1.3277"
    id 'java'
    id 'no.nils.wsdl2java' version "0.12"
}

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'org.sonarqube'
apply plugin: 'jacoco'
apply plugin: 'no.nils.wsdl2java'
apply plugin: 'project-report'

group = 'com.example.abc'
version = '0.0.1'
sourceCompatibility = '17'

wsdl2java {
    cxfVersion = "3.3.2"
    wsdlDir = file("src/main/resources/wsdl")
    wsdlsToGenerate = [['-autoNameResolution', "$wsdlDir/example.wsdl"]]
}
jar {
    enabled = false
}
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2021.0.5"
    }
}
dependencies {

    //===========SOAP===========
    implementation("com.sun.xml.bind:jaxb-impl:3.0.2")
    wsdl2java("com.sun.xml.bind:jaxb-impl:2.3.3")

    implementation "javax.xml.bind:jaxb-api:2.3.1"
    implementation "javax.xml.ws:jaxws-api:2.3.1"
    implementation "com.sun.xml.ws:rt:2.3.1"
    implementation "org.glassfish.jaxb:jaxb-runtime:2.3.2"
    implementation "org.glassfish.main.javaee-api:javax.jws:3.1.2.2"
    implementation "com.sun.xml.messaging.saaj:saaj-impl:1.5.1"
    implementation (group: 'com.github.ben-manes.caffeine', name: 'caffeine', version: '3.1.2'){
        exclude group: 'org.yaml', module: 'snakeyaml'
    }
    implementation group: 'com.fasterxml.jackson.core', name: 'jackson-annotations', version: '2.14.1'

    //===========SOAP===========

}


sourceSets {
    main {
        java {
            srcDirs "src/main/java"
        }
    }

    test {
        java {
            srcDirs "src/test/java"
        }

    }
}

tried removing the no.nils.wsdl2java plugin and replaced it with the io.mateo.cxf-codegen plugin. Added configuration to specify the WSDL location and output directory.

expected the io.mateo.cxf-codegen plugin to generate Java classes from the specified WSDL file in the provided directory, similar to how the no.nils.wsdl2java plugin functioned


Solution

  • You could call org.apache.cxf.tools.wsdlto.WSDLToJava using a JavaExec task

    eg:

    configurations {
       cxf
    }
    dependencies {
       cxf 'org.apache.cxf:apache-cxf:2.6.17'
    }
    tasks.register('wsdl2java', JavaExec) {
       ext {
          wsdlFile = "$projectDir/src/main/resources/MyWSDL.wsdl"
          outDir = "$buildDir/generated/wsdl2java"
       }
       main = 'org.apache.cxf.tools.wsdlto.WSDLToJava'
       classpath = configurations.cxf
       args ['-client', '-d', outDir, wsdlFile]
    
       // set task inputs/outputs for up-to-date checks
       inputs.file wsdlFile
       outputs.dir outDir
    }   
    

    See https://cxf.apache.org/docs/wsdl-to-java.html