Use maven repository as a service repository

Skip to end of sidebar Go to start of sidebar
Skip to end of metadata
Go to start of metadata

Whenever you create web services there is the problem where to store the service contracts. What you need is some kind of service repository. Of course there are good products that support this. But typically they are quite expensive and quite complex. So why not use the maven repository for this task?  It already does a great job storing jars of libraries and zips of complete applications.

So what does a service repository based on a maven repository offer?

  • Great integration into your build process (if you use maven for your builds)
  • Support for namespaces (groupIds)
  • Support for versioning
  • Support for fast roundtripps with SNAPSHOT versions of your service contracts (WSDLs)
  • The pom of an application using services documents which services in which version are used
  • Support independent organizations or business units by using separate maven repositories

How is a service contract mapped to the maven repository?

So how is a service contract stored in the maven repository? Just like any other library. For example you would store the service
namespace: http://pizza.apache.org
service name: PizzaService
version: 1.0.0
 
as the following maven artifact

<groupId>org.apache.pizza</groupId>
<artifactId>PizzaService</artifactId>
<version>1.0.0</version>
<type>wsdl</type>

This artifact is represented in the maven repo by the file:

/org/apache/pizza/PizzaService-1.0.0.wsdl

How can the service contract be created and uploaded?

The howto Defining Contract first webservices with wsdl generation from java shows how to easily create a wsdl from a service definition in Java. From CXF 2.3.0 on you can easily deploy the generated wsdl file to the maven repo. Simply add the following to the configuration section of the java2ws plugin:

<configuration>
  <attachWsdl>true</attachWsdl>
</configruation>

The basic usage of the java2ws plugin is described in the CXF User Guide - Maven Integration.

If you create the wsdl file in another way or do not use CXF 2.3.0 you can use the build helper maven plugin with the goal attachArtifact.

Retrieving the service contract and generating source code from it

So you have the wsdl in the maven repo. How do you retrieve it and build the java stubs from it?

Before CXF 2.3.0 you could use the copy-dependency goal of the maven dependencies plugin. This copies a maven artifact from the maven repo to the target directory. Then you would use the cxf codegen plugin to generate the java code.

From CXF 2.3.0 on the codegen plugin can retrieve the wsdl directly from the maven repo.

<plugin>
 <groupId>org.apache.cxf</groupId>
 <artifactId>cxf-codegen-plugin</artifactId>
 <version>2.3.0</version>
 <executions>
  <execution>
   <id>generate-sources</id>
    <phase>generate-sources</phase>
    <configuration>
     <wsdlOptions>
      <wsdlOption>
       <wsdlArtifact>
        <groupId>org.apache.pizza</groupId>
	<artifactId>PizzaService</artifactId>
	<version>1.0.0</version>
       </wsdlArtifact>
      </wsdlOption>
     </wsdlOptions>
    </configuration>
    <goals>
     <goal>wsdl2java</goal>
    </goals>
   </execution>
  </executions>
</plugin>

 


Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Feb 24, 2010

    Anonymous says:

    I like that idea. I'm currently trying to figure out the best architecture for s...

    I like that idea.
    I'm currently trying to figure out the best architecture for such an approach, search the net for any helpful info, found already a couple of helpful articles by http://rapid4me.com SE
    I don't want to keep the wsdl within the service implementation project, because I see it as a contract the developer has to fulfill.
    Also it should be versioned independently from the service implementation versioning.

    I would then like to have the wsdl file included as a dependency, so the generated wsdl during jboss deployment can be validated against the "contract".
    However I have no idea yet on how to fetch the wsdl from the repository during the build process.

    1. Feb 24, 2010

      Christian Schneider says:

      You can use the maven dependency plugin for this. Simply use copy-dependencies w...

      You can use the maven dependency plugin for this. Simply use copy-dependencies with type wsdl to copy all wsdls to the place you want.