Search code examples
gradlexsdxjc

xjc, generate java classes from xsd (using URL) in gradle 7.2


I use plugin com.github.bjornvester.xjc to generate java classes from xsd:

xjc {
    xjcVersion.set("2.3.3")
    outputJavaDir = file("${buildDir}/generated-sources/jaxb")
    
    ext.downloaded = file("$buildDir/xjc/downloaded/schema2.wsdl")
    doFirst {
        mkdir downloaded.parentFile
        downloaded.text = new URL("http://www.example.com/foo.xsd").text}
    
    groups {
        register("schema1") {
            xsdFiles = files(xsdDir.file("${projectDir}/src/main/resources/wsdl/schema1.wsdl"))
            defaultPackage.set("pl.com.project.schema1")
        }
        register("schema2") {
            xsdFiles = files(downloaded)
            defaultPackage.set("pl.com.project.schema2")
        }
    }
}

And I got an error in line "xjc {" : enter image description here


Solution

  • In my previous attempt I incorrectly assumed that xjc was a task. After looking at the github page I can see that "xjc" is an extension object, not a task

    So try this:

    tasks.register('downloadXsd') {
       ext.xsd = file("$buildDir/downloadXsd/foo.xsd")
       outputs.file xsd // important!!! configures the task outputs
       doLast {
          mkdir xsd.parentFile
          xsd.text = new URL("http://www.example.com/foo.xsd").text
       }
    }
    xjc {
        ...
        groups {
            register("schema1") {
                // assuming the plugin is written properly, this should configure a task dependency
                xsdFiles = files(tasks.named('downloadXsd'))
                ...
            }
            ...
        }
    }
    

    You could improve this using the download task to download the xsd which shows progress of the download and also has caching options