Tuesday, April 29, 2008

Running Axis wsdl2java from within Maven2 pom.xml

I needed to generate Axis infrastructure classes using wsdl2java for my web-service client application. I had to generate java classes from two WSDLs into two different packages respectively. I found one Maven2 plugin for that, but it has a limitation that I can not specify different packages for different WSDLs. I tried putting the same plugin twice in for both WSDLs specifying different package for each WSDL, but that too didn't work as it generated *.java files only for first declaration.

So I had to use Axis's ant tasks for wsdl2java. I did it like this:

<path id="compile_classpath" refid="maven.compile.classpath"/>
<taskdef resource="axis-tasks.properties" classpathref="compile_classpath" />

<property name="legacyService.wsdl" value="src/main/resources/ref-wsdl/legacy.wsdl"/>
<property name="newOne.wsdl" value="http://your.host/service.wsdl"/>
<property name="generated.dir" value="src/main/java"/>

<axis-wsdl2java output="${generated.dir}" verbose="true" url="${legacyService.wsdl}" >
<mapping namespace="urn:ServiceNameSpace" package="com.rst.webservice.legacy.client.axis14.util" />
<axis-wsdl2java output="${generated.dir}" verbose="true" url="${newOne.wsdl}" >
<mapping namespace="http://webservice.rst.com" package="com.rst.webservice.newone.client.axis14.util" />

<!-- ..... -->
<!-- ..... -->

Now, if you observe the above piece of pom.xml, axis:wsdl2java task is executed in 'generate-sources' phase, and Maven needs axis-ant.jar to execute that, but
it is not available to Maven by default. So we need to provide Maven with this dependency.

How to provide this dependency to maven?
Now that's tricky, if you see the Documentation page about
Maven Classpaths for ant-tasks then you would notice that the classpath that Maven refers to for executing various goals is not available as built-in variable. So I could have defined a path variable with hard-coded path to axis-ant.jar or I could define a 'dependency' to axis-ant and refer to maven.compile.classpath. And as you see I chose the second approach.


sepe said...

Hi Rushikesh,

I struggled a lot with the different wsdl2java maven plugins and faced similar problems like you. Finally with your advice I could manage to completly include my old ant tasks into maven.

For better separation between classpath and plugin dependencies I included the ant jar's right after the executions tag.


Rushikesh S. Thakkar said...

Hi Sebastian,
I'm glad it worked for you!

Thanks for acknowledging the same. Sorry for my delayed response. Your comment somehow went unnoticed.

- Rushi

Chakri said...

Hi Rushikesh,

In maven axis wsdl2java plugin is there a way to specify wsdl location as url. i mean not as a file in any project directoy but as url resource.

Thank you.

Rushikesh S. Thakkar said...

Hi Chakri,
I apologize for my late response. As I mentioned in my post I am specifying the location of WSDL as an HTTP URL:
<property name="newOne.wsdl" value="http://your.host/service.wsdl"/>

I guess that's what you want, right?