Share

September 22, 2008

Maven 2 - WSDL TO Java using Jaxb

Step 1:
Download the wsdls and put them in a dir i use "src/main/wsdl", this must match schemaDirectory
Step 2:
Add the following to the build pom.xml

<build>
   <plugin>
    <groupId>com.sun.tools.xjc.maven2</groupId>
    <artifactId>maven-jaxb-plugin</artifactId>
    <executions>
        <execution>
                <id>ws-source-gen</id>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <schemaDirectory>src/main/wsdl</schemaDirectory>
                    <generateDirectory>src/main/autogen</generateDirectory>
                </configuration>
            </execution>
        </executions>
    </plugin>
....
</build>

Step 3:
Add the autogen dir to the resources

        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/autogen</directory>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

Step 4:
I tent do run mvn test to gen the source and run the unit test with the genrated sources

NOTES:
- Don't check in autogen into source countral
After trial and error i learned not to check the auto gen code since you contal the changes to it by checked in the wsdl.

- Exclude autogen code from code covrage
using org.codehaus.mojo to do code covrage i tend to add the following plugin and exclude the auto gencode... so that the reports are clean

<plugin>
 <groupId>org.codehaus.mojo</groupId>
  <artifactId>cobertura-maven-plugin</artifactId>
 <executions>
  <execution>
       <goals>
       <goal>clean</goal>
       </goals>
  </execution>
  </executions>
  <configuration>
  <instrumentation>
       <excludes>
       <exclude>com/company/autogen/**/*.class</exclude>
       </excludes>
  </instrumentation>
  </configuration>
</plugin>

5 comments:

  1. What versions of jaxb do you have in your pom.xml as a dependency? When I try to add there is about 100 different versions of jaxb out there.

    ReplyDelete
  2. I can't recall which one i was using on Java 1.5 but Java 1.6 includes Jaxb...

    I think it was Sun's from the Sun's old maven repo

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. Jus as an update... Mine needed the following as well to get it working for wsdl... (used ver 1.1)

    [args]-wsdl[/args]
    [schemaFiles]src/main/resources/wsdl/xxx.wsdl[/schemaFiles]

    ReplyDelete