Skip to content

Loading A WSDL From A Maven Repository

Loading a WSDL from a remote artifact can be accomplished using native Gradle APIs as shown below. For example, to use the WSDL published in the cxf-testutils JAR artifact:

Workers

kotlin
import io.mateo.cxf.codegen.workers.Wsdl2JavaOption

val myConfiguration: Configuration by configurations.creating // <1>

dependencies {
    myConfiguration("org.apache.cxf:cxf-testutils:3.5.3") // <2>
}

val copyArtifact = tasks.register("copyArtifact", Copy::class) { // <3>
    from(myConfiguration) {
        include {
            it.name.startsWith("cxf-testutils")
        }
    }
    into(layout.buildDirectory.dir("copied-artifact"))
}

val extractWsdl = tasks.register("extractWsdl") { // <4>
    val wsdlFile = layout.buildDirectory.file("extracted.wsdl")
    inputs.files(copyArtifact)
    outputs.file(wsdlFile)
    doLast {
        // Assumes single JAR artifact from previous task
        val archive = inputs.files.singleFile.walk().filter { it.isFile }.single()
        val textResource = resources.text.fromArchiveEntry(archive, "wsdl/calculator.wsdl")
        val f = wsdlFile.get().asFile
        f.createNewFile()
        f.writeText(textResource.asString())
    }
}

cxfCodegen {
    options {
        register<Wsdl2JavaOption>("calculator") {
            wsdl = extractWsdl.map { it.outputs.files.singleFile.toPath().toAbsolutePath().toString() } // <5>
        }
    }
}
groovy
import io.mateo.cxf.codegen.workers.Wsdl2JavaOption

configurations {
    myConfiguration // <1>
}

dependencies {
    myConfiguration "org.apache.cxf:cxf-testutils:4.0.0" // <2>
}

def copyArtifact = tasks.register("copyArtifact", Copy) { // <3>
    from(configurations.myConfiguration) {
        include {
            it.name.startsWith("cxf-testutils")
        }
    }
    into(layout.buildDirectory.dir("copied-artifact"))
}

def extractWsdl = tasks.register("extractWsdl") { // <4>
    def wsdlFile = layout.buildDirectory.file("extracted.wsdl")
    inputs.files(copyArtifact)
    outputs.file(wsdlFile)
    doLast {
        // Assumes single JAR artifact from previous task
        def list = []
        inputs.files.singleFile.eachFileRecurse(groovy.io.FileType.FILES) { f -> list.add(f) }
        def archive = list.first()
        def textResource = resources.text.fromArchiveEntry(archive, "wsdl/calculator.wsdl")
        def f = wsdlFile.get().asFile
        f.createNewFile()
        f.write(textResource.asString())
    }
}

cxfCodegen {
    options {
        register("calculator", Wsdl2JavaOption) {
            wsdl = extractWsdl.map { it.outputs.files.singleFile.toPath().toAbsolutePath().toString() } // <5>
        }
    }
}
  1. Define a configuration to hold the dependency.
  2. Add the dependency containing a WSDL to the configuration.
  3. Define a task to copy the dependency that contains the WSDL.
  4. Extract the WSDL from the artifact.
  5. Use the task output as input to option configuration.

Task

kotlin
import io.mateo.cxf.codegen.wsdl2java.Wsdl2Java

val myConfiguration: Configuration by configurations.creating // <1>

dependencies {
    myConfiguration("org.apache.cxf:cxf-testutils:3.5.3") // <2>
}

val copyArtifact = tasks.register("copyArtifact", Copy::class) { // <3>
    from(myConfiguration) {
        include {
            it.name.startsWith("cxf-testutils")
        }
    }
    into(layout.buildDirectory.dir("copied-artifact"))
}

val extractWsdl = tasks.register("extractWsdl") { // <4>
    val wsdlFile = layout.buildDirectory.file("extracted.wsdl")
    inputs.files(copyArtifact)
    outputs.file(wsdlFile)
    doLast {
        // Assumes single JAR artifact from previous task
        val archive = inputs.files.singleFile.walk().filter { it.isFile }.single()
        val textResource = resources.text.fromArchiveEntry(archive, "wsdl/calculator.wsdl")
        val f = wsdlFile.get().asFile
        f.createNewFile()
        f.writeText(textResource.asString())
    }
}

tasks.register("calculator", Wsdl2Java::class) {
    inputs.files(extractWsdl)
    toolOptions {
        wsdl = extractWsdl.map { it.outputs.files.singleFile.toPath().toAbsolutePath().toString() }  // <5>
    }
}
groovy
import io.mateo.cxf.codegen.wsdl2java.Wsdl2Java

configurations {
    myConfiguration // <1>
}

dependencies {
    myConfiguration "org.apache.cxf:cxf-testutils:4.0.0" // <2>
}

def copyArtifact = tasks.register("copyArtifact", Copy) { // <3>
    from(configurations.myConfiguration) {
        include {
            it.name.startsWith("cxf-testutils")
        }
    }
    into(layout.buildDirectory.dir("copied-artifact"))
}

def extractWsdl = tasks.register("extractWsdl") { // <4>
    def wsdlFile = layout.buildDirectory.file("extracted.wsdl")
    inputs.files(copyArtifact)
    outputs.file(wsdlFile)
    doLast {
        // Assumes single JAR artifact from previous task
        def list = []
        inputs.files.singleFile.eachFileRecurse(groovy.io.FileType.FILES) { f -> list.add(f) }
        def archive = list.first()
        def textResource = resources.text.fromArchiveEntry(archive, "wsdl/calculator.wsdl")
        def f = wsdlFile.get().asFile
        f.createNewFile()
        f.write(textResource.asString())
    }
}

tasks.register("calculator", Wsdl2Java) {
    inputs.files(extractWsdl)
    toolOptions {
        wsdl = extractWsdl.map { it.outputs.files.singleFile.toPath().toAbsolutePath().toString() } // <5>
    }
}
  1. Define a configuration to hold the dependency.
  2. Add the dependency containing a WSDL to the configuration.
  3. Define a task to copy the dependency that contains the WSDL.
  4. Extract the WSDL from the artifact.
  5. Use the task output as input to the Wsdl2Java task.

Released under Apache 2.0 license