In this howto I will show you how to create a XML Schema from an annotated JAXB class using the maven jaxb-schemagen plugin.

STEP 1:  Annotate your domain classes with JAXB annotations.

In our example we have a simple Contact class with name, surname, birthday and a list of mails.

public class Contact {
        private String name;
        private String surname;
        private Birthday birthday;
        private List<Mail> mail;
       // Add getters & setters

And the Birthday class:

public class Birthday {
        private String day;
        private String month;
        private String year;
        // Add getters & setters

And the Mail class:

public class Mail {
        public enum MailType { PERSONAL, WORK };
        private String email;
        private MailType type;
        // Add getters & setters

STEP 2: Annotate the XML namespace as a package annotation


@XmlSchema (
                xmlns = {},
                namespace = "",
                elementFormDefault = XmlNsForm.UNSET,
                attributeFormDefault = XmlNsForm.UNSET
package net.jordeu.codesnips.xsdcreator;

import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlNsForm;

STEP 3: Config Maven pom.xml
First add this repository to the pom.xml or check that you have it in your settings.xml or proxy repository.

     <name> Maven 2 Repository</name>

And add this plugin changing the “includes” section and “schemas” section according to your project name and layout.



Checkout the sample project


One Response to “How to create a XSD from Java classes annotated with JAXB”

  1. Learner says:

    Good one! Save me heaps of work to figure out how to do schemagen :)

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free