I have been using JAXB in a Java project using JDK 8. Having migrated to JDK 11, the JAXB package name is no longer recognized. I have also not found a way to add JAXB as a dependency in gradle. How can I import JAXB into my project?
Tutorial: Using JAXB with Gradle Create a new Gradle project called com.vogella.xml.jaxb.gradle. Adjust your build.gradle file to include the javax.xml.bind and org.eclipse.persistence libraries as dependencies to your Gradle plug-in.
Step 1 – Open IntelliJ IDEA and Welcome Screen appears. Click the Open button present on Welcome Screen. Step 2 – Navigate to your Gradle project and select the top-level folder. Select the project you want to Import. Select the OK button to proceed to the next screen. Step 3 – A screen appears to Open or Import project.
In Java 9 and Java 10 we need to use the --add-modules=java.xml.bind option. In Java 11, JAXB has been removed from JDK and we need to add it to the project as a separate library via Maven or Gradle. In our examples, we use JDK 11 and Maven to create our applications.
Configure IntelliJ IDEA to use JAXB and XMLBeans Download and install the XMLBeans tool. In the Settings/Preferences dialog Ctrl+Alt+S, open the Plugins page and make sure that the bundled Java EE: Web Services (JAX-WS) plugin is enabled.
You need to include JAXB API and choose from one of the JAXB implementations because JAXB is is no more included by default in the JDK 11. You need to add some dependencies to your build.gradle
.
So first:
compile group: 'javax.xml.bind', name: 'jaxb-api', version: '2.3.1'
and if you decide to use for example MOXy, then something like:
compile group: 'org.eclipse.persistence', name: 'org.eclipse.persistence.moxy',
version: '2.7.3'
See also this great explanation
This example using MOXy also requires jaxb.properties
file containing information about JAXBContextFactory
(see here chapter 2.1):
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Advantage of this seems to be:
Because you do not need to change any application code, you can easily switch between different JAXB implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With