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.

lennartj commented 8 years ago

At the moment, some plugins which we depend upon are not yet updated for Java9.

[ERROR] Failed to execute goal org.codehaus.mojo:cobertura-maven-plugin:2.7:clean (default) on project jaxb2-maven-plugin: Execution default of goal org.codehaus.mojo:cobertura-maven-plugin:2.7:clean failed: Plugin org.codehaus.mojo:cobertura-maven-plugin:2.7 or one of its dependencies could not be resolved: Could not find artifact com.sun:tools:jar:0 at specified path /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/../lib/tools.jar -> [Help 1]
ge0ffrey commented 8 years ago

We're seeing a similar issue for Drools, when running a build, that works on JDK 8, on JDK 9: https://issues.jboss.org/browse/DROOLS-1170

lennartj commented 8 years ago

This is not an unimportant issue by any means. In its present form, the plugin will not be able to run on JDK9.

However, I am tempted to defer starting fixing it until this autumn to give other plugin developers the chance to make their plugins compliant with JDK9 before integrating the JDK9 support into the jaxb2-maven-plugin.

ge0ffrey commented 8 years ago

@lennartj A lot of high profile projects are now in full throttle in testing against JDK 9, as part of the "JDK 9 quality outreach" program started a few months ago. Here's a list of those projects: https://wiki.openjdk.java.net/display/quality/Quality+Outreach

That has been direct cause for us to start testing against JDK 9. Download JDK 9 can be easily downloaded here: https://jdk9.java.net/download/

gunnarmorling commented 8 years ago

For now I'm not using the jaxb2-maven-plugin for my Java 9 experiments as I couldn't get it to work. For some reason it would ignore the JAXB binding file I need to use (and which always was applied when using the plug-in with JDK 8).

So I'm invoking xjc explicitly now with some Maven trickery. I'm going to share it here, maybe it's helpful to others:

<!-- Create target dir -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <tasks>
                    <echo message="Creating target/generated-sources/jaxb"/>
                    <mkdir dir="./target/generated-sources/jaxb"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- Invoke xjc -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>generate schema types</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>xjc</executable>
        <arguments>
            <argument>-enableIntrospection</argument>
            <argument>-p</argument>
            <argument>org.hibernate.validator.internal.xml</argument>
            <argument>-extension</argument>
            <argument>-target</argument>
            <argument>2.1</argument>
            <argument>-d</argument>
            <argument>target/generated-sources/jaxb</argument>
            <argument>src/main/xsd/validation-configuration-1.1.xsd</argument>
            <argument>src/main/xsd/validation-mapping-1.1.xsd</argument>
            <argument>-b</argument>
            <argument>src/main/xjb/binding-customization.xjb</argument>
        </arguments>
    </configuration>
</plugin>

<!-- Add target dir to compilation -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>target/generated-sources/jaxb</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
lennartj commented 8 years ago

I am quite aware of this; although we will include support for JDK9 within the plugin I will start with a much-needed release (2.3) handling enums, enhanced schema generation and various other fixes.

After that, we should approach 2 things:

  1. Supporting Java 9, and
  2. See if we can merge the codebases for the two JAXB plugins (i.e. this one and the one from java.net). While this latter task is certainly optional, I believe it would be good to have 1 "semi-normative" JAXB plugin rather than two which works in different ways. However, this is not only up to me and the rest of the MojoHaus folks, but also Alexei and the committers at java.net.
lennartj commented 8 years ago

And also - Thanks @gunnarmorling for sharing the Exec-plugin recipe.

lennartj commented 7 years ago

For the 2.4 version of the plugin (or perhaps we should move to 3.0 for this feature), we should support JDK 9.

eolivelli commented 7 years ago

I am getting this error with 2.3.1 version:

[ERROR] Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc (xjc) on project magnews.uiapp.testcase: Execution xjc of goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc failed: A required class was missing while executing org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc: com/sun/codemodel/CodeWriter

Any plan to fix this issue ? is there a schedule for 2.4 ? I will be glad to help if needed for me it is a showstopper for the migration to Java9

lennartj commented 7 years ago

There are clear plans to fix it. However, the tools.jar and JDK organisation to some extent is revamped in JDK 9.

... so this issue requires a tad more attention to detail than usual, to enable the plugin to run both on JDK8 and earlier and (using the same codebase) JDK9

eolivelli commented 7 years ago

thank you @lennartj for your quick reply.

I see your point, I will wait. I will test the new version as soon the fix will be available

gunnarmorling commented 7 years ago

enable the plugin to run both on JDK8 and earlier and (using the same codebase) JDK9

On that specific one, it may be a good idea to produce a multi-release JAR, allowing to have different versions of specific classes, each tied to one Java version. I've blogged about building MR JARs recently.

Novanic commented 7 years ago

The workaround with exec-maven-plugin is not working in my (current) environment, it throws: "A type incompatibility occurred while executing org.codehaus.mojo:exec-maven-plugin:1.6.0:exec: java.base/java.lang.String cannot be cast to org.codehaus.mojo.exec.Modulepath"... And jaxb2-maven-plugin is still not runnable with Java 9 (it uses classes which are moved to an internal package, so it is not even possible to just open the visibility...). You have 2,5 months at maximum to get this working. ;-)

Update: Ok, it is working when version 1.5 of the exec-maven-plugin instead of the newer version 1.6. And I had to set the -encoding parameter. And I had require the deprecated java.xml.bind module. Now the project is working with Java 9.

rfscholte commented 6 years ago

https://stackoverflow.com/questions/46356092/jaxb2-maven-plugin-failing-on-java-9 is a good hint. For these java.se.ee modules we should find the matching dependency in Central. Most should be there, and it will also work with earlier java version(s).

subes commented 6 years ago

I fixed this issue by launching com.sun.tools.xjc.XJCFacade in a forked java process (because the facade calls System.exit -_-) inside my own plugin instead of continuing to scratch my head about why jaxb2-maven-plugin does not use .xjb schema bindings with java 9. At least this approach seems to be more robust regarding future java versions. Also this approach does not rely on the xjc executable being in the $PATH. I made this downwards compatible by using the approach from here to declare java version dependant dependencies. You can see the result here:

Maybe this helps in finding a solution for jaxb2-maven-plugin that is downwards compatible.

Also, if you get the error prefix '' is already bound to '', try disabling the episode file generation in xjc since it is turned on by default in jaxb2-maven-plugin as it seems.

ghost commented 6 years ago

Is there progress in resolving this problem when using Java 9 ?

Execution xjc of goal org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc failed: A required class was missing while executing org.codehaus.mojo:jaxb2-maven-plugin:2.3.1:xjc: com/sun/codemodel/CodeWriter

dlemmermann commented 6 years ago

Any news on this?

lennartj commented 6 years ago

Will start the development transition from the Maven2 compliant plugin to the Maven3 (and Java9) compliant plugin next week.

lennartj commented 6 years ago

Currently, there are a few plugins and/or code quality dependencies which are not Java 9 compliant.

Among those are Cobertura and Checkstyle. Will investigate if we can ensure plugin code quality without such plugins... or when they - in turn - expect to be Java 9 compliant.

ptahchiev commented 6 years ago

Java10 on the horizon, you guys still lack support for Java9 :(

jvissers commented 6 years ago

I'm also a bit worried about the fact that this plugin does not work for Java 9. The main problems seem to be related to schema bindings (https://github.com/mojohaus/jaxb2-maven-plugin/issues/104) and the episode generation. These issues are seen in the other JAXB maven plugin also - which seems to suggest that both plugins are suffering from a similar fundamental issue introduced with Java 9. I'm thinking about keeping around a Java 8 build that produces the Java source files out of XSD's and then reuse these to have them compiled with Java 9. Its not pretty, but given the fact that XSD=>JAXB is a dead end/legacy path for us anyway, we might be able to get away with it.

Obviously I'd prefer if the maven plugin ecosystem would be fully Java 9 compliant - but apparently that is easier said then done.

jvissers commented 6 years ago

FYI: Found that http://cxf.apache.org/cxf-xjc-plugin.html (version 3.2.1) - does honor schemaBindings correctly. So these guys apparently have fixed the issue ~ which they also ran into.

PascalSchumacher commented 6 years ago

A fix for this would be highly appreciated!

By the way: Thank you very much for providing the maven-jaxb2-plugin! 👍

tuxedo0801 commented 6 years ago

Any update on this?

martinjoconnor commented 6 years ago

Also keenly awaiting an update.

lennartj commented 6 years ago

Now that release 2.4 is performed, the task of morphing the plugin for compliance with java 9, 10 and 11 is started.

The main problem is the dependency on the tools.jar (which is removed in Java9) for the compilation part of the plugin. While toolchains is a great way of injecting relevant parts of the JDK to plugins, tools.jar is not present within java 9+. Investigating the best alternative approach.

lennartj commented 6 years ago

I have been able to compile and run the unit tests for the plugin on Java 10. However, the IntegrationTests currently fails since the invoker plugin do not add the required Java10 modules.

Typically, running the compiler and surefire plugins require some added modules:

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <configuration>
                                ...
                                <argLine>--add-modules=java.xml.bind</argLine>
                            </configuration>
                        </plugin>

... and ...

                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-compiler-plugin</artifactId>
                            <configuration>
                                ...
                                <compilerArgs>
                                    <arg>--add-modules</arg>
                                    <arg>java.xml.bind</arg>
                                </compilerArgs>
                            </configuration>
lennartj commented 6 years ago

However, the invoker plugin executes the failsafe plugin, which would need to add these options within a profile for java 9+.

... and I find it somewhat unclear to derive the optimal pattern for the corresponding module ... which is the java.activation module, according to the stack trace, and some investigation into the JDK:

Caused by: java.lang.ClassNotFoundException: javax.activation.MimeTypeParseException
    at org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy.loadClass(SelfFirstStrategy.java:50)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.unsynchronizedLoadClass(ClassRealm.java:271)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:247)
    at org.codehaus.plexus.classworlds.realm.ClassRealm.loadClass(ClassRealm.java:239)
    ... 50 more

... and

unzip -l src.zip | grep javax.activation.MimeTypeParseException
      757  03-27-2018 03:05   java.activation/javax/activation/MimeTypeParseException.java
eolivelli commented 6 years ago

From java9 onwards you have to explicitly add JAXB, Java Activation...dependency

<libs.jaxws.jaxb-api>2.3.0</libs.jaxws.jaxb-api>
<libs.jaxws.jaxb-impl>2.3.0</libs.jaxws.jaxb-impl>
<libs.javaxactivation.api>1.2.0</libs.javaxactivation.api>

and these are the deps:

         <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${libs.jaxws.jaxb-api}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>${libs.jaxws.jaxb-impl}</version>
        </dependency>
        <dependency>
            <groupId>javax.activation</groupId>
            <artifactId>javax.activation-api</artifactId>
            <version>${libs.javaxactivation.api}</version>
        </dependency>
        <dependency>
            <groupId>com.sun.activation</groupId>
            <artifactId>javax.activation</artifactId>
            <version>${libs.javaxactivation.api}</version>
        </dependency>

And consider upgrading surefire to the latest version, which has some fixes for jdk9/jdk10

Hope that helps

lennartj commented 6 years ago

But the problem arises from the Maven Invoker Plugin which fires the Failsafe plugin under the hood.

Also - the integration tests will need to work under both JDK 7,8,9 and 10. Still working on this.

webczat commented 6 years ago

can anyone tell me why jaxb/xjc fails to generate episodes? I mean what's the issue actually?

martinjoconnor commented 5 years ago

Any update on this?

We recently migrated our system to Java 10 but have one component still requiring Java 8 because of its dependence on this (excellent) plugin.

lennartj commented 5 years ago

The problem - currently - is related to the schemagen tool. When running the integration tests for the plugin, I get failures when running JDK 9, 10, and 11. The reason is that schemagen behaves differently from when run under JDK 8.

JDK 1.8 execution:
==================
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" targetNamespace="http://acme.com/customer-api" xmlns:tns="http://acme.com/customer-api" xmlns:xs="http://www.w3.org/2001/XMLSchema">

... but ...

JDK 9 execution:
================
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<schema version="1.0" targetNamespace="http://acme.com/customer-api" xmlns:tns="http://acme.com/customer-api" xmlns:xs="http://www.w3.org/2001/XMLSchema">

Notice that the namespace prefix of the schema element is missing from the JDK9 execution? This seems incorrect, and implies that the schema/root element is http://acme.com/customer-api instead of http://www.w3.org/2001/XMLSchema, which it should be.

As far as I can tell, this is another schemagen internal behavior change from JDK 8 to 9. I cannot seem to find any workaround to change this behavior. Due to the namespace being incorrect, the transformation steps will not apply their magic - because the plugin expects the schema element to reside in its proper namespace.

lennartj commented 5 years ago

So .. we need to report this as a bug against the schemagen tool in JDK 9 and 10. schemagen is altogether removed from JDK 11 - so we will hence need to re-implement the internals of this plugin to enable it running on top of JDK 11.

agusmba commented 5 years ago

Yes. I'm not sure if it's the same one as https://github.com/eclipse-ee4j/jaxb-ri/issues/1220 or if a new one should be opened.

Regarding the removal from JDK11, this can shed some light on how to integrate it: https://github.com/eclipse-ee4j/jaxb-ri/issues/1168#issuecomment-395753567

(Note: I've put the links to the new repository)

lennartj commented 5 years ago

Thanks!

I will take a look at the jaxb-ri project codebase to understand how to move on from here. I want to avoid "manually" inserting the "xs:" prefix within the top schema element, and all relevant child elements within the generated schema.

mw84367 commented 5 years ago

Also, if you get the error prefix '' is already bound to '', try disabling the episode file generation in xjc since it is turned on by default in jaxb2-maven-plugin as it seems.

This does no longer work since version 2.4, because the option is deprecated and thus ignored.

So how can this issue be fixed?

RickDeckard76 commented 5 years ago

Hi, I confirm version 2.4 doesn't work with jdk11. Not possible for us to completly migrate to JDK11 until a fix will be release. Any news?

anupamnayak commented 5 years ago

For now I'm not using the jaxb2-maven-plugin for my Java 9 experiments as I couldn't get it to work. For some reason it would ignore the JAXB binding file I need to use (and which always was applied when using the plug-in with JDK 8).

So I'm invoking xjc explicitly now with some Maven trickery. I'm going to share it here, maybe it's helpful to others:

<!-- Create target dir -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <tasks>
                    <echo message="Creating target/generated-sources/jaxb"/>
                    <mkdir dir="./target/generated-sources/jaxb"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- Invoke xjc -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>generate schema types</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>xjc</executable>
        <arguments>
            <argument>-enableIntrospection</argument>
            <argument>-p</argument>
            <argument>org.hibernate.validator.internal.xml</argument>
            <argument>-extension</argument>
            <argument>-target</argument>
            <argument>2.1</argument>
            <argument>-d</argument>
            <argument>target/generated-sources/jaxb</argument>
            <argument>src/main/xsd/validation-configuration-1.1.xsd</argument>
            <argument>src/main/xsd/validation-mapping-1.1.xsd</argument>
            <argument>-b</argument>
            <argument>src/main/xjb/binding-customization.xjb</argument>
        </arguments>
    </configuration>
</plugin>

<!-- Add target dir to compilation -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>target/generated-sources/jaxb</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

I am able to generated classes using this. But i have multiple xsd files and I want to generate classes in different packages! So i just used plugin multiple time. So problem I'm facing is that generated-sources folder getting cleared each time erasing old generated classes.

There was aption in jaxb2 maven plugin to disable clearOutputDirectory. But Im not finding this over using exec maven plugin.

Please can anyone help?

dvsanthosh commented 5 years ago

@lennartj , Can you please prioritize this issue. Seems like several folks are stuck with this issue.

Hi, I confirm version 2.4 doesn't work with jdk11. Not possible for us to completly migrate to JDK11 until a fix will be release. Any news?

lennartj commented 5 years ago

See the explanations above.

The plugin suffers from bugs introduced in schemagen tool (i.e. JDK) for JDK 9+ This is a JDK - not plugin - bug which must be fixed in the JDK.

RickDeckard76 commented 5 years ago

Hi Lennart, thx for your excellent work. Ok, I understand about your consideration that this is a JDK 9/10/11 bug; JDK11 is a LTS, do you have some rumors about the resolution of the bug? On the other side, you wrote: "I will take a look at the jaxb-ri project codebase to understand how to move on from here. I want to avoid "manually" inserting the "xs:" prefix within the top schema element, and all relevant child elements within the generated schema." Any news? Thx in advance.

dvsanthosh commented 5 years ago

@lennartj Maybe I'm missing something here. As per the jdk11 release notes,schemagen and xjc (from jdk.xml.bind) have been removed and the openjdk community provided alternatives to this package. Do you mean these APIs too are having bugs in it? Ref: https://openjdk.java.net/jeps/320 https://stackoverflow.com/questions/48204141/replacements-for-deprecated-jpms-modules-with-java-ee-apis/48204154#48204154

boukewoudstra commented 5 years ago

The xjc command util is missing in java11. However, this util just forwards any commands to the XJCFacade class from the jaxb-xjc package. Update: I am using the Driver class now since the JVM would otherwise shutdown ;).

I did some experiments to use this class directly. That seems possible. See for my experiment: https://github.com/boukewoudstra/jaxb-xjc-java11-test. Maybe it can be used as inspiration to get this working again ;)

I am using the jaxb version 2.3.1. Version 2.3.0 was still giving reflection errors.

missedone commented 5 years ago

with regards to the issue of missing xs: prefix with goal schemagen running in Java 11, the workaround is building plexus-classworlds 2.5.3-SNAPSHOT, see
https://github.com/eclipse-ee4j/jaxb-ri/issues/1220#issuecomment-449831200

here is my plugin dependency for your reference:


        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <dependencies>
                <dependency>
                    <groupId>org.glassfish.jaxb</groupId>
                    <artifactId>jaxb-jxc</artifactId>
                    <version>2.3.1</version>
                </dependency>
                <dependency>
                    <groupId>javax.activation</groupId>
                    <artifactId>javax.activation-api</artifactId>
                    <version>1.2.0</version>
                </dependency>
                <dependency>
                    <groupId>javax.xml.bind</groupId>
                    <artifactId>jaxb-api</artifactId>
                    <version>2.3.1</version>
                </dependency>
                <dependency>
                    <groupId>org.glassfish.jaxb</groupId>
                    <artifactId>jaxb-runtime</artifactId>
                    <version>2.3.1</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <id>schemagen</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>schemagen</goal>
                    </goals>
                </execution>
            </executions>
...
``
lennartj commented 5 years ago

I will take a look at the classworlds upgrade; from the comments, it looks promising. ... which implies that we only have to wait for a stable release of plexus-classworlds - but that should be a quicker process than the corresponding JDK release process.

slachiewicz commented 5 years ago

While waiting for Maven release - try with replacing plexus-classworld in local Maven install with the new version (2.6.0) from http://central.maven.org/maven2/org/codehaus/plexus/plexus-classworlds/2.6.0/

berardino commented 5 years ago

For now I'm not using the jaxb2-maven-plugin for my Java 9 experiments as I couldn't get it to work. For some reason it would ignore the JAXB binding file I need to use (and which always was applied when using the plug-in with JDK 8).

So I'm invoking xjc explicitly now with some Maven trickery. I'm going to share it here, maybe it's helpful to others:

<!-- Create target dir -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <tasks>
                    <echo message="Creating target/generated-sources/jaxb"/>
                    <mkdir dir="./target/generated-sources/jaxb"/>
                </tasks>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

<!-- Invoke xjc -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>generate schema types</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>exec</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <executable>xjc</executable>
        <arguments>
            <argument>-enableIntrospection</argument>
            <argument>-p</argument>
            <argument>org.hibernate.validator.internal.xml</argument>
            <argument>-extension</argument>
            <argument>-target</argument>
            <argument>2.1</argument>
            <argument>-d</argument>
            <argument>target/generated-sources/jaxb</argument>
            <argument>src/main/xsd/validation-configuration-1.1.xsd</argument>
            <argument>src/main/xsd/validation-mapping-1.1.xsd</argument>
            <argument>-b</argument>
            <argument>src/main/xjb/binding-customization.xjb</argument>
        </arguments>
    </configuration>
</plugin>

<!-- Add target dir to compilation -->
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>target/generated-sources/jaxb</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

the following also works and does not depend on the xjc executable :

<properties>
        <exec-maven-plugin.version>1.6.0</exec-maven-plugin.version>
        <jaxb-xjc.version>2.3.1</jaxb-xjc.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>${exec-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <phase>none</phase>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <includePluginDependencies>true</includePluginDependencies>
                    <mainClass>com.sun.tools.xjc.XJCFacade</mainClass>
                    <arguments>
                        <argument>-d</argument>
                        <argument>${project.build.sourceDirectory}</argument>
                        <argument>${project.basedir}/xsd</argument>
                    </arguments>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.glassfish.jaxb</groupId>
                        <artifactId>jaxb-xjc</artifactId>
                        <version>${jaxb-xjc.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
ghost commented 5 years ago

The other thing to note of course is that xjc has been removed from the jdk.xml.bind module. It is now a standalone zip bundle... https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-ri/2.4.0-b180830.0438

Perhaps this to should be added to the pom as a dependency..

<!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-ri -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-ri</artifactId>
    <version>2.4.0-b180830.0438</version>
    <type>pom</type>
</dependency>
davidmoten commented 5 years ago

plexus-classworlds 2.6.0 didn't work for me with Java 9, perhaps I'm using it incorrectly (though I'm generating from DTD not xsd):

    <profile>
        <id>j9</id>
        <activation>
            <jdk>[1.9,)</jdk>
        </activation>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxb2-maven-plugin</artifactId>
                    <version>2.4</version>
                    <executions>
                        <execution>
                            <goals>
                                <goal>xjc</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <!-- Set the package of the generated code -->
                        <packageName>au.gov.amsa.jul.jaxb</packageName>

                        <!-- Indicate that we should use DTD input instead 
                            of XSDs -->
                        <sourceType>dtd</sourceType>

                        <!-- Define the directory where we should find the 
                            DTD files -->
                        <sources>
                            <source>src/main/resources</source>
                        </sources>

                        <externalEntityProcessing>true</externalEntityProcessing>
                    </configuration>
                    <dependencies>
                        <dependency>
                            <groupId>org.codehaus.plexus</groupId>
                            <artifactId>plexus-classworlds</artifactId>
                            <version>2.6.0</version>
                        </dependency>
                        <dependency>
                            <groupId>com.sun.activation</groupId>
                            <artifactId>javax.activation</artifactId>
                            <version>1.2.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>

Output:

Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:2.4:xjc (default) on project logging-jaxb: Prefix '' is already bound to '' -> [Help 1]