Generating stubs from multiple WSDL files using Maven or Gradle

Introduction

In this post we will see an example on generating stubs from multiple QSDL files using Maven or Gradle plugin. Let’s say, you are given a WSDL file and you want to generate Java class from it, then you need to do some configurations in your maven or gradle project to automate the activities.

You can also generate using command from command line but using maven or gradle configurations it will be easier to manage.

Prerequisites

Java, Maven or Gradle

Example

Using Maven Configuration

If you have maven based project then you can use below maven based plugin to generate stubs or Java classes from the WSDL file.

...
<build>
	<plugins>
		<plugin>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-codegen-plugin</artifactId>
			<version>3.1.10</version>
			<executions>
				<execution>
					<id>generate-sources</id>
					<phase>generate-sources</phase>
					<configuration>
						<sourceRoot>${basedir}/src/generated-sources/java</sourceRoot>
						<wsdlOptions>
							<wsdlOption>                                                                                                                   	<wsdl>${basedir}/src/main/resources/wsdl/<wsdl1>.wsdl</wsdl>
								<!-- if you want to use URL -->
								<!-- <wsdl>http://<host>:<port>/<name1>.wsdl</wsdl> -->
							</wsdlOption>
							<wsdlOption>                                                                                                               		<wsdl>${basedir}/src/main/resources/wsdl/<wsdl2>.wsdl</wsdl>
								<!-- if you want to use URL -->
								<!-- <wsdl>http://<host>:<port>/<name2>.wsdl</wsdl> -->
							</wsdlOption>
						</wsdlOptions>
					</configuration>
					<goals>
						<goal>wsdl2java</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
	</plugins>
</build>

Using Gradle Configurations

If you have gradle based project then you can use below gradle based plugin to generate stubs or Java classes from WSDL file.

buildscript {
    repositories {
        mavenCentral()
		jcenter()
    }
    dependencies {
		classpath 'no.nils:wsdl2java:0.10'
    }
}
apply plugin: 'java'
apply plugin: 'no.nils.wsdl2java'
wsdl2javaExt {
	cxfVersion = "3.1.10"
}
wsdl2java{
	generatedWsdlDir = file("${projectDir}/src/main/service") //java source classes will be generated
	wsdlDir=file("${projectDir}/src/main/resources/wsdl/") //wsdl directory
	wsdlsToGenerate = [
		[file("${projectDir}/src/main/resources/wsdl/wsdl1.wsdl")], //wsdl1
		[file("${projectDir}/src/main/resources/wsdl/wsdl2.wsdl")], //wsdl2
		[file("${projectDir}/src/main/resources/wsdl/wsdl3.wsdl")]  //wsdl3
	]
}
compileJava.dependsOn wsdl2java
...

If you are using plugin com.github.bjornvester.wsdl2java, then you can use below build.gradle script.

Note that the default directory for the WSDL files is src/main/resources and Java classes will be generated under build/generated/wsdl2java directory.

plugins {
    id 'java-library'
    id "com.github.bjornvester.wsdl2java" version "0.2"
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
    mavenCentral()
    maven {
		url 'http://maven.java.net/content/repositories/staging/'
	}
}
dependencies {
	implementation 'com.sun.xml.ws:rt:2.3.1'
	implementation 'org.glassfish.jaxb:txw2:2.4.0-b180608.0325'
}
wsdl2java {
	cxfVersion.set("3.3.2")
}

That’s all. Thanks for reading.

2 thoughts on “Generating stubs from multiple WSDL files using Maven or Gradle

  1. Hola como vas?
    tienes un ejemplo en Git hub para visualizar la implmentacion de forma detalla.
    mil gracias

Leave a Reply

Your email address will not be published. Required fields are marked *