I am trying to generate java classes using following code but it's failing for some gradle
plugin issue.
I searched for it and found there are many plugin available for generating the java classed from xsd
but only few plugins
for generating the code form wsdl
.
jaxb
is one of them which I thought to use.
Here is my build.gradle file:
configurations {
jaxws
}
buildscript {
ext {
springBootVersion = "2.1.4.RELEASE"
}
repositories {
mavenCentral()
}
dependencies {
classpath "org.springframework.boot:spring-boot-gradle-plugin:$springBootVersion"
jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter-actuator'
compile 'org.springframework.boot:spring-boot-starter-web'
compile 'org.springframework.boot:spring-boot-starter-web-services'
compile 'org.apache.httpcomponents:httpclient'
compile 'com.sun.xml.ws:jaxws-tools:2.1.4'
}
task wsimport {
ext.destDir = file("${projectDir}/src/main/generated")
doLast {
ant {
sourceSets.main.output.classesDir.mkdirs()
destDir.mkdirs()
taskdef(name: 'wsimport',
classname: 'com.sun.tools.ws.ant.WsImport',
classpath: configurations.jaxws.asPath
)
wsimport(keep: true,
destdir: sourceSets.main.output.classesDir,
sourcedestdir: destDir,
extension: "true",
verbose: "false",
quiet: "false",
package: "com.abc.test",
xnocompile: "true",
wsdl: '${projectDir}/src/main/resources/wsdls/wsdl_1.0.0.wsdl') {
xjcarg(value: "-XautoNameResolution")
}
}
}
}
compileJava {
dependsOn wsimport
source wsimport.destDir
}
bootJar {
baseName = 'API'
version = '1.0'
}
Now here is the error I am getting when I try to build project using command line.
C:\DEV\workspace\API>gradlew clean build --stacktrace
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\DEV\workspace\API\build.gradle' line: 14
* What went wrong:
A problem occurred evaluating root project 'API'.
> Could not find method jaxws() for arguments [com.sun.xml.ws:jaxws-tools:2.1.4] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Taking reference from this code; https://gist.github.com/ae6rt/8883335
configurations {
jaxws
}
buildscript {
dependencies {
jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
}
}
The configuration jaxws
is not applicable for build script dependencies. First, it is placed outside of the buildscript
configuration and thus not visible. Second, build script dependencies allow for classpath
configuration only (External dependencies for the build script). Removing jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
from the build script dependencies fixes issue
Could not find method jaxws() for arguments [...]
Next issue is that you define the jax-ws dependency as
compile 'com.sun.xml.ws:jaxws-tools:2.1.4'
and try to reference it as
taskdef(name: 'wsimport',
classname: 'com.sun.tools.ws.ant.WsImport',
classpath: configurations.jaxws.asPath)
^^^^^
The jaxws
configuration has no dependencies defined so far, thus the path is empty. Changing the dependency in question to
jaxws 'com.sun.xml.ws:jaxws-tools:2.1.4'
is likely to resolve this issue for you.
Update
Since Gradle replaced File classesDir
with FileCollection classesDirs
, as per your comment you're now receiving error
No signature of method: org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection.mkdirs() is applicable for argument types: () values: [] Possible solutions: min(), tails(), first(), inits(), minus(org.gradle.api.file.FileCollection), min(java.util.Comparator)
on line
sourceSets.main.output.classesDirs.mkdirs()
If you've got only 1 classes output dir, a workaround would be to use
sourceSets.main.output.classesDirs.singleFile.mkdirs()
(from: FileCollection.getSingleFile())