Search code examples
javaxmlxsdjaxbmaven-jaxb2-plugin

How to change default directory for generating Java from XSD (JAXB)


I have a manve project, and I'm using the JAXB2 plugin to generate java class from XSD schema. By default, the classes are generated in the target folder, but I need to generate it in the src/main/java folder.

I tried adding the line attribute generateDirectory. The classes are generated where I want, but I can't import them on other places

Here is my pom.xml:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <configuration>
                <schemaDirectory>src/main/resources/schemas</schemaDirectory>
                <generateDirectory>src/main/java/com/evol/foo/generated-bar-sources</generateDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

And my java class using the generated files:

package com.evol.foo.service;
import com.evol.foo.generated-bar-sources; //error: cannot resolve symbol generated

@Component
public class XMLParserService {

  //ComplexType cannot be found
  public ComplexType parseErrorFile(String filePath) throws JAXBException {
    File file = new File(filePath);
    JAXBContext jaxbContext = JAXBContext.newInstance(ComplexType.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ComplexType errFile = (ComplexType) 
    jaxbUnmarshaller.unmarshal(file);
    return errFile;
  }

I'm using Intellij Comunity and Java 8. What am I doing wrong?


Solution

  • I think the package should be declared outside the generateDirectory using generatePackage :

    <plugin>
        <groupId>org.jvnet.jaxb2.maven2</groupId>
        <artifactId>maven-jaxb2-plugin</artifactId>
        <version>0.13.1</version>
        <configuration>
            <schemaDirectory>src/main/resources/schemas</schemaDirectory>
            <generateDirectory>src/main/java</generateDirectory>
            <generatePackage>com.evol.foo.generated-bar-sources</generatePackage>
        </configuration>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
    </plugin>