Created
June 17, 2020 20:10
-
-
Save hrabosch/4bd1bd90256fa4bb4179d2088f2a1241 to your computer and use it in GitHub Desktop.
Template for using Apache CXF to generate java classes from wsdl
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
plugins { | |
id 'java' | |
} | |
group 'org.hrabosch' | |
version '1.0-SNAPSHOT' | |
sourceCompatibility = 1.8 | |
// Create configuration to wrap our dependencies. Then they can be easily put into classpath for execution java (WSDLToJava class) | |
configurations { | |
cxf | |
} | |
repositories { | |
mavenCentral() | |
} | |
// Externalize basic variables which will be used here | |
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" | |
] | |
} | |
// Do not forget this part to be sure our generated java classes are available in project as source. | |
sourceSets.main.java.srcDirs += "$outputDir" | |
// Basic set of dependencies - not all are required. Also possible to add dependencies based on plugins which u'd like to use during generating using args options (toString, equals, etc.) | |
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', | |
'org.slf4j:slf4j-simple:1.7.30' | |
) | |
} | |
// Use wsdl files defined in sourceWsdls array and generate classes | |
task generateJavaClasses { | |
doLast{ | |
sourceWsdls.each { wsdlFile -> | |
println "Generating " + wsdlFile | |
// Execute Java - Here WSDLToJava class with added dependencies on classpath via configuration | |
javaexec { | |
classpath configurations.cxf | |
main = 'org.apache.cxf.tools.wsdlto.WSDLToJava' | |
args '-d', outputDir | |
// Here it is solved by one binding file, it can be changed to use different for each wsdl in many ways - like keep binding file name in array together with wsdl file name. In ext it would be 2D array | |
args '-b', 'PATH/TO/BINDING/FILE.xjb' | |
args wsdlFile | |
} | |
} | |
} | |
} | |
// Use ALL wsdl files in defined directory and generated classes | |
task generateJavaClassesAllWsdlFiles { | |
doLast{ | |
// Find all wsdl files in directory defined in ext | |
fileTree(wsdlDir).matching { | |
include "*.wsdl" | |
}.each { | |
wsdlFile -> | |
// Do conversion as above | |
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 | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment