jaxb2-maven-plugin 2.4 and Java 10

Well, based on my own experience and based on discussion here I claim that:

Maven plugin “jaxb2-maven-plugin” in version 2.4 is not ready for Java 10.

Java version:

$ java -version
java version "10.0.1" 2018-04-17
Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

Maven version:

$ mvn -v
Apache Maven 3.5.2
Maven home: /usr/share/maven
Java version: 10.0.1, vendor: Oracle Corporation
...

Error reported by Maven:

[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (xjc) on project ...: Execution xjc of goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc failed: A required class was missing while executing org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc: javax/activation/DataSource
[ERROR] -----------------------------------------------------
[ERROR] realm =    plugin>org.codehaus.mojo:jaxb2-maven-plugin:2.4
[ERROR] strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy
[ERROR] urls[0] = file:/home/.../.m2/repository/org/codehaus/mojo/jaxb2-maven-plugin/2.4/jaxb2-maven-plugin-2.4.jar
[ERROR] urls[1] = file:/home/.../.m2/repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar
[ERROR] urls[2] = file:/home/.../.m2/repository/org/glassfish/jaxb/jaxb-core/2.3.0/jaxb-core-2.3.0.jar
[ERROR] urls[3] = file:/home/.../.m2/repository/org/glassfish/jaxb/txw2/2.3.0/txw2-2.3.0.jar
[ERROR] urls[4] = file:/home/.../.m2/repository/org/glassfish/jaxb/jaxb-runtime/2.3.0/jaxb-runtime-2.3.0.jar
[ERROR] urls[5] = file:/home/.../.m2/repository/org/jvnet/staxex/stax-ex/1.7.8/stax-ex-1.7.8.jar
[ERROR] urls[6] = file:/home/.../.m2/repository/com/sun/xml/fastinfoset/FastInfoset/1.2.13/FastInfoset-1.2.13.jar
[ERROR] urls[7] = file:/home/.../.m2/repository/org/glassfish/jaxb/jaxb-xjc/2.3.0/jaxb-xjc-2.3.0.jar
[ERROR] urls[8] = file:/home/.../.m2/repository/org/glassfish/jaxb/xsom/2.3.0/xsom-2.3.0.jar
[ERROR] urls[9] = file:/home/.../.m2/repository/org/glassfish/jaxb/codemodel/2.3.0/codemodel-2.3.0.jar
[ERROR] urls[10] = file:/home/.../.m2/repository/com/sun/xml/bind/external/rngom/2.3.0/rngom-2.3.0.jar
[ERROR] urls[11] = file:/home/.../.m2/repository/com/sun/xml/dtd-parser/dtd-parser/1.2/dtd-parser-1.2.jar
[ERROR] urls[12] = file:/home/.../.m2/repository/com/sun/istack/istack-commons-tools/3.0.5/istack-commons-tools-3.0.5.jar
[ERROR] urls[13] = file:/home/.../.m2/repository/org/apache/ant/ant/1.7.0/ant-1.7.0.jar
[ERROR] urls[14] = file:/home/.../.m2/repository/org/apache/ant/ant-launcher/1.7.0/ant-launcher-1.7.0.jar
[ERROR] urls[15] = file:/home/.../.m2/repository/relaxngDatatype/relaxngDatatype/20020414/relaxngDatatype-20020414.jar
[ERROR] urls[16] = file:/home/.../.m2/repository/org/glassfish/jaxb/jaxb-jxc/2.3.0/jaxb-jxc-2.3.0.jar
[ERROR] urls[17] = file:/home/.../.m2/repository/com/thoughtworks/qdox/qdox/2.0-M8/qdox-2.0-M8.jar
[ERROR] urls[18] = file:/home/.../.m2/repository/org/sonatype/sisu/sisu-inject-bean/2.3.0/sisu-inject-bean-2.3.0.jar
[ERROR] urls[19] = file:/home/.../.m2/repository/org/sonatype/sisu/sisu-guice/3.1.0/sisu-guice-3.1.0-no_aop.jar
[ERROR] urls[20] = file:/home/.../.m2/repository/org/sonatype/sisu/sisu-guava/0.9.9/sisu-guava-0.9.9.jar
[ERROR] urls[21] = file:/home/.../.m2/repository/org/sonatype/aether/aether-util/1.13.1/aether-util-1.13.1.jar
[ERROR] urls[22] = file:/home/.../.m2/repository/org/codehaus/plexus/plexus-interpolation/1.14/plexus-interpolation-1.14.jar
[ERROR] urls[23] = file:/home/.../.m2/repository/org/codehaus/plexus/plexus-component-annotations/1.5.5/plexus-component-annotations-1.5.5.jar
[ERROR] urls[24] = file:/home/.../.m2/repository/org/sonatype/plexus/plexus-sec-dispatcher/1.3/plexus-sec-dispatcher-1.3.jar
[ERROR] urls[25] = file:/home/.../.m2/repository/org/sonatype/plexus/plexus-cipher/1.4/plexus-cipher-1.4.jar
[ERROR] urls[26] = file:/home/.../.m2/repository/org/apache/maven/plugin-tools/maven-plugin-annotations/3.5.1/maven-plugin-annotations-3.5.1.jar
[ERROR] urls[27] = file:/home/.../.m2/repository/org/apache/maven/skins/maven-fluido-skin/1.6/maven-fluido-skin-1.6.jar
[ERROR] urls[28] = file:/home/.../.m2/repository/org/codehaus/plexus/plexus-compiler-api/2.5/plexus-compiler-api-2.5.jar
[ERROR] urls[29] = file:/home/.../.m2/repository/org/codehaus/plexus/plexus-utils/3.1.0/plexus-utils-3.1.0.jar
[ERROR] urls[30] = file:/home/.../.m2/repository/org/sonatype/plexus/plexus-build-api/0.0.7/plexus-build-api-0.0.7.jar
[ERROR] urls[31] = file:/home/.../.m2/repository/com/sun/istack/istack-commons-runtime/3.0.5/istack-commons-runtime-3.0.5.jar
[ERROR] Number of foreign imports: 1
[ERROR] import: Entry[import  from realm ClassRealm[maven.api, parent: null]]
[ERROR] 
[ERROR] -----------------------------------------------------
[ERROR] : javax.activation.DataSource

Relevant parts of pom.xml to reproduce:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>...</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>10</maven.compiler.source>
		<maven.compiler.target>10</maven.compiler.target>
	</properties>
	<!-- ... -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>jaxb2-maven-plugin</artifactId>
				<version>2.4</version>
				<executions>
					<execution>
						<id>xjc</id>
						<goals>
							<goal>xjc</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<packageName>some.package</packageName>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

NOTE: The bad news is that none of older versions of jaxb2-maven-plugin worked with Java 10 as well.

Solution

The solution is to use exec-maven-plugin to invoke xjc directly. The version of this plugin that worked for me with Java 10 was 1.5.0 – so not the latest one at the time of writing this article (1.6.0).

But we need 2 more Maven plugins: maven-antrun-plugin and build-helper-maven-plugin.

The maven-antrun-plugin is there to create folders on path {project directory}/target/generated-sources as xjc rejects to create missing directories itself.

The build-helper-maven-plugin just adds the folder {project directory}/target/generated-sources to set of Java sources compiled by Maven.

Relevant parts of pom.xml to make it working with Java 10, assuming the XSD file path is src/main/xsd/schema.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>...</groupId>
	<artifactId>...</artifactId>
	<version>...</version>
	<packaging>jar</packaging>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>10</maven.compiler.source>
		<maven.compiler.target>10</maven.compiler.target>
	</properties>
	<!-- ... -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-antrun-plugin</artifactId>
				<version>1.7</version>
				<executions>
					<execution>
						<phase>initialize</phase>
						<goals>
							<goal>run</goal>
						</goals>						
						<configuration>
							<tasks>
								<mkdir dir="target/generated-sources"/>
							</tasks>
						</configuration>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>exec-maven-plugin</artifactId>
				<version>1.5.0</version>
				<executions>
					<execution>
						<phase>generate-sources</phase>
						<goals>
							<goal>exec</goal>
						</goals>
					</execution>
				</executions>
				<configuration>
					<executable>xjc</executable>
					<arguments>
						<argument>-p</argument>
						<argument>some.package</argument>
						<argument>-d</argument>
						<argument>target/generated-sources</argument>
						<argument>src/main/xsd/schema.xsd</argument>
					</arguments>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>build-helper-maven-plugin</artifactId>
				<version>3.0.0</version>
				<executions>
					<execution>
						<phase>process-sources</phase>
						<goals>
							<goal>add-source</goal>
						</goals>
						<configuration>
							<sources>
								<source>target/generated-sources</source>
							</sources>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

The last thing to mention is that we need an additional dependency to jaxb-api – this solution should work even with Java 11 (hopefully…):

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

Ufff… Or you can just return to Java 8 instead. 🙂

Advertisements

About krzysztoftomaszewski

I've got M.Sc. in software engineering. I graduated in 2005 at Institute of Computer Science, Warsaw University of Technology, Faculty of Electronics and Information Technology. I'm working on computer software design and engineering continuously since 2004.
This entry was posted in Java and tagged , , . Bookmark the permalink.

3 Responses to jaxb2-maven-plugin 2.4 and Java 10

  1. Razvan P says:

    I’m experiencing the same problem. Initially the activation library was missing, and subsequently got Prefix ” is already bound to ”. More I’m working with Java 10, more I discover how much it sucks!

    Your solution looks good, but so does a batch command.

    • My impressions are different. So far I think Java 10 is OK. But all tools around are still so not prepared. When trying to use Java 10 we’re discovering how many tools (maven plugings, libraries) are using internal APIs. It was always a bad practise, still tool/libs developers were using it. Now it pays back. The conclusion is that we need to wait so all these things are updated to Java 10.

  2. I don’t know how you put this together, but thank you. It’s the first complete solution I’ve seen. You’re likely almost as good as you think you are. (that’s a compliment)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s