This post explains how to generate Java files from XSD using JAXB and Maven. I have created a simple Maven web application project and placed the XSD files under src\main\xsd folder of the root of the Maven project.
The pom.xml is as given below.
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.thea</groupId>
<artifactId>jaxbgenerator</artifactId>
<packaging>war</packaging>
<version>0.1</version>
<name>jaxbgenerator Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.3</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>central</id>
<name>Repository for Maven </name>
<url>http://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>codehausplugin</id>
<name>Repository for plugin </name>
<url>http://repository.codehaus.org/</url>
</pluginRepository>
</pluginRepositories>
<build>
<finalName>jaxbgenerator</finalName>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>com.thea.assessment</packageName>
<schemaFiles>college/StudentDetails.xsd</schemaFiles>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
The command to generate Java files is mvn jaxb2:xjc
If you notice the StudentDetails.xsd is under a folder college [that is from the project root it is under src\main\xsd\college]. Any other referred xsd files also can be placed under src\main\xsd.
By default the Java files are placed in the folder target\generated-sources\jaxb under project root. To override this you can specify outputDirectory value in the above pom.xml
<configuration>
<packageName>com.thea.assessment</packageName>
<outputDirectory>${basedir}/src/main/java</outputDirectory>
<schemaFiles>college/StudentDetails.xsd</schemaFiles>
</configuration>
As observed the generated Java files will have the package name com.thea.assessment.
If we do not specify any value for schemaFiles, all xsd files under src/main/xsd will be processed.
To know how to unmarshal (map) a xml file into the Java files generated using above steps please refer Unmarshal XML into Java using JAXB
