40

When I attempt to run "mvn generate-sources" this is my output :

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building gensourcesfromwsdl 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.104s
[INFO] Finished at: Tue Aug 20 15:41:10 BST 2013
[INFO] Final Memory: 2M/15M
[INFO] ------------------------------------------------------------------------

I do not receive any errors but there are no java classes generated from the wsdl file.

Here is my pom.xml file that I'm running the plugin against :

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>gensourcesfromwsdl</groupId>
    <artifactId>gensourcesfromwsdl</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxws-maven-plugin</artifactId>
                    <version>1.12</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>wsimport</goal>
                            </goals>
                            <configuration>
                                <wsdlLocation>http://mysite/firstwsdl.asmx?wsdl</wsdlLocation>
                                <packageName>com</packageName>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

</project>

What am I doing wrong ? The package com exists in the project 'gensourcesfromwsdl' and the wsdl location is valid.

When I run wsimport via the command line : >wsimport -keep -verbose http://mysite/firstwsdl.asmx?wsdl the class is generated.

andand
  • 17,134
  • 11
  • 53
  • 79
blue-sky
  • 51,962
  • 152
  • 427
  • 752
  • did you ever find a solution to this problem? I'm stuck on the same problem – Oliver Watkins Nov 25 '13 at 10:21
  • @Oliver Watkins im afraid not, I ended up generating the classes outside of Maven. This may help you : http://stackoverflow.com/questions/6920175/how-to-generate-java-classes-from-wsdl-file – blue-sky Nov 25 '13 at 12:06

7 Answers7

52

To generate classes from WSDL, all you need is build-helper-maven-plugin and jaxws-maven-plugin in your pom.xml
Make sure you have placed wsdl under folder src/main/resources/wsdl and corresponding schema in src/main/resources/schema, run command "mvn generate-sources" from Project root directory.

C:/Project root directory > mvn generate-sources

generated java classes can be located under folder

target/generated/src/main/java/com/raps/code/generate/ws.

pom.xml snippet

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals><goal>add-source</goal></goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated/src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxws-maven-plugin</artifactId>
    <version>1.12</version>
    <configuration>
        <wsdlDirectory>${project.basedir}/src/main/resources/wsdl</wsdlDirectory>
        <packageName>com.raps.code.generate.ws</packageName>
        <keep>true</keep>
        <sourceDestDir>${project.build.directory}/generated/src/main/java</sourceDestDir>
    </configuration>
    <executions>
        <execution> 
            <id>myImport</id>
            <goals><goal>wsimport</goal></goals>
        </execution>
    </executions>
</plugin>
Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • 2
    This might be a stupid question but what's the build-helper-maven-plugin are for? whats the different between that and the jaxws-maven-plugin one? – Silly Sally Jun 22 '17 at 05:00
  • 4
    @SillySally jaxws-maven-plugin executes wsimport step to generate java code from web service definition language (wsdl, xsd files). This code is generated into a dedicated folder, `/target/generated...` build-helper-maven-plugin is a plugin that will declare additionnal source folder (to add `/target/generated...` in addition to the legacy src/main/java one). (stupid question doesn't exist ! ^:p) – boly38 Sep 07 '17 at 13:16
  • 6
    ahhh. okay. but one more question, why dont we just generate the code under src/main/java ? – Silly Sally Sep 10 '17 at 08:44
  • how can we define java version for wsimport? – Afroz Shaikh Oct 04 '17 at 10:02
  • 7
    @SillySally this to avoid confusion between the code you're writing and generated code that must keep untouched ;) – boly38 Oct 11 '17 at 18:41
  • 2
    @SillySally also otherwise the version management system (svn/etc.) would always notice changed sources after generation which would be quite dirty in an automated build environment – fl0w Jul 02 '18 at 11:55
  • 1
    @Silly Sally, I do not agree with boly38 and fl0w. If we put generated files in the source folder, git does not consider new generated files as untouched. With this solution we can clearly identify changes in java files in the future, in case a wsdl will be updated. Next, the generated files are visible as soon as you check out the project. You do not meet any more a situation, when your code is red, cause you have not generated it yet. Maybe these reasons are not so important, but it is easier to configure in maven and more convenient to work with it. – Alexandr Sep 07 '18 at 11:22
  • `build-helper-maven-plugin` tells maven to consider some outside folder as containing your java code. Without it Intellij can see your generated classes, but Maven - don't (on clean - install). – Zon Sep 21 '18 at 14:46
  • @Alexandr, you wrote "changes in java files in the future, in case a wsdl will be updated" - so its better to **generate classes** (by wsdl and xsd) **every build**, isn't it ? Because if we use one-time generated by `wsimport` classes - we'll not see any changes ... – Dan Brandt Feb 27 '20 at 18:01
  • 1
    @DanBrandt you can generate it each time. But cause you generate it against the local wsdl file, which is usually the same between builds, you do not get much. The only advantage of this strategy is, that you do not need to remember how to trigger wsimport. And exactly this strategy is described by my answer to this issue. – Alexandr Mar 01 '20 at 04:41
29

Here is an example of how to generate classes from wsdl with jaxws maven plugin from a url or from a file location (from wsdl file location is commented).

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<build>  
    <plugins>           
        <!-- usage of jax-ws maven plugin-->
        <plugin> 
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>1.12</version>
            <executions> 
                <execution> 
                    <id>wsimport-from-jdk</id>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <!-- using wsdl from an url -->
                <wsdlUrls>
                    <wsdlUrl>
                        http://myWSDLurl?wsdl
                    </wsdlUrl>
                </wsdlUrls>
                <!-- or using wsdls file directory -->
                    <!-- <wsdlDirectory>src/wsdl</wsdlDirectory> -->
                <!-- which wsdl file -->
                <!-- <wsdlFiles> -->
                    <!-- <wsdlFile>myWSDL.wsdl</wsdlFile> -->
                <!--</wsdlFiles> -->
                <!-- Keep generated files -->
                <keep>true</keep> 
                <!-- Package name --> 
                <packageName>com.organization.name</packageName> 
                <!-- generated source files destination-->
                <sourceDestDir>target/generatedclasses</sourceDestDir>
            </configuration>
        </plugin>
    </plugins>  
</build>  

Cassian
  • 3,648
  • 1
  • 29
  • 40
6

Even though this is bit late response, may be helpful for someone. Look like you have used pluginManagement. If you use pluginManagement , it will not pick the plug-in execution.

It should be under

<build>
<plugins>           
        <plugin> 
Midhun
  • 146
  • 1
  • 4
  • i just submitted an edit for this, as you point out this might not be clear for everyone, especially maven beginners, that might find the answer – fl0w Jul 02 '18 at 12:04
3

Try to wrap wsdlLocation in wsdlUrls

            <wsdlUrls>
                <wsdlLocation>http://url</wsdlLocation>
            </wsdlUrls>
3

I see some people prefer to generate sources into the target via jaxws-maven-plugin AND make this classes visible in source via build-helper-maven-plugin. As an argument for this structure

the version management system (svn/etc.) would always notice changed sources

With git it is not true. So you can just configure jaxws-maven-plugin to put them into your sources, but not under the target folder. Next time you build your project, git will not mark these generated files as changed. Here is the simple solution with only one plugin:

      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.6</version>

    <dependencies>
      <dependency>
        <groupId>org.jvnet.jaxb2_commons</groupId>
        <artifactId>jaxb2-fluent-api</artifactId>
        <version>3.0</version>
      </dependency>
      <dependency>
        <groupId>com.sun.xml.ws</groupId>
        <artifactId>jaxws-tools</artifactId>
        <version>2.3.0</version>
      </dependency>
    </dependencies>

    <executions>
      <execution>
        <goals>
          <goal>wsimport</goal>
        </goals>
        <configuration>
          <packageName>som.path.generated</packageName>
          <xjcArgs>
            <xjcArg>-Xfluent-api</xjcArg>
          </xjcArgs>
          <verbose>true</verbose>
          <keep>true</keep> <!--used by default-->
          <sourceDestDir>${project.build.sourceDirectory}</sourceDestDir>
          <wsdlDirectory>src/main/resources/META-INF/wsdl</wsdlDirectory>
          <wsdlLocation>META-INF/wsdl/soap.wsdl</wsdlLocation>
        </configuration>
      </execution>
    </executions>
  </plugin>

Additionally (just to note) in this example SOAP classes are generated with Fluent API, so you can create them like:

A a = new A()
  .withField1(value1)
  .withField2(value2);
Alexandr
  • 9,213
  • 12
  • 62
  • 102
0

The key here is keep option of wsimport. And it is configured using element in About keep from the wsimport documentation :

-keep                     keep generated files
Gaurav
  • 1,549
  • 2
  • 15
  • 31
0

i was having the same issue while generating the classes from wsimport goal. Instead of using jaxws:wsimport goal in eclipse Maven Build i was using clean compile install that was not able to generate code from wsdl file. Thanks to above example. Run jaxws:wsimport goal from Eclipse ide and it will work

Waqas Ahmed
  • 4,801
  • 3
  • 36
  • 45