1. Introduction
In this tutorial, we'll show how to configure the JAX-WS maven plugin to generate Java classes from a WSDL (web service description language) file. As a result, we'll be able to easily call web services using the generated classes.
2. Configuring Our Maven Plugin
First, let's include our JAX-WS Maven plugin with the wsimport goal in the build plugins section of our pom.xml file:
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
In short, the wsimport goal generates JAX-WS portable artifacts for use in JAX-WS clients and services. The tool reads a WSDL file and generates all the required artifacts for web service development, deployment, and invocation.
2.1. WSDL Directory Configuration
Within our Maven plugin section, the wsdlDirectory configuration property informs the plugin where our WSDL files are located. In this example, we'll tell the plugin to get all WSDL files in our project's src/main/resources directory:
<plugin>
...
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
</configuration>
</plugin>
2.2. WSDL Directory-Specific Files Configuration
Additionally, we can use the wsdlFiles configuration property to define a list of WSDL files to consider when generating the classes:
<plugin>
...
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
<wsdlFiles>
<wsdlFile>file1.wsdl</wsdlFile>
<wsdlFile>file2.wsdl</wsdlFile>
...
</wsdlFiles>
</configuration>
</plugin>
However, when the wsdlFiles property isn't set, all the files in the directory specified by the wsdlDirectory property will be considered.
2.3. WSDL URLs Configuration
Alternatively, we can configure the plugin's wsdlUrl configuration property:
<plugin>
...
<configuration>
<wsdlUrls>
<wsdlUrl>http://localhost:8888/ws/country?wsdl</wsdlUrl>
...
</wsdlUrls>
</configuration>
</plugin>
To use this option, the server hosting the URL for the WSDL file must be up and running so that our plugin can read it.
2.4. Configuring the Generated Classes Directory
Next, in the packageName property, we can set up the generated classes package name, and in the sourceDestDir, the output directory:
<plugin>
...
<configuration>
<packageName>com.baeldung.soap.ws.client</packageName>
<sourceDestDir>
${project.build.directory}/generated-sources/
</sourceDestDir>
</configuration>
</plugin>
As a result, our final version of the plugin configuration using the wsdlDirectory option is:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<wsdlDirectory>${project.basedir}/src/main/resources/</wsdlDirectory>
<packageName>com.baeldung.soap.ws.client</packageName>
<sourceDestDir>
${project.build.directory}/generated-sources/
</sourceDestDir>
</configuration>
</plugin>
3. Running the JAX-WS Plugin
Finally, with our plugin configured, we can generate our classes with Maven and check the output logs:
mvn clean install
[INFO] --- jaxws-maven-plugin:2.6:wsimport (default) @ jaxws ---
[INFO] Processing: file:/D:/projetos/baeldung/tutorials/maven-modules/maven-plugins/jaxws/src/main/resources/country.wsdl
[INFO] jaxws:wsimport args: [-keep, -s, 'D:\projetos\baeldung\tutorials\maven-modules\maven-plugins\jaxws\target\generated-sources', -d, 'D:\projetos\baeldung\tutorials\maven-modules\maven-plugins\jaxws\target\classes', -encoding, UTF-8, -Xnocompile, -p, com.baeldung.soap.ws.client, "file:/D:/projetos/baeldung/tutorials/maven-modules/maven-plugins/jaxws/src/main/resources/country.wsdl"]
parsing WSDL...
Generating code...
4. Check Generated Classes
After running our plugin, we can check the output in the folder target/generated-sources configured in the sourceDestDir property.
The generated classes can be found in com.baeldung.soap.ws.client as configured in the packageName property:
com.baeldung.soap.ws.client.Country.java
com.baeldung.soap.ws.client.CountryService.java
com.baeldung.soap.ws.client.CountryServiceImplService.java
com.baeldung.soap.ws.client.Currency.java
com.baeldung.soap.ws.client.ObjectFactory.java
5. Conclusion
In this article, we saw how to generate Java classes from a WSDL file using the JAX-WS plugin. As a result, we're now able to create a web service client and use the generated classes to call our services.
The source code for our application is available over on GitHub.
The post Generate WSDL Stubs with Maven first appeared on Baeldung.