Java

SOAP Webservices with Maven, Project setup

This is the second part in the series about SOAP Webservices with Maven. It continues after SOAP Webservices with Maven, Introduction

Before we can actually start setting up the Soap webservice, you need a maven project. For this you need a POM file. This pom file you can then use in your IDE of choice to create the actual project. An example of a POM file with all the bits and pieces for a soap Webservice is displayed below.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <artifactId>soap</artifactId>
    <groupId>com.ractoc.coding.tutorials</groupId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <modelVersion>4.0.0</modelVersion>
    <build>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <generateDirectory>target/generated</generateDirectory>
                            <generatePackage>com.ractoc.coding.tutorials.soap.generated</generatePackage>
                            <schemaDirectory>src/main/webapp/schemas</schemaDirectory>
                            <schemas>
                                <schema>
                                    <fileset>
                                        <directory>src/main/webapp/schemas</directory>
                                        <includes>
                                            <include>**/*.xsd</include>
                                        </includes>
                                    </fileset>
                                </schema>
                            </schemas>
                            <enableIntrospection>false</enableIntrospection>
                            <cleanPackageDirectories>true</cleanPackageDirectories>
                            <markGenerated>false</markGenerated>
                            <forceRegenerate>false</forceRegenerate>
                            <args>
                                <arg>-no-header</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>com.sun.xml.ws</groupId>
            <artifactId>jaxws-rt</artifactId>
            <version>2.1.3</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

This is the complete Maven POM. Now lets zoom in on the various plugins and dependencies to see what they are for.

Plugins

            <plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <version>0.9.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <phase>generate-sources</phase>
                        <configuration>
                            <generateDirectory>target/generated</generateDirectory>
                            <generatePackage>com.ractoc.coding.tutorials.soap.generated</generatePackage>
                            <schemaDirectory>src/main/webapp/schemas</schemaDirectory>
                            <schemas>
                                <schema>
                                    <fileset>
                                        <directory>src/main/webapp/schemas</directory>
                                        <includes>
                                            <include>**/*.xsd</include>
                                        </includes>
                                    </fileset>
                                </schema>
                            </schemas>
                            <enableIntrospection>false</enableIntrospection>
                            <cleanPackageDirectories>true</cleanPackageDirectories>
                            <markGenerated>false</markGenerated>
                            <forceRegenerate>false</forceRegenerate>
                            <args>
                                <arg>-no-header</arg>
                            </args>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

This first plugin handles the actual code generation. It looks in the schemaDirectory,  src/main/webapp/schemas. There it includes all xsd files throught the pattern **/*.xsd. This pattern matches all the files with the xsd extension in the current folder and all the folders below it. From these files it creates the various classes, one per specified datatype, in the generatePackage com.ractoc.coding.tutorials.soap.generated. These will be created in the generateDirectory target/generated.

There are still more interesting options which can be enabled with this plugin. These will be added and explained later.

These are the main properties in the plugin configuration.

            
<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.7</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

This next plugin adds the folder containing the generated sources as a source folder. This is needed to tell Maven that there are sources here that need to be compiled.

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

The third and final plugin handles the generation of the WAR file. This is the file which you will actually deploy to the webserver. Because a simple webservice does not need to have a web.xml descriptor file, we need to tell the maven-war-plugin to ignore this missing file.

Dependencies

The next part of the POM file contains the various dependencies. The most basic webservice project needs just 2 dependencies.

 <dependency>
     <groupId>com.sun.xml.ws</groupId>
     <artifactId>jaxws-rt</artifactId>
     <version>2.1.3</version>
 </dependency>

This dependency contains all the classes needed for the SOAP webservice. Some webservice, like Tomcat, need these classes to be deployed alongside the applications. Webservers with a complete j2ee container should not need these libraries to be deployed with the application.

 <dependency>
     <groupId>javax</groupId>
     <artifactId>javaee-api</artifactId>
     <version>6.0</version>
     <scope>provided</scope>
 </dependency>

This dependency is needed at compile time only. The webserver will always have the actual implementation available. Maven however does not. By including the API in this way, the code will actually compile.

This tutorial continuous in the next part of the series, SOAP webservices with Maven, Contract first