Packages are removed when creating Jaxb classes. Using Maven and the generated maven package name is incorrect.

I tried to create Jaxb classes from XSD using jaxb2-maven-plugin.

I can get jaxb classes in a package, but my other packages are uninstalled. What is the reason for this? How did it happen? Please, you can give suggestions.

Below 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 gave targetNamespace = "com.test.jaxb.model", but after generation I can only see jaxb classes under the package name: model.jaxb.test.com ..

Why is the package name reversed and why are my other packages being deleted?

+4
source share
3 answers

The main problem is that you are using src/main/java as <outputDirectory> . There are two main problems.

  • The generated sources will be in the directory structure, which under normal circumstances is under version control. What will you do with the generated sources? Should they be checked? Your VCS will signal that new files are found that have not yet been added.
  • Generated sources will not be deleted when mvn clean called.

You must completely remove <outputDirectory>src/main/java</outputDirectory> and let maven and the plugin do their work.

If you delete these lines, you will have the sources generated in target/generated-sources , and they will compile during the compile phase, which I assume is what you want.


Regarding the package reverse name, I believe that you should change targetNamespace to this:

 <xsd:schema targetNamespace="http://www.test.com/jaxb/model" ... 
+10
source

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 works !!

+6
source

In the configuration tag, add the property below.

 <clearOutputDir>false</clearOutputDir> 
0
source

Source: https://habr.com/ru/post/1446141/


All Articles