Last active
June 29, 2018 02:05
-
-
Save gmazzo/82d45ab496dd67260fb888c9d744ac53 to your computer and use it in GitHub Desktop.
WSDL SOAP JAX-WB client generation with Gradle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.gradle.process.internal.ExecActionFactory | |
import org.gradle.process.internal.JavaExecAction | |
import javax.inject.Inject | |
configurations { | |
jaxws | |
} | |
dependencies.add('jaxws', 'com.sun.xml.ws:jaxws-tools:2.2.10') | |
sourceSets.all { ss -> | |
def srcDir = file("src/${ss.name}/wsdl") | |
def genDir = file("$buildDir/generated/${ss.name}/java") | |
def suffix = ss.name == 'main' ? '' : ss.name.capitalize() | |
tasks.create("wsImport$suffix", WsImport.class, { self -> | |
wsdlDir = srcDir | |
outDir = genDir | |
onlyIf { srcDir.directory } | |
tasks[ss.compileJavaTaskName].dependsOn self | |
}) | |
ss.java.srcDirs genDir | |
} | |
class WsImport extends DefaultTask { | |
private final ExecActionFactory actionFactory | |
@InputDirectory | |
File wsdlDir | |
@OutputDirectory | |
File outDir | |
@TaskAction | |
void process() { | |
project.files(wsdlDir) | |
.asFileTree | |
.matching({ include '**/*.wsdl' }) | |
.each { processFile(it) } | |
} | |
@Inject | |
WsImport(ExecActionFactory actionFactory) { | |
this.actionFactory = actionFactory | |
} | |
void processFile(File file) { | |
JavaExecAction action = actionFactory.newJavaExecAction() | |
action.classpath = project.configurations['jaxws'] | |
action.main = 'com.sun.tools.ws.WsImport' | |
action.args = [ | |
file.absolutePath, | |
'-s', outDir.absolutePath, | |
'-extension', | |
'-Xnocompile', | |
logger.isDebugEnabled() ? '-Xdebug' : '-quiet'] | |
action.workingDir = wsdlDir | |
action.execute().rethrowFailure() | |
} | |
} | |
class DownloadWsdls extends DefaultTask { | |
@Input | |
Map<String, String> froms = new HashMap<>() | |
@OutputDirectory | |
File into | |
void from(String file, String url) { | |
froms[file] = url | |
} | |
void into(File into) { | |
this.into = into | |
} | |
@TaskAction | |
void perform() { | |
Set<URL> downloaded = [] | |
froms.each { k, v -> | |
def targetDir = new File(into, k) | |
def url = new URL(v) | |
def file = "${new File(url.path).name}.wsdl" | |
downloadAndProcess(targetDir, k, file, url, downloaded) | |
} | |
} | |
void downloadAndProcess(File targetDir, String entry, String file, URL url, Set<URL> downloaded) { | |
println "$entry: downloading: $url..." | |
// downloads the given WSDL | |
targetDir.mkdirs() | |
def targetFile = new File(targetDir, file) | |
targetFile.withOutputStream { os -> | |
os << url.openStream() | |
} | |
// parses the content and downloads the imports too | |
new XmlSlurper() | |
.parse(targetFile) | |
.'**'.findAll({ it.name() == 'import' }) | |
.each { | |
def name = [email protected]() | |
if (name) { | |
def importUrl = url.toURI().resolve("$name").toURL() | |
if (downloaded.add(importUrl)) { | |
downloadAndProcess(targetDir, entry, name, importUrl, downloaded) | |
} | |
} | |
} | |
} | |
} | |
// TODO this is a sample task, customize it with your own WSDLs | |
task downloadWsdls(type: DownloadWsdls) { | |
into file('src/main/wsdl') | |
from 'calculator', 'http://www.dneonline.com/calculator.asmx?wsdl' | |
from 'weather', 'https://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php?wsdl' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment