Spring Boot Web Services Client with Jib

Posted on October 18, 2020
Tags: java, programming

When it comes to building a SOAP web services client using Spring, the guide suggests using an Ant based gradle build task to generate the required classes. However, if you are using Jib to produce a Docker container, you may find that the generated classes are not in the container, and there are few guides as to how to get them in there.

It looks something like this:

...
task genJaxb {
	ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
	ext.classesDir = "${buildDir}/classes/jaxb"
	ext.schema = "http://localhost:8080/ws/countries.wsdl"

	outputs.dir classesDir

	doLast() {
		project.ant {
			taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
					classpath: configurations.jaxb.asPath
			mkdir(dir: sourcesDir)
			mkdir(dir: classesDir)

				xjc(destdir: sourcesDir, schema: schema,
						package: "com.example.consumingwebservice.wsdl") {
						arg(value: "-wsdl")
					produces(dir: sourcesDir, includes: "**/*.java")
				}

				javac(destdir: classesDir, source: 1.8, target: 1.8, debug: true,
						debugLevel: "lines,vars,source",
						classpath: configurations.jaxb.asPath) {
					src(path: sourcesDir)
					include(name: "**/*.java")
					include(name: "*.java")
					}

				copy(todir: classesDir) {
						fileset(dir: sourcesDir, erroronmissingdir: false) {
						exclude(name: "**/*.java")
				}
			}
		}
	}
}
...

The crux of the issue is getting the generated classes onto the main source set so that Jib will package them up. I’m not a Gradle expert, so while there may be a way to make this happen with this setup, the neater solution appeared to be to us the XJC Gradle Plugin which takes care of a lot of this automatically.

The basics of my usage was:

  1. Copy the WSDL file to a new folder, src/main/schemas/xjc
  2. Setting up the xjcGeneration in build.gradle to put generate the schema and put it in the source set
plugins {
    id 'com.github.edeandrea.xjc-generation' version '1.5'
}

ext {
    jaxbVersion = '2.2.11'
}

dependencies {
    xjc "javax.xml.bind:jaxb-api:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-impl:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-xjc:$jaxbVersion"
    xjc "com.sun.xml.bind:jaxb-core:$jaxbVersion"
    xjc 'javax.activation:activation:1.1.1'
}

xjcGeneration {
    defaultAdditionalXjcOptions = ['encoding': 'UTF-8']

    schemas {
        countries {
            schemaFile = 'countries.wsdl'
            javaPackageName = 'com.example.consumingwebservice.wsdl'
            sourceSet = 'main'
        }
    }
}

If a full working example is of interest leave a comment.