mojohaus / jaxb2-maven-plugin

JAXB2 Maven Plugin
https://www.mojohaus.org/jaxb2-maven-plugin/
Apache License 2.0
106 stars 77 forks source link

Prepare jaxb2-maven-plugin to run on JDK 11 #43

Closed gunnarmorling closed 5 years ago

gunnarmorling commented 8 years ago

Hi, when running the jaxb2-maven-plugin on JDK 9 I am getting quite a handful of errors due to the usage of classes previously present in tools.jar and now not exposed any longer.

I could make it work by adding the following dependencies to my plugin configuration:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>codemodel</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>txw2</artifactId>
    <version>2.2.11</version>
</dependency>
<dependency>
    <groupId>com.sun.xsom</groupId>
    <artifactId>xsom</artifactId>
    <version>20140925</version>
</dependency>
<dependency>
    <groupId>com.sun.istack</groupId>
    <version>2.21</version>
    <artifactId>istack-commons-runtime</artifactId>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind.external</groupId>
    <artifactId>rngom</artifactId>
    <version>2.2.11</version>
</dependency>

I think the plug-in could be changed to always use these dependencies instead of relying on JDK-internal classes. Unfortunately, the POMs of these dependencies resolve to a non-existing variable ${tools}, so a warning about the POMs being invalid will be issued by mvn, but JAXB seems to work fine with them.

davidmoten commented 5 years ago

Tried the exec-maven-plugin solution suggested by @berardino but maven process is crashed after code is generated. Needs to fork I suppose but don't know how to do that yet.

missedone commented 5 years ago

@davidmoten You need to place the classworlds jar to $maven-home/boot rather than maven dependency Remember to delete or move the old jar

davidmoten commented 5 years ago

@davidmoten You need to place the classworlds jar to $maven-home/boot rather than maven dependency Remember to delete or move the old jar

Thanks @missedone. If that's the case I won't even try it because I want our build to work everywhere not just on a patched maven installation. I'll try the exec:exec workaround from @gunnarmorling now.

davidmoten commented 5 years ago

Ah, the exec:exec workaround from @gunnarmorling won't work from Java 11+ because xjc executable has been removed. I might make a simple xjc-maven-plugin using @subes work as basis (unfortunately his binary is not deployed to Maven Central and I might make some big simplifications to it).

davidmoten commented 5 years ago

I've released xjc-maven-plugin to Maven Central and it's working well for me. I did use @subes' approach (fork a process and call XJCFacade) but also restricted the classpath (bindings files were sometimes ignored otherwise). Usage is documented on the project home page.

The plugin is unit tested on Oracle JDK 8, 9, 10, 11 and OpenJDK 10, 11 (using Travis).

Hope that's useful for others too while jaxb2-maven-plugin is getting sorted out.

chrlembeck commented 5 years ago

@davidmoten : Thanks a lot for your solution. It's working almost fine on my project generating sources from xsds. Unfortunately, the xjc plugin generates its output in windows default encoding rather than UTF-8. Is there any way do configure it to produce UTF-8? The java 8 xjc documentation does not give any hint.

subes commented 5 years ago

@chrlembeck I think you can just change the system property file.encoding and xjc will pick it up:

https://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding

Maybe you can do this inside your pom.xml with a simple property tag, otherwise a configuration option needs to be added to the plugin.

chrlembeck commented 5 years ago

@subes, @davidmoten: I tried both: Adding <file.encoding>UTF-8</file.encoding> into the properties section of the pom.xml and setting the system properties via mvn -Dfile.encoding=UTF-8 clean compile. Unfortunately the was no effect and the generated source files can not be compiled because of its wrong encoding.

subes commented 5 years ago

Ok, it seems XJC has an option to set the encoding: https://docs.oracle.com/javase/9/tools/xjc.htm#JSWOR741

"-encoding encoding" This option specifies character encoding for generated source files.

So using a configuration option and passing this as the parameter should solve the issue. Or maybe just pass the file.encoding property value as the option.

edwardxia commented 5 years ago

I got the current version of this plugin working on JDK 8, 9, 10, 11 just by explicitly declaring all the necessary dependencies:

  <dependencies>
    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>
    <dependency>
      <groupId>com.sun.activation</groupId>
      <artifactId>jakarta.activation</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>

  <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>......</packageName>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.3.2</version>
          </dependency>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>jakarta.activation</artifactId>
            <version>1.2.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
RickDeckard76 commented 5 years ago

I got the current version of this plugin working on JDK 8, 9, 10, 11 just by explicitly declaring all the necessary dependencies:

  <dependencies>
    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>
    <dependency>
      <groupId>com.sun.activation</groupId>
      <artifactId>jakarta.activation</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>

  <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>......</packageName>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.3.2</version>
          </dependency>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>jakarta.activation</artifactId>
            <version>1.2.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

Doesn't work for me. Error is still:

"[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (XJC Build) on project test-backend: Prefix '' is already bound to '' -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (XJC Build) on project test-backend: Prefix '' is already bound to ''

RickDeckard76 commented 5 years ago

Hi all, I got the solution thanks to the excellent work of @davidmoten . For all that are in my same situation (switching to JDK11 having few few time): simply go to the github page of @davidmoten here https://github.com/davidmoten/jax-maven-plugin and use the plugin; it works without problems in my case. Edit: I pasted the wrong link, above there is the correct link to the jax-maven-plugin

davidmoten commented 5 years ago

Glad to hear it's working @RickDeckard76, I've migrated the xjc-maven-plugin code to a more generic plugin jax-maven-plugin that supports xjc, wsimport, wsgen and schemagen goals. I believe Rick used jax-maven-plugin successfully and I've applied it to a number of uses across my company codebase with success. I'd like to pick up more test cases to cover the problems mentioned in this thread so feel free to mention them on issues at jax-maven-plugin. By the way jax-maven-plugin is deployed to Maven Central.

sebastienvermeille commented 5 years ago

I confirm that @edwardxia solution works for OpenJDK 10. (After 4 hours looking everywhere... I finally got it working on java 10.

Hope it will works for 11 when we migrate to 11 :/

RickDeckard76 commented 5 years ago

I confirm that @edwardxia solution works for OpenJDK 10. (After 4 hours looking everywhere... I finally got it working on java 10.

Hope it will works for 11 when we migrate to 11 :/

I confirm, it works on JDK11 (I used openJDK11 and AdoptOpenJDK11)

k-tomaszewski commented 5 years ago

For me the solution provided by @edwardxia didn't work without adding a special workaround - I needed to add an extra, dummy XSD file to my project to get rid of the error: [ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (xjc) on project test-project: Prefix '' is already bound to ''

I've described the full solution here: https://artofcode.wordpress.com/2019/02/26/jaxb2-maven-plugin-2-4-and-java-11/

This workaround was verified for OpenJDK 11 .

charegre commented 5 years ago

I got the current version of this plugin working on JDK 8, 9, 10, 11 just by explicitly declaring all the necessary dependencies:

  <dependencies>
    <dependency>
      <groupId>org.glassfish.jaxb</groupId>
      <artifactId>jaxb-runtime</artifactId>
      <version>2.3.2</version>
    </dependency>
    <dependency>
      <groupId>com.sun.activation</groupId>
      <artifactId>jakarta.activation</artifactId>
      <version>1.2.1</version>
    </dependency>
  </dependencies>

  <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>......</packageName>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-xjc</artifactId>
            <version>2.3.2</version>
          </dependency>
          <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>jakarta.activation</artifactId>
            <version>1.2.1</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

Hi,

I am using OpenJDK 11.0.2 and I tried your approach but I am getting the following error:

org.xml.sax.SAXParseException: External parsing is disabled. Cannot parse URI: file:/Path_to_my/my.dtd at com.sun.xml.dtdparser.InputEntity.fatal (InputEntity.java:971) at com.sun.xml.dtdparser.InputEntity.init (InputEntity.java:140) at com.sun.xml.dtdparser.DTDParser.parseInternal (DTDParser.java:288) ... ... [ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (xjc) on project my-project: [ERROR] +=================== [XJC Error] [ERROR] | [ERROR] | 0: file:/Path_to_my/my.dtd [ERROR] | [ERROR] +=================== [End XJC Error]

I checked the permissions of my.dtd, and it should be able to read it, I even changed them to all read, write and execute.

It still fails, any suggestions for OpenJDK 11 ?

lennartj commented 5 years ago

Updated all JAXB-API dependencies to the Jakarta artifacts (see #138), and got the full suite of IT tests to build on JDK 11 as well.

However, it implies that one adds the dependency below (since the javax.xml.bind is not in the JDK any longer):

<dependency>
  <groupId>jakarta.xml.bind</groupId>
  <artifactId>jakarta.xml.bind-api</artifactId>
  <version>2.3.2</version>
</dependency>
shavo007 commented 4 years ago

@edwardxia magic mate! such a minefield to get this plugin working with java 11. i just wanted to use schema gen...

DhruvAShah commented 1 year ago

Hi , I was getting similar compilation errors while building maven with Java 17 and maven-jaxb2 plugin 2.3.0 . I tried latest version as well but got Mojo Exception while building the same with Maven . After researching a bit I was able to fix the issue using below implementation for jaxb2-plugin ,

Java - 17 Jaxb2-maven-plugin - 0.14.0 Spring Boot Maven - 2.7.4

In properties Tag add below version ,

0.14.0 In Build Tag , org.jvnet.jaxb2.maven2 maven-jaxb2-plugin ${maven-jaxb2-plugin.version} example-xmls generate ${project.basedir}/src/main/java/com/cumulocity/exchnage_Demo ${project.basedir}/src/main/resources/ **/*.xsd ${project.basedir}/src/main/resources/ **/*.xjb org.glassfish.jaxb jaxb-runtime 2.3.3 After this JAXB-2 Implementation was successfully generated . Make sure to add below dependency as well helpful to provide implementation during runtime (When we run the main class) org.glassfish.jaxb jaxb-runtime 2.3.3 runtime This should work for all those struggling with Java 17 and spring boot version 2.7.4+ with jabxb2 maven plugin.