0

I want to work with Eclipse and jdk 22 with JAXB to compile an xsd schema in order to generate the java classes.

I don't have a Maven installation except the one built into Eclipse itself.

To do this I create a maven project in Eclipse and configure pom.xml as follows:

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.prueba</groupId> <artifactId>ruebaJAXB</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>jakarta.xml.bind</groupId> <artifactId>jakarta.xml.bind-api</artifactId> <version>4.0.2</version> </dependency> <dependency> <groupId>org.glassfish.jaxb</groupId> <artifactId>jaxb-runtime</artifactId> <version>2.3.3</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>2.4.0</version> <executions> <execution> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <schemaFiles> <schemaFile>${basedir}/src/main/resources/personas.xsd</schemaFile> </schemaFiles> <outputDirectory>${project.build.directory}/src/capaNegocio</outputDirectory> </configuration> </plugin> </plugins> </build> </project> 

I have a schema in src/main/resources. If I right-click on it under the Generate option, I only see the XML File option.

What steps do I need to take to generate Java classes from this schema?

1
  • Which version of JAXB API are you targeting ? Jakarta-based 4.x or Javax-based 2.3 ? You're importing API 4.x but Runtime 2.3 which are incompatible... In order to launch generation from Eclipse, you could right-click on the project itself and then "run as" menu -> "maven generate sources" Commented Nov 14, 2024 at 8:33

2 Answers 2

0

After you right-click on the project and let it Maven -> Update Project..., right-click on it again, choosing Run As... -> Maven Build... and make sure xjc is in the Goals text field.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using a recent Eclipse version, this should integrate the auto building process for maven projects with the jaxb2-maven-plugin without too much effort from the user (at least, a confirm that it has to install an addition for maven lifecycle mapping to manage that maven plugin).

You don't have usually to use the Generate > JAXB Classes... menu because the integration of the plugin in the building pipeline should already have generated all the xsd described classes automatically.

As an alternative to the jaxb2-maven-plugin, you can also consider using the cxf-xjc-plugin by Apache, like this:

<properties> <org.apache.cxf.xjc.version>4.0.2</org.apache.cxf.xjc.version> </properties> <dependencies> <dependency> <groupId>org.apache.cxf.xjc-utils</groupId> <artifactId>cxf-xjc-runtime</artifactId> <version>${org.apache.cxf.xjc.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-xjc-plugin</artifactId> <version>${org.apache.cxf.xjc.version}</version> <executions> <execution> <id>xjc</id> <goals> <goal>xsdtojava</goal> </goals> <phase>generate-sources</phase> <configuration> <fork>always</fork> <encoding>UTF-8</encoding> <additionalJvmArgs>-Duser.language=en</additionalJvmArgs> <xsdOptions> <xsdOption> <xsd>${basedir}/src/main/resources/personas.xsd</xsd> <packagename>com.mypackage</packagename> </xsdOption> </xsdOptions> </configuration> </execution> </executions> </plugin> 

In this case, the generated sources are put under the target/generated/src/main/java/com/mypackage directory.

Also in this case, the integration in Eclipse is quite seamlessy and the plugin appears in the Lifecycle Mapping of the project; the sources are generated automatically during the project building phase.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.