Archive

Posts Tagged ‘netbeans’

Importing Multiple WSDLs with Maven

March 11th, 2009 22 comments

The jax-ws-maven plugin for Maven includes  the handy wsimport goal. This goal will take a WSDL from a URL or file and generate Java bindings for the service described.  The generated code may not be beautiful, but it works. Anyway, today I spent more than an hour fiddling with wsimport. In my case I was trying to import multiple WSDLs into my project with different target packages. I ran into several hurdles and figured that I’d document them here for future victims.

First, for better or worse, I’m working in NetBeans.  The Maven support is passable, but out of the box, the error reporting leaves a lot to be desired. In particular, detailed error reporting must be enabled. Otherwise, when there’s an error in your pom.xml file, you’ll get a nice <Badly formed Maven project> error with no other explanation. To enable error reporting go to Tools->Options->Miscellaneous->Maven, and check “Produce Exception Error Messages”. That will make your life easier. Now, about wsimport…

The obvious way to import multiple WSDLs is to include multiple executions in the jaxws-maven-plugin section of pom.xml. In fact, this is even the right way to do it… but if you just take your single-WSDL example, stolen from some tutorial somewhere, and copy it, you’ll end up with a couple of problems. First, when multiple execution are present, they must each be given a unique id, using of all things, the <id> tag.  This wasn’t that tough to figure out once I turned on error reporting as described above.

The second issue was more problematic and, in my opinion, probably a bug in the plugin. Each execution includes a “staleFile” which is used to manage dependencies, i.e. correctly recompiling WSDL when it changes. However, it happens that when multiple executions are present they use the same staleFile.  This means that the import of the second WSDL always thinks it’s up to date and thus never runs.  After a bunch of googling, I managed to find a solution in this bug report. So, the solution is to manually set a staleFile for each WSDL. Here’s the resulting <plugin> block from pom.xml:

<plugin>
   <groupId>org.codehaus.mojo</groupId>
   <artifactId>jaxws-maven-plugin</artifactId>
   <executions>
      <execution>
         <id>FirstWsdl</id>
         <goals>
            <goal>wsimport</goal>
         </goals>
         <configuration>
            <wsdlLocation>http://localhost:8080/FirstWsdl?wsdl</wsdlLocation>
            <wsdlFiles>
            <wsdlFile>path/to/FirstWsdl.wsdl</wsdlFile>
            </wsdlFiles>
            <packageName>com.example.first</packageName>
            <!-- Without this, multiple WSDLs won't be processed :( -->
            <staleFile>${project.build.directory}/jaxws/stale/wsdl.FirstWsdl.done</staleFile>
         </configuration>
      </execution>
      <execution>
         <id>SecondWsdl</id>
         <goals>
            <goal>wsimport</goal>
         </goals>
         <configuration>
            <wsdlLocation>http://localhost:8080/SecondWsdl?wsdl</wsdlLocation>
            <wsdlFiles>
            <wsdlFile>path/to/SecondWsdl.wsdl</wsdlFile>
            </wsdlFiles>
            <packageName>com.example.second</packageName>
            <!-- Without this, multiple WSDLs won't be processed :( -->
            <staleFile>${project.build.directory}/jaxws/stale/wsdl.SecondWsdl.done</staleFile>
         </configuration>
      </execution>
   </executions>
</plugin>

That’s it. Cheers.

Categories: java Tags: , , ,