5

I tried to generate Jaxb Classes from XSD using jaxb2-maven-plugin.

I am able to get the jaxb classes in a package but my other packages are getting deleted. What is the reason for this? How to over come this? please can you give suggestions.

Below is what I tried

<bulid>
    <pluginManagement>
    <plugins>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>xjc</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <schemaDirectory>src/main/xsd</schemaDirectory>
            <outputDirectory>src/main/java</outputDirectory>
        </configuration>
        </plugin>
    </plugins>
    </pluginManagement>
    </bulid>

and xsd look like this :

<?xml version="1.0" encoding="UTF-8"?><xsd:schema targetNamespace="com.test.jaxb.model" 
        xmlns:ns="com.test.jaxb.model" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="TestResults">
        <xsd:sequence>
            <xsd:element maxOccurs="unbounded" minOccurs="0" name="testSuites" type="Q1:TestSuiteRun"/>
        </xsd:sequence>
        <xsd:attribute name="testProject" type="xsd:string"/>
    </xsd:complexType>


    <xsd:complexType name="TestCaseRun">
        <xsd:complexContent>
            <xsd:extension base="Q1:TestRun">
                <xsd:sequence>
        <xsd:element name="result" type="Q1:Severity"/>
      <xsd:element maxOccurs="unbounded" minOccurs="0" name="variations" type="Q1:VariationRun">
                    </xsd:element>
                </xsd:sequence>
                <xsd:attribute name="variationCount" type="xsd:int"/>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>

    </xsd:schema>

I have given targetNamespace="com.test.jaxb.model" but after generation I am only able to see jaxb classes under package name : model.jaxb.test.com..

Why so the package name is geting reversed and why is my other packages getting deleted ?

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
buttowski
  • 4,657
  • 8
  • 27
  • 33

3 Answers3

11

Your main problem is that you are using the src/main/java as <outputDirectory>. There are two major problems with that.

  1. The generated sources will be in a directory structure that under normal circumstances is under version control. What will you do with the generated sources? Should they be checked in? Your VCS will be signalling that new files have been found that are still not added.
  2. The generated sources will not be deleted when you call mvn clean.

You should remove the <outputDirectory>src/main/java</outputDirectory> completely and let maven and the plugin to their job.

If you remove those lines you will have the sources generated into target/generated-sources and they will be compiled during the compile phase which I assume is what you want.


Regarding the reversed package name I believe you should change targetNamespace to this:

<xsd:schema targetNamespace="http://www.test.com/jaxb/model"
    ...
maba
  • 47,113
  • 10
  • 108
  • 118
  • i made this change but files are getting generated as below target>generate-source>jaxb>http>www_test_com>jaxb>model – buttowski Nov 15 '12 at 07:13
  • i want something like com.test.jaxb.model – buttowski Nov 15 '12 at 07:15
  • I have tried it myself and I get `com.test.jaxb.model` and nothing else. And I am using the `targetNamespace` that I suggested. – maba Nov 15 '12 at 07:25
  • mvn jaxb2:xjc this is wat i used to run the plugin – buttowski Nov 15 '12 at 11:52
  • "The generated sources will be in a directory structure that under normal circumstances is under version control. " => No it shouldn't! That is also if you use Maven or other tools it will create those folders for the generated source in a way that Git by default will detect that it has to .gitignore it! You should not check in generated sources into the source control. It is actually bad practice. Better to fix your build process so that it generates the sources in the way you want them. Otherwise everybody who regenerates it has to do the manual steps you did to fix them. Manual == evil :) – seba.wagner Sep 26 '17 at 22:51
8

problem solved :

<bulid>
    <pluginManagement>
    <plugins>
        <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
            <execution>
                <phase>generate-sources</phase>
                <goals>
                    <goal>xjc</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <schemaDirectory>src/main/xsd</schemaDirectory>
            <outputDirectory>src/main/java</outputDirectory>
            <packageName>com.test.jaxb.model</packageName>
            <clearOutputDir>false</clearOutputDir>
        </configuration>
        </plugin>
    </plugins>
    </pluginManagement>
    </bulid>

i removed targetNameSpace from xsd

mvn jaxb2:xjc worked !!

buttowski
  • 4,657
  • 8
  • 27
  • 33
  • 3
    That is not how you should solve your problem. You're going to get headache later on. – maba Nov 15 '12 at 07:45
2

Under configuration tag add below property.

<clearOutputDir>false</clearOutputDir>
ChandraBhan Singh
  • 2,841
  • 1
  • 22
  • 27