Having 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 ;)

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