Search code examples
javajaxbmaven-pluginjibxxml-binding

Making data model classes serializable


I am using JiBX for XML-Java data binding. The current configuration generates classes pretty well but I want these generated classes to implement java.io.Serializable.

Here is maven plugin configuration to generate java classes from given schema.

<plugin>
    <groupId>org.jibx</groupId>
    <artifactId>jibx-maven-plugin</artifactId>
    <version>1.2.3</version>
    <configuration>
        <schemaLocation>src/main/resources</schemaLocation>            
        <includeSchemas>
            <includeSchema>FS_OTA_VehResRS.xsd</includeSchema>
        </includeSchemas>
        <options>
            <package>com.test.cars.model.ota2009a.vehresrs</package>
        </options>
        <schemaBindingDirectory>src/main/java</schemaBindingDirectory>
        <includeSchemaBindings>
            <includeSchemaBinding>*_binding.xml</includeSchemaBinding>
        </includeSchemaBindings>
    </configuration>
    <executions>            
        <execution>
        <id>generate-java-code-from-schema</id>
        <goals>
            <goal>schema-codegen</goal>
        </goals>
        </execution>
        <execution>
        <id>compile-the-binding-</id>
        <goals>
            <goal>bind</goal>
        </goals>
        </execution>            
    </executions>
</plugin>

This link suggesting to use org.jibx.schema.codegen.extend.SerializableDecorator in order implement java.io.Serializable to all generated classes. But I don't have idea how to write customization file and configure jibx-maven-plugin.

Can anyone please guide me to achieve this?


Solution

  • I am able to get it.

    I created src/main/resources/schema-customizations.xml. The content of this custom config file is:

    <schema-set xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <class-decorator class="org.jibx.schema.codegen.extend.SerializableDecorator"/>
    </schema-set>
    

    Also modified pom.xml to add in customization configuration under <configuration>

    <customizations>
        <customization>src/main/resources/schema-customizations.xml</customization>
    </customizations>
    

    and run mvn jibx:schema-codegen

    Now all generated classes are implementing java.io.Serializable

    Thanks @SB