Posts Convert xsd to java using jaxb2-maven-plugin
Post
Cancel

Convert xsd to java using jaxb2-maven-plugin

Converting xsd to java is easy using maven “jaxb2-maven-plugin”.

This plugin usage example is also present on below maven site

jaxb2-maven-plugin usage

The below example is for generating java code using multiple xsd.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<build>
 <plugins>
   <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>jaxb2-maven-plugin</artifactId>
        <version>1.5</version>
        <executions>
            <execution>
                <id>jaxb-FirstMessage</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
                <configuration>
                    <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
                    <packageName>my.test.firstmessage</packageName>
                    <schemaFiles>FirstMessage.xsd</schemaFiles>
                    <outputDirectory>${project.build.directory}/generated-sources/firstMessage</outputDirectory>
                    <staleFile>${project.build.directory}/generated-sources/firstMessage-stale</staleFile>
                </configuration>
            </execution>
            <execution>
                <id>jaxb-SecondMessage</id>
                <goals>
                    <goal>xjc</goal>
                </goals>
                <configuration>
                    <schemaDirectory>${project.basedir}/src/main/resources/xsd</schemaDirectory>
                    <clearOutputDir>false</clearOutputDir>
                    <packageName>my.test.secondmessage</packageName>
                    <schemaFiles>SecondMessage.xsd</schemaFiles>
                    <outputDirectory>${project.build.directory}/generated-sources/secondMessage</outputDirectory>
                    <staleFile>${project.build.directory}/generated-sources/secondMessage-stale</staleFile>
                </configuration>
            </execution>
    </plugin>
 <plugins>
<build> 

Here as explained in the maven site

Each execution needs its own stale file, otherwise the plugin will think there is nothing to generate in the executions after the fist one.

Need to set clearOutputDir to false for all but the first execution, otherwise the Java sources generated in prior steps will be cleared (deleted).

You can get this plugin latest version at maven central using the below link

jaxb2-maven-plugin

This post is licensed under CC BY 4.0 by the author.