Warm tip: This article is reproduced from stackoverflow.com, please click
java jaxb xml xsd xjc

jaxb

发布于 2020-04-23 10:13:37

I need some help, I'm trying to unmarshall XML for my application. So I have an XSD file, and I want to generate Java classes from it. But I don't want to use XJC command (because it's not in my JDK and also I want it to be done automatically every time I run my code).

Does someone have another option with JAXB without using xjc please?

I'm working with Java (jdk : 12 & jre : 8) and IntelliJ community.

update : the indicated plugin in pom file wasn't working "couldn't find artifact.."

Questioner
ElisaGab
Viewed
40
Daniil 2020-02-06 19:28

It really depends on how your project is organized / build. If you want to generate java classes to be built on base of XSD files each time when project is build (which makes sense, because XSD files could be changed due to new requirements or new service version), you probably should use build manager (maven or gradle) and an appropriate maven / gradle plugin, which will be invoked during project build.

For this purpose maven-jaxb2-plugin could be used, which uses XJC under the hood as well, but when using maven or gradle you shouldn't worry about having or not having the XJC in your system - it will be downloaded by project build manager as well as all other dependencies defined.

Updated:

Here is an example of plugin configuration:

<build>

    <plugins>

        <!-- Generate JAXB classes -->
        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <executions>
                <!-- Handle XML schema -->
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaIncludes>
                            <include>PATH_TO_XSD_FILE</include>
                        </schemaIncludes>
                        <generatePackage>NAME_OF_JAVA_PACKAGE</generatePackage>
                    </configuration>
                    <id>jaxb-generate-classes</id>
                </execution>
            </executions>
            <configuration>
                <forceRegenerate>true</forceRegenerate>
                <generateDirectory>${project.build.directory}/generated-sources/xjc</generateDirectory>
                <verbose>true</verbose>
                <debug>true</debug>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics-annotate</artifactId>
                    <version>1.0.2</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.codemodel</groupId>
                    <artifactId>codemodel</artifactId>
                    <version>2.6</version>
                </dependency>
            </dependencies>
        </plugin>

    </plugins>
</build>