Generating JAXB classes with Maven plugin

How to generate JAXB classes based on XSD file automatically using Maven?

First make sure that you compile your application using at least Java SE 6:

 

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
    <target>1.6</target>
    <source>1.6</source>
  </configuration>
</plugin>

 

 

Then goto this website: http://mojo.codehaus.org/jaxb2-maven-plugin/index.html

 

If you want to generate JAXB classes based on XSD, you internally use xjc command in JDK. Add this plugin to pom.xml:

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>1.6</version>
  <executions>
    <execution>
      <id>xjc</id>
      <goals>
        <goal>xjc</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <packageName>com.example.myschema</packageName>
  </configuration>
</plugin>

Change package name to whatever you like. Add your XSD file inside src/main/xsd and once you call at least mvn generate-sources, JAXB classes will be generated automatically inside target/generated-sources/jaxb. Btw. Maven phase generate-sources is called very early in Maven lifecycle (before compile).