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

    Copyright (c) 2018, 2021 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Distribution License v. 1.0, which is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

    SPDX-License-Identifier: BSD-3-Clause

-->

<!DOCTYPE book [
<!ENTITY % ents SYSTEM "docbook.ent">
%ents;
]>
<section version="5.0"
         xml:id="deployment-maven-coordinates"
         xml:lang="en" xmlns="http://docbook.org/ns/docbook"
         xmlns:xlink="http://www.w3.org/1999/xlink"
         xmlns:ns5="http://www.w3.org/1999/xhtml"
         xmlns:ns3="http://www.w3.org/2000/svg"
         xmlns:ns="http://docbook.org/ns/docbook"
         xmlns:m="http://www.w3.org/1998/Math/MathML">
    <title>Using JAXB with Maven</title>


    <section xml:id="Maven_artifacts">
        <title>Maven coordinates for JAXB artifacts</title>
        <itemizedlist>
            <listitem>
                <para><emphasis role="bold">jakarta.xml.bind:jakarta.xml.bind-api:</emphasis> API classes for JAXB.
                    Required to compile against JAXB.</para>
            </listitem>
            <listitem>
                <para><emphasis role="bold">org.glassfish.jaxb:jaxb-runtime:</emphasis> Contains the main runtime used
                    for serialization and deserialization java objects to/from xml.</para>
            </listitem>
            <listitem>
                <para><emphasis role="bold">org.glassfish.jaxb:jaxb-xjc:</emphasis> Tool to generate JAXB java sources
                    from XML representation.</para>
            </listitem>
            <listitem>
                <para><emphasis role="bold">org.glassfish.jaxb:jaxb-jxc:</emphasis> Tool to generate XML schema from
                    JAXB java sources.</para>
            </listitem>
        </itemizedlist>
    </section>

    <section xml:id="Fat-jar">
        <title>JAXB fat-jar bundles</title>
        <itemizedlist>
            <listitem>
                <para><emphasis role="bold">com.sun.xml.bind:jaxb-impl:</emphasis> JAXB runtime fat jar.</para>
            </listitem>
            <listitem>
                <para><emphasis role="bold">com.sun.xml.bind:jaxb-xjc:</emphasis> Class generation tool fat jar.</para>
            </listitem>
            <listitem>
                <para><emphasis role="bold">com.sun.xml.bind:jaxb-jxc:</emphasis> Schema generation tool fat jar.</para>
            </listitem>
        </itemizedlist>
        <para>In contrast to <literal>org.glassfish.jaxb</literal> artifacts, these jars have all dependency classes included inside. These artifacts does not contain JPMS module descriptors.
            In Maven projects <literal>org.glassfish.jaxb</literal> artifacts are supposed to be used instead. </para>
    </section>

    <section xml:id="Binary-distribution">
        <title>Binary distribution</title>
        <itemizedlist>
            <listitem>
                <para><emphasis role="bold">com.sun.xml.bind:jaxb-ri:</emphasis> Zip distribution containing tooling
                    scripts and all dependency jars in one archive.</para>
            </listitem>
        </itemizedlist>
    </section>

    <section xml:id="Api_and_runtime">
        <title>JAXB API and Runtime</title>
        <para>
            Minimum requirement to compile is jaxb-api. If a client application is running on an environment where JAXB
            runtime is provided, <literal>jaxb-api</literal> is all that is needed.
                <example><title>API only</title>

                <programlisting language="xml"><![CDATA[
                <!-- API -->
                <dependency>
                    <groupId>jakarta.xml.bind</groupId>
                    <artifactId>jakarta.xml.bind-api</artifactId>
                    <version>${jaxb.version}</version>
                </dependency>
             ]]></programlisting>
            </example></para>

        <para>
            If client application needs to include the runtime, e.g. running standalone on JavaSE
            <literal>jaxb-runtime</literal> should be also included.
            <example><title>API + Runtime</title>

            <programlisting language="xml"><![CDATA[
                <!-- API -->
                <dependency>
                    <groupId>jakarta.xml.bind</groupId>
                    <artifactId>jakarta.xml.bind-api</artifactId>
                    <version>${jaxb.version}</version>
                </dependency>

                <!-- Runtime -->
                <dependency>
                    <groupId>org.glassfish.jaxb</groupId>
                    <artifactId>jaxb-runtime</artifactId>
                    <version>${jaxb.version}</version>
                </dependency>
    ]]></programlisting>
        </example></para>
    </section>


    <section xml:id="Jaxb_tooling">
        <title>Using JAXB tools for java sources and XML schema generation</title>

        <para>
            To generate JAXB classes from schema in Maven project, a community
            <link xlink:href="https://github.com/highsource/maven-jaxb2-plugin">maven-jaxb2-plugin</link> can be used.
            <example><title>Using maven-jaxb2-plugin</title>

                <programlisting language="xml"><![CDATA[
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.jvnet.jaxb2.maven2</groupId>
                            <artifactId>maven-jaxb2-plugin</artifactId>
                            <executions>
                                <execution>
                                    <id>generate</id>
                                    <goals>
                                        <goal>generate</goal>
                                    </goals>
                                </execution>
                            </executions>
                        </plugin>
                    </plugins>
                </build>
    ]]></programlisting>
            </example>
            See the <link xlink:href="https://github.com/highsource/maven-jaxb2-plugin">maven-jaxb2-plugin documentation</link> for configuration details.</para>

    <para>
        Alternatively to community plugins, there are tooling artifacts jaxb-xjc and jaxb-jxc,
        which can be used for
        java from XML schema generation and vice versa.
        <example><title>Using JAXB tooling artifacts</title>

            <programlisting language="xml"><![CDATA[
            <!-- Tooling dependencies -->
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-xjc</artifactId>
                <version>${jaxb.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jaxb</groupId>
                <artifactId>jaxb-jxc</artifactId>
                <version>${jaxb.version}</version>
            </dependency>

            <!-- Invoke tooling API (Java 11) -->
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                    <!-- Generate java sources from schema -->
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>java</executable>
                            <arguments>
                                <argument>--module-path</argument>
                                <modulepath/>
                                <argument>-m</argument>
                                <argument>com.sun.tools.xjc/com.sun.tools.xjc.XJCFacade</argument>
                                <argument>-p</argument>
                                <argument>com.example</argument>
                                <argument>-d</argument>
                                <argument>${project.build.directory}/generated-sources</argument>
                                <argument>${project.build.directory}/classes/schema.xsd</argument>
                            </arguments>
                        </configuration>
                    </execution>

                    <!-- Generate XML Schema from sources -->
                    <execution>
                        <id>jxc</id>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>java</executable>
                            <arguments>
                                <argument>--module-path</argument>
                                <modulepath/>
                                <argument>-m</argument>
                                <argument>com.sun.tools.jxc/com.sun.tools.jxc.SchemaGeneratorFacade</argument>
                                <argument>-d</argument>
                                <argument>${project.build.directory}/generated-sources</argument>
                                <argument>${project.build.directory}/classes/com/example/Author.java</argument>
                                <argument>${project.build.directory}/classes/com/example/Book.java</argument>
                            </arguments>
                            <longModulepath>false</longModulepath>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
]]></programlisting>
        </example>
    See also <link linkend='schema-generation-invoking-schemagen-programatically'>xml schema compiler usage</link>.</para>

    <para>Where are schemagen and xjc command line scripts available in JavaSE prior to 11?
        These are included only in the <link linkend="Maven_artifacts">zip distribution.</link></para>
    </section>

</section>