views:

1172

answers:

1

I have hard times using maven to generate my client. So Please refer to http://stackoverflow.com/questions/2131001/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.

+2  A: 

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&lt;/wsdlLocation&gt; 
                    </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.

macbutch
Thanks you, very good shot actually. Do you know how to create the client jar from there?
ben
You're welcome - I've updated my answer with another pom fragment which should get you a bit closer.
macbutch