Generating stubs from multiple WSDL files using Maven or Gradle

You may also like to read:

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

...

That’s all. Thanks for reading.

Generating stubs from multiple WSDL files using Maven or Gradle

Leave a Reply

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

Scroll to top