Create a SOAP client using the JAX-WS Maven Plugin
April 8th, 2010 by micha kopsHaving written the article “How to build a Confluence SOAP client in 5 minutes” some readers asked me for some more information and help using the JAX-WS plugin that I mentioned in the article instead of the Axis plugin – so here we go
Contents
Steps
- Create a simple maven project first using archetype:create or archetype:generate
mvn archetype:create -DgroupId=com.hascode.jaxws -DartifactId=soap-tutorial
- We get a pom.xml like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.jaxws</groupId> <artifactId>soap-tutorial</artifactId> <version>0.1</version> </project>
- Insert plugin references and dependencies to your pom.xml
<dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <packageName>com.hascode.schema</packageName> </configuration> </plugin> </plugins> </build>
- Specify the package name for the generated java files in the plugin configuration – I used com.hascode.schema – you might want to edit this
- JAX-WS needs at least Java5 so add this source/target information to the build element
<plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin>
- If you wish to use a specific version of JAX-WS you may specify your needs adding this plugin configuration to the build element
<dependencies> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-tools</artifactId> <version>2.2.1</version> </dependencies>
- If maven cries about unresolved dependencies to com.sun.xml.ws-rt you should add the Sun Maven Repository to your pom.xml
<repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2</url> </repository> </repositories>
- In conclusion – my pom.xml looks like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hascode.jaxws</groupId> <artifactId>soap-tutorial</artifactId> <version>0.1</version> <dependencies> <dependency> <groupId>com.sun.xml.ws</groupId> <artifactId>jaxws-rt</artifactId> <version>2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxws-maven-plugin</artifactId> <executions> <execution> <goals> <goal>wsimport</goal> </goals> </execution> </executions> <configuration> <packageName>com.hascode.schema</packageName> </configuration> </plugin> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>maven2-repository.dev.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2</url> </repository> </repositories> </project>
- Create a directory named wsdl in your src directory – the WSDL files go there – the maven plugin searches for files matching *.wsdl in this directory
- Get a valid WSDL somewhere an store it in src/wsdl – I used a sample wsdl file from a IBM tutorial I once read for testing.
- Generate your java files using the jaxws-plugin:
mvn jaxws:wsimport
- Enjoy the generated files in target/jaxws/wsimport/java with the defined package name com.hascode.schema
Troubleshooting
- wsimport often needs some extra tuning to get some wsdl definitions going – so if you did not succeed try to configure the process in adjusting the plugin parameters, get one or more bottles of wine and parse through the jax-ws documentation or switch to axis and get the stuff running in minutes – nah just kidding
- In Eclipse you sometimes have to adjust the Java VM defined in the build path and the compiler target level (Project Properties > Java Compiler | Build Path)
- In Eclipse you can easily validate your stored wsdl file by opening it in the xml editor and selecting “Validate” in the context menu
- “[ERROR] undefined element declaration ‘xs:schema’” xs:schema is referenced but an import is missing – read this nice article from Vivek Pandey on Java.net how to fix this problem
Links
- Goal documentation: jaxws:wsimport
- Java.net: JAX-WS Maven Plugin
- Sun blog article: Using JAX-WS with Maven
- My article explaining the Axis-Maven-Plugin
- Oracle/Sun Jax-WS Homepage
- IBM.com: Developing a JAX-WS client from a WSDL file
- Axis Maven Plugin Homepage
- Vivek Pandey. Java.net: How to deal with unresolved schema references
<dependencies>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.hascode.somepackage</packageName>
</configuration>
</plugin>
</plugins>
<build>
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>2.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxws-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>wsimport</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.hascode.somepackage</packageName>
</configuration>
</plugin>
</plugins>
<build>
Tags: Axis, Axis2, jax-ws, maven, maven2, soap, tutorial, webservice, wsdl, wsimport
April 18th, 2012 at 8:16 pm
Great article, but the command is “mvn jaxws:wsimport” not just “jaxws:import”.
April 19th, 2012 at 7:21 pm
thanks for mentioning! *article updated*
August 7th, 2012 at 3:06 am
Your blogs- are very unique and interesting.
October 4th, 2012 at 2:03 pm
Is it possible to specify the URL of a WSDL for Maven to use, instead of downloading and installing the WSDL file in a wsdl directory?
October 8th, 2012 at 12:42 pm
I think it is possible – the jaxws-maven-plugin allows to specify a list of wsdl urls for the goal jaxws:wsimport: http://mojo.codehaus.org/jaxws-maven-plugin/wsimport-mojo.html