Search code examples
jaxbxjc

jaxb2-basics-annotate plugin migration to jaxb 3.0 and jakarta namespace


Years ago I have developed a maven archetype for creating a skeleton project that uses an xsd schema and a global.xjb to generate jaxb classes through xjc. It is based on com.evolvedbinary.maven.jvnet:jaxb2-maven-plugin, org.jvnet.jaxb2_commons:jaxb2-basics-annotate (in order to add JacksonProperty annotations) and com.fillumina:krasa-jaxb-tools (to apply Bean Validation instead of jaxb schema validation) I would like to refactor it in order to comply to the new JAXB 3.0 and in particular to the the jakarta namespace that has superseeded the old javax. jaxb2-maven-plugin has a new version for that (jaxb30-maven-plugin). krasa-jaxb-tools got a new option validationAnnotations = ( jakarta | javax ). And everything works fine except for annotations. I couldn't find anything new for jaxb2-basics-annotate plugin. Who knows if is there a new refactored version? Also a org.jvnet.jaxb2_commons:jaxb2-basics could be useful, for generating equals method, etc. Many thanks

UPDATE

This is the new pom with the use of the new plugins refactored by Rick O'Sullivan

<?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>org.example</groupId>
    <artifactId>jaxb-xjc-sample</artifactId>
    <version>1.0-SNAPSHOT</version>
    <name>jaxb-xjc-sample</name>
    <description>Generate java classes from schema through xjc</description>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <resources-dir>src/main/resources</resources-dir>
        <jaxb-resources-dir>${resources-dir}/jaxb</jaxb-resources-dir>
        <maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
        <!-- jaxb3 -->
        <jaxb-api.version>4.0.0</jaxb-api.version>
        <!-- jaxb2 -->
        <!-- <jaxb-api.version>2.3.1</jaxb-api.version> -->
        <!-- jaxb3 -->
        <jaxb-runtime.version>4.0.1</jaxb-runtime.version>
        <!-- jaxb2 -->
        <!-- <jaxb-runtime.version>2.3.7</jaxb-runtime.version> -->
        <jackson.version>2.14.0</jackson.version>
        <higherjaxb-maven-plugin.version>2.0.0</higherjaxb-maven-plugin.version>
        <basicjaxb-plugins.version>2.0.0</basicjaxb-plugins.version>
        <basicjaxb-runtime.version>2.0.0</basicjaxb-runtime.version>
        <hyperjaxb-annox-plugin.version>2.0.0</hyperjaxb-annox-plugin.version>
        <krasa-jaxb-tools.version>2.2</krasa-jaxb-tools.version>
        <!-- jaxb3 -->
        <validation-api.version>3.0.2</validation-api.version>
        <!-- jaxb2 -->
        <!-- <validation-api.version>2.0.1.Final</validation-api.version> -->
        <junit-jupiter.version>5.9.1</junit-jupiter.version>
        <slf4j.version>2.0.4</slf4j.version>
        <surefire-plugin.version>3.0.0-M7</surefire-plugin.version>
        <commons-utils.version>1.0.0</commons-utils.version>
        <commons-logging.version>1.2</commons-logging.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.fsajeva.commons</groupId>
            <artifactId>commons-utils</artifactId>
            <version>${commons-utils.version}</version>
        </dependency>
        <!-- basicjaxb-runtime is needed by basicjaxb.plugins for some args, e.g. -Xequals
        uncomment or comment this dependency in case of use -->
        <dependency>
            <groupId>org.patrodyne.jvnet</groupId>
            <artifactId>hisrc-basicjaxb-runtime</artifactId>
            <version>${basicjaxb-runtime.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${jackson.version}</version>
        </dependency>
        <!-- jaxb3 -->
        <dependency>
            <groupId>jakarta.validation</groupId>
            <artifactId>jakarta.validation-api</artifactId>
            <version>${validation-api.version}</version>
        </dependency>
        <!-- jaxb2 -->
        <!-- <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>${validation-api.version}</version>
        </dependency> -->
        <!-- jaxb3 -->
        <dependency>
            <groupId>jakarta.xml.bind</groupId>
            <artifactId>jakarta.xml.bind-api</artifactId>
            <version>${jaxb-api.version}</version>
        </dependency>
        <!-- jaxb2 -->
        <!-- <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>${jaxb-api.version}</version>
        </dependency> -->
        <dependency>
            <groupId>org.glassfish.jaxb</groupId>
            <artifactId>jaxb-runtime</artifactId>
            <version>${jaxb-runtime.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>${slf4j.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>${maven-compiler-plugin.version}</version>
            </plugin>
            <plugin>
                <groupId>org.patrodyne.jvnet</groupId>
                <!-- jaxb3 -->
                <artifactId>hisrc-higherjaxb30-maven-plugin</artifactId>
                <!-- jaxb2 -->
                <!-- <artifactId>hisrc-higherjaxb23-maven-plugin</artifactId> -->
                <version>${higherjaxb-maven-plugin.version}</version>
                <executions>
                    <execution>
                        <id>generate</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <extension>true</extension>
                    <args>
                        <arg>-Xannotate</arg>
                        <arg>-Xsetters</arg>
                        <arg>-Xsetters-mode=direct</arg>
                        <arg>-XJsr303Annotations</arg>
                        <!-- just comment this in case of jaxb2 as javax is the default value -->
                        <arg>-XJsr303Annotations:validationAnnotations=jakarta</arg>
                    </args>
                    <schemaDirectory>${jaxb-resources-dir}/xsd</schemaDirectory>
                    <schemaIncludes>
                        <include>books.xsd</include>
                    </schemaIncludes>
                    <bindingDirectory>${jaxb-resources-dir}/xjb</bindingDirectory>
                    <bindingIncludes>
                        <include>global.xjb</include>
                    </bindingIncludes>
                    <plugins>
                        <plugin>
                            <groupId>org.patrodyne.jvnet</groupId>
                            <artifactId>hisrc-basicjaxb-plugins</artifactId>
                            <version>${basicjaxb-plugins.version}</version>
                        </plugin>
                        <plugin>
                            <groupId>org.patrodyne.jvnet</groupId>
                            <artifactId>hisrc-hyperjaxb-annox-plugin</artifactId>
                            <version>${hyperjaxb-annox-plugin.version}</version>
                        </plugin>
                        <plugin>
                            <groupId>com.fillumina</groupId>
                            <artifactId>krasa-jaxb-tools</artifactId>
                            <version>${krasa-jaxb-tools.version}</version>
                        </plugin>
                    </plugins>
                </configuration>
                <dependencies>
                    <!-- jaxb3 -->
                    <dependency>
                        <groupId>jakarta.validation</groupId>
                        <artifactId>jakarta.validation-api</artifactId>
                        <version>${validation-api.version}</version>
                    </dependency>
                    <!-- jaxb2 -->
                    <!-- <dependency>
                        <groupId>javax.validation</groupId>
                        <artifactId>validation-api</artifactId>
                        <version>${validation-api.version}</version>
                    </dependency> -->
                    <dependency>
                        <groupId>com.fasterxml.jackson.core</groupId>
                        <artifactId>jackson-annotations</artifactId>
                        <version>${jackson.version}</version>
                    </dependency>
                    <!-- needed by Xsetters-mode=direct -->
                    <dependency>
                        <groupId>commons-logging</groupId>
                        <artifactId>commons-logging</artifactId>
                        <version>${commons-logging.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>${surefire-plugin.version}</version>
                <configuration>
                    <parallel>methods</parallel>
                    <threadCount>10</threadCount>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Of course you have to modify your global.xjb accordingly

<?xml version="1.0" encoding="UTF-8"?>

<!-- jaxb3 -->
<jaxb:bindings version="3.0"
               xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jaxb:extensionBindingPrefixes="annox"
               xmlns:annox="http://jvnet.org/basicjaxb/xjc/annox">

<!-- jaxb2 -->
<!-- <jaxb:bindings version="2.1"
               xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
               xmlns:xsd="http://www.w3.org/2001/XMLSchema"
               jaxb:extensionBindingPrefixes="annox"
               xmlns:annox="http://jvnet.org/basicjaxb/xjc/annox"> -->

Remember also to use at least version 3.8.4 of maven


Solution

  • Federico, my forks of the JAXB2 projects, originally developed by the admirable Alexey Valikov (a.k.a. Highsource), support JAXB 3.0 and in particular the jakarta namespace.

    This project is a fork of org.jvnet.annox:annox-project:

    This project is a fork of org.jvnet.jaxb2_commons:jaxb2-annotate-plugin-project:

    Note: Review my projects' readme for changes to package names and namespaces. Plus, each readme has links for my forks of other Highsource projects, including hisrc-basicjaxb to generate hashCode, equals, toString methods, etc.