Tutorials  /  JavaScript

How to Generate XSD from Java Class with JAXB-2 Maven Plugin

Ccentron Redaktion · December 2024 ·3 min read ·JavaScript, Tutorial

Introduction

Generating XML Schema Definitions (XSD) from Java classes is a common requirement when working with JAXB in Java-based applications. By using the JAXB-2 Maven Plugin, you can simplify this process and automate schema generation directly within your Maven projects. This guide walks you through the setup, configuration, and execution to generate XSD files efficiently from annotated Java classes.

We will use JAXB-2 Maven Plugin in a Maven project to generate XSD from Java classes.

JAXB2 Maven Plugin uses JAXB SchemaGenerator utility to generate XSD from Java classes. Java classes should have JAXB annotations to be used by this plugin. Minimum Java version required is Java 5.

VM

Matching infrastructure at centron

Node applications need somewhere to run: ccloud³ VMs with full root access from €3.12 per month – or as a managed server if you would rather not run it yourself. Rent a cloud server →

Setting up the Maven Project

First, create a new Maven project. You can give any name, group ID, and artifact ID you want. Once we build our project, it will generate XSD classes in the

target/generated-resources/schemagen directory. After build, our project structure will look like the image below.

Final pom.xml File

Here is the final pom.xml file we have:

XML
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jaxb-schemagen</groupId>
	<artifactId>jaxb-schemagen</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<dependencies>
		<dependency>
			<groupId>javax.xml.bind</groupId>
			<artifactId>jaxb-api</artifactId>
			<version>2.1</version>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-compiler-plugin</artifactId>
					<version>2.5.1</version>
				</plugin>
			</plugins>
		</pluginManagement>

		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>1.5</version>
				<executions>
					<execution>
						<id>schemagen</id>
						<goals>
							<goal>schemagen</goal>
						</goals>
					</execution>
				</executions>

				<configuration>
					<transformSchemas>
						<transformSchema>
							<uri>https://www.example.org/employee</uri>
							<toPrefix>empns</toPrefix>
							<toFile>employee.xsd</toFile>
						</transformSchema>
						<transformSchema>
							<uri>https://www.example.org/address</uri>
							<toPrefix>addrns</toPrefix>
							<toFile>address.xsd</toFile>
						</transformSchema>
					</transformSchemas>
					<includes>
						<include>com/journaldev/bean/*</include>
					</includes>
					<verbose>true</verbose>

				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

Java Classes

Here are the Java classes we have that will be used to generate XSD:

Employee.java

Java
package com.journaldev.bean;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;

@XmlType(namespace = "https://www.example.org/employee")
public class Employee {
    private String name;
    private int id;
    private String role;
    private Address address;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlAttribute
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }
}

Address.java

Java
package com.journaldev.bean;

import javax.xml.bind.annotation.XmlType;

@XmlType(namespace = "https://www.example.org/address")
public class Address {
    private String city;
    private int zip;
    private String addressLine1;
    private String addressLine2;

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public int getZip() {
        return zip;
    }

    public void setZip(int zip) {
        this.zip = zip;
    }

    public String getAddressLine1() {
        return addressLine1;
    }

    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }

    public String getAddressLine2() {
        return addressLine2;
    }

    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }
}

Generated XSD Files

Our project setup is ready. Just build the project using the command mvn clean install, and the XSD files will be generated.

employee.xsd

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema" targetNamespace="https://www.example.org/address" version="1.0">

  <xs:complexType name="address">
    <xs:sequence>
      <xs:element minOccurs="0" name="addressLine1" type="xs:string"/>
      <xs:element minOccurs="0" name="addressLine2" type="xs:string"/>
      <xs:element minOccurs="0" name="city" type="xs:string"/>
      <xs:element name="zip" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

address.xsd

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="https://www.w3.org/2001/XMLSchema" xmlns:addrns="https://www.example.org/address" targetNamespace="https://www.example.org/employee" version="1.0">

  <xs:import namespace="https://www.example.org/address" schemaLocation="address.xsd"/>

  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element minOccurs="0" name="address" type="ns1:address"/>
      <xs:element minOccurs="0" name="name" type="xs:string"/>
      <xs:element minOccurs="0" name="role" type="xs:string"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:int" use="required"/>
  </xs:complexType>
</xs:schema>

Conclusion

Generating XSD from Java classes using the JAXB-2 Maven Plugin is a straightforward and efficient process. By following the steps outlined above, you can automate schema creation and maintain consistency in your XML data structures. This approach is highly beneficial for projects that require frequent updates to schemas or rely on strongly typed XML interactions. We hope this guide makes your schema generation process simpler and more reliable.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie JavaScript
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren