Search code examples
javamaven-2jax-wsmaven-plugin

Use Maven to trigger a wsgen & wsimport in a row, using wsdlLocation


I have hard times using maven to generate my web service client. So please refer to Creating a web-service client directly from the source for the first part of my question.

To keep it simple and short, I want to go from here (a file in src/main/java) :

package com.example.maven.jaxws.helloservice;
   
import javax.jws.WebService;

@WebService
public class Hello {
     public String sayHello(String param) {
         return "Hello " + param;
     }
} 

to there :

/**
 * This class was generated by the JAX-WS RI.
 * JAX-WS RI 2.1.7-b01-
 * Generated source version: 2.1
 * 
 */
@WebServiceClient(name = "HelloService", targetNamespace = "http://helloservice.jaxws.maven.example.com/", wsdlLocation = "http://localhost:8080/test/")
public class HelloService extends Service {

    private final static URL HELLOSERVICE_WSDL_LOCATION;
    private final static Logger logger = 
         Logger.getLogger(com.example.wsimport.HelloService.class.getName());
    ...etc

using only 1 pom.xml file.

Please note the wsdlLocation set on the end. The pom.xml file will probably use both maven-jaxws-plugin wsgen AND wsimport with some tricky configuration to achieve this.


Solution

  • Assuming, you're not going to try to use the generated stubs in the same project that you're doing this in (which would create circular dependencies and be a bad idea...) then, yes, you can do something like this.

    The config is not all that tricky, in fact you'd kind of guessed it in your question but here goes:

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxws-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-wsdl</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>wsgen</goal>
                        </goals>
                        <configuration>
                            <sei><!-- fully qualified class name goes here --></sei>
                            <genWsdl>true</genWsdl>
                        </configuration>
                    </execution>
                    <execution>
                        <id>generate-stubs</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>wsimport</goal>
                        </goals>
                        <configuration>
                            <wsdlDirectory>target/jaxws/wsgen/wsdl</wsdlDirectory>
                            <wsdlFiles>                   
                                <wsdlFile><!-- class name goes here -->Service.wsdl</wsdlFile>
                            </wsdlFiles>
                            <!-- *** you need the next line to set the wsdlLocation in the generated stubs *** -->
                            <wsdlLocation>http://localhost:8080/test</wsdlLocation> 
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    Update - to package up the generated code into a jar I would use the maven-jar-plugin like so:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>package-wsclient-jars</id>
                            <phase>package</phase>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                            <configuration>
                                <classesDirectory>target/jaxws/<!-- rest of the path here, can't remember it right now --></classesDirectory>
                                <classifier>wsclient</classifier>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
    

    I've quickly pasted this from our config but our usage is a little different (as we zip up the wsdl files not the generated client but I believe this will get you pretty close). You'll probably have to setup the version for the maven-jar-plugin if you're not already using it - 2.3.1 seems to be the latest.