Buscar este blog

jueves, 2 de abril de 2015

Eclipse - Maven - Configure wsdl2java

When you work with web services you need to have all service classes in application classpath in order to invoke service methods and retrieve the result class.
The simplest way to do that is to generate the client class with some kind of tool like wsimport, and then place them into some package of your aplication.

For example, if want to invoke http://localhost:8080/simplearchetype-ws/servizos/UsuarioService?wsdl, I could create service class in this way:

"c:\Program Files\Java\jdk1.6.0_45\bin\wsimport.exe" -p es.myapp.usuarioservice -keep http://localhost:8080/simplearchetype-ws/servizos/UsuarioService?wsdl

This will generate all necessary source files, i.e. ".java", in the due package. Then I would have to copy to the src/main/java folder of my project and go on.

But this aproach has a small issue, what happens when the service contract changes? Well, if this happens, obviously you will have to change thesource code too, but also the client classes generated previously. So you will need to repeat the wsimport again.

There is a more automatic solution for this. You can use cxf-codegen-plugin in your pom.xml, so maven will generate all client sources automatically when the wsdl changes.

In pom.xml file you need to include this plugin:

<plugin>
   <groupId>org.apache.cxf</groupId>
   <artifactId>cxf-codegen-plugin</artifactId>
   <version>${cxf.version}</version>
   <executions>
      <execution>
         <id>generate-sources</id>
         <phase>generate-sources</phase>
         <configuration>
            <sourceRoot>${project.build.directory}/generated/cxf</sourceRoot>
            <wsdlOptions>
               <wsdlOption>
                  <wsdl>${basedir}/src/main/resources/wsdl/UsuarioService.wsdl</wsdl>
               </wsdlOption>
            </wsdlOptions>
         </configuration>
         <goals>
            <goal>wsdl2java</goal>
         </goals>
      </execution>
   </executions>
</plugin>

In this example, the wsdl file is located in src/main/resoruces/wsdl directory of the proyect.

But Eclipse may complain about this plugin:


The next thing to do is try to discover new connectors. If you are lucky and you found a valid connector, then you are done. Eclipse will recognize the directory target/generated/cxf/ as a valid source directory and will include all source files in build path.

But if this does not work, you can make eclipse ingnore this error by adding another plugin more:

<pluginManagement>
   <plugins>           
      <plugin>
         <groupId>org.eclipse.m2e</groupId>
         <artifactId>lifecycle-mapping</artifactId>
         <version>1.0.0</version>
         <configuration>
            <lifecycleMappingMetadata>
               <pluginExecutions>
                  <pluginExecution>
                     <pluginExecutionFilter>
                        <groupId>org.apache.cxf</groupId>
                        <artifactId>cxf-codegen-plugin</artifactId>
                        <versionRange>[2.4.6,)</versionRange>
                        <goals>
                           <goal>wsdl2java</goal>
                        </goals>
                     </pluginExecutionFilter>
                     <action>
                        <execute>
                           <runOnIncremental>false</runOnIncremental>
                        </execute>
                     </action>
                  </pluginExecution>
               </pluginExecutions>
            </lifecycleMappingMetadata>
         </configuration>
      </plugin>
   </plugins>
</pluginManagement>

With this, eclipse will ignore wsdl2java goal of cxf-codegen-plugin.

And finally, you need to manually configure project build path to include this folder. Selecting the project, right click > Build path > Configure Build Path.
In Source tab you have to select Add Folder and browse to Target > generated > cxf and check this last one.


Then, each time maven builds the project, it will generate the service class based on the wsdl file.

No hay comentarios:

Publicar un comentario