Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 427
benji Avatar asked Jan 28 '10 21:01

benji


People also ask

How do I run Wsimport?

To run the wsimport , go to the root directory of web services client project and use command prompt. -s -s defines the directory for source files. And pass the wsdl URL of web services end point. The below classes will be generated for web service client for the given WSDL URL.

What does Mvn generate sources do?

The “maven-source” plugin is used to pack your source code and deploy along with your project. This is extremely useful, for developers who use your deployed project and also want to attach your source code for debugging.


1 Answers

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.

like image 119
macbutch Avatar answered Sep 25 '22 11:09

macbutch