Monday, January 9, 2023

maven generate java classes from xsd schema

 You can use the JAXB Maven plugin to generate Java classes from an XSD schema. Here's an example of how to do it:

  1. Add the JAXB plugin to your pom.xml file:
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> </configuration> <dependencies> <dependency> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.5.0</version> </dependency> </dependencies> </plugin> </plugins> </build>
<build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.4.0</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory> <outputDirectory>${basedir}/src/main/java</outputDirectory> </configuration> </plugin> </plugins> </build>
  1. Place your XSD schema file(s) in the ${basedir}/src/main/resources/xsd directory (or the directory specified in the schemaDirectory parameter in the plugin configuration).

  2. Run the following Maven command to generate the Java classes:

mvn clean generate-sources

The generated Java classes will be placed in the ${basedir}/src/main/java directory (or the directory specified in the outputDirectory parameter in the plugin configuration).

Note: This will only work if you have the necessary dependencies in your pom.xml file. Specifically, you will need the following dependency:

<dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency>

No comments:

Post a Comment