Which artifacts should I use for JAXB RI in my Maven project?
Asked Answered
F

2

53

Historically, I always used the following JAXB RI artifacts in my Maven projects:

  • com.sun.xml.bind:jaxb-impl - Runtime
  • com.sun.xml.bind:jaxb-xjc - Schema compiler
  • com.sun.xml.bind:jaxb-jxc - Schema generator

Since approximately version 2.2.10* these artifacts are now described as "old":

com.sun.xml.bind:jaxb-impl

Old JAXB Runtime module.

So it looks like these artifacts are now obsolete.

The question is:

Which artifacts should be used instead?

Firecracker answered 16/10, 2014 at 20:34 Comment(0)
F
83

After clarification with Oracle, the following artifacts should be used:

Runtime

If you want to unmarshal XML to Java objects or marshal Java objects as XML:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>...</version>
</dependency>

Schema compiler (XJC)

If you have an XML Schema and want to generate the Java code out of it:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-xjc</artifactId>
    <version>...</version>
</dependency>

Schema generator (JXC/schemagen)

If you have Java classes with JAXB annotations and want to generate a XML Schema based on them:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-jxc</artifactId>
    <version>...</version>
</dependency>

The two latter artifacts (org.glassfish.jaxb:jaxb-xjc and org.glassfish.jaxb:jaxb-jxc) are wrapped by Maven plugins so you normally would not need them in the runtime.

Eclipse usage

If your Maven projects somehow don't get the full classpath, turn on debug output and check the Maven console. You might be seeing the following error message there:

[ERROR] 'dependencyManagement.dependencies.dependency.systemPath' for com.sun:tools:jar must specify an absolute path but is ${tools.jar} @

This is due to the following problem:

Maven not picking JAVA_HOME correctly

The solution by @rustyx is to add -vm option to the eclipse.ini:

-vm
<PATH_TO_JDK>\jre\bin\javaw.exe
Firecracker answered 16/10, 2014 at 20:34 Comment(5)
Runtime bundle reference is what I was looking for. Perfectly worked. We dont have to include com.sun.xml.bind:jaxb-implBackwoods
I found the mention of the artifacts org.glassfish.jaxb:jaxb-jxc and org.glassfish.jaxb:jaxb-xjc for the schema generator and schema compiler. What is missing is how to use these artifacts to actually invoke those tools. Even more important with Java 11. An example using Gradle, would be great.Foolscap
@Foolscap That is out of the scope of this question. Please ask a separate question.Firecracker
@Firecracker Thanks, I've asked #51485637Foolscap
To marshal Java objects as XML in REST service response I had also to add <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency>Loser
E
1

For the latest JAXB 2 version 2.3.3 according to the Eclipse Implementation of JAXB these are the Maven coordinates for Eclipse implementation of JAXB artifacts:

<!-- API classes for Jakarta XML Binding. Required to compile against Jakarta XML Binding. -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
</dependency>

<!-- Contains the main runtime used for serialization and deserialization java objects to/from xml. -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.6</version>
</dependency>

<!-- Tool to generate Jakarta XML Binding java sources from XML representation. -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-xjc</artifactId>
    <version>2.3.6</version>
</dependency>

<!-- Tool to generate XML schema from Jakarta XML Binding java sources. -->
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-jxc</artifactId>
    <version>2.3.6</version>
</dependency>

Also, the documentation says that

In general com.sun.xml.bind artifacts are supposed to be used instead.

The maven coordinates for them are:

<!-- API classes for Jakarta XML Binding. Required to compile against Jakarta XML Binding. -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.3</version>
</dependency>

<!-- RI Implementation of JAXB runtime jar. -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.6</version>
</dependency>

<!-- Class generation tool jar. -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-xjc</artifactId>
    <version>2.3.6</version>
</dependency>

<!-- Schema generation tool jar. -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-jxc</artifactId>
    <version>2.3.6</version>
</dependency>

EDIT: The documentation for the JAXB version 2.3.3 has been removed already. Here's the link to the same section for JAXB 2.3.7

Extravascular answered 17/10, 2022 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.