Showing posts with label Maven. Show all posts
Showing posts with label Maven. Show all posts

Saturday 10 July 2021

Generating the Pojo Classes automatically

There are scenarios, where we need to add the new attributes often, then the creation of POJO automatically will be helpful to you. You No need to make a lot of work, just do some changes it will create it. 

As far as I know, there are two ways we can generate these Pojo Classes.  

  • Using org.jvnet.jaxb2.maven2 
  • Using org.codehaus.mojo 

1. Generating through the org.jvnet.jaxb2.maven2 

The org.jvnet.jaxb2.maven2:maven-jaxb2-plugin, the most advanced and feature-full Maven plugin for XML Schema compilation. 


This Maven plugin wraps and enhances the JAXB Schema Compiler (XJC) and allows compiling XML Schemas (as well as WSDL, DTDs, RELAX NG) into Java classes in Maven builds. 


In order to use this, we need to have the XSD with us, through which we can generate the POJO classes. 


1.Create a directory in src/main/resources/XSD and copy the XSD file there. 

2.Add the Dependency there as like below. 

<plugin>

   <groupId>org.jvnet.jaxb2.maven2</groupId>

   <artifactId>maven-jaxb2-plugin</artifactId>

   <version>0.12.1</version>

   <executions>

      <execution>

         <id>generate</id>

         <goals>

            <goal>generate</goal>

         </goals>

      </execution>

   </executions>

   <configuration>

      <generatePackage>com.searchendeca.main.pojo</generatePackage>

      <generateDirectory>${project.basedir}/src/main/java</generateDirectory>

      <schemaDirectory>src/main/resources/xsd</schemaDirectory>

      <schemaIncludes>*.xsd</schemaIncludes>

   </configuration>

</plugin>

Here we need to specify where the files will be generated and the package it needs to generate then do alt+F5 which is Maven Refresh congratulations your POJO classes are generated automatically. 


2. Generating through the org.codehaus.mojo 

This plugin runs the XJC binding compiler from the JAXB distribution and integrates XJC’s configuration properties into a Maven project. 

Add the following dependency. 

<plugin>

   <groupId>org.codehaus.mojo</groupId>

   <artifactId>jaxb2-maven-plugin</artifactId>

   <version>2.4</version>

   <executions>

      <execution>

         <id>xjc</id>

         <goals>

            <goal>xjc</goal>

         </goals>

      </execution>

   </executions>

   <configuration>

      <sources>

         <source>src/main/resources/xsd/sample_CustomersOrders.xsd</source>

         <source>src/main/resources/xsd</source>

      </sources>

      <outputDirectory>src/main/java</outputDirectory>

      <!-- The package of your generated sources -->

      <packageName>com.searchendeca.main.pojo</packageName>

      <clearOutputDir>true</clearOutputDir>

      <addGeneratedAnnotation>false</addGeneratedAnnotation>

   </configuration>

</plugin>

 

If you are not specifying the output directory it will generate in the target folder. 

From the above two methods, I don’t see any difference apart from the groupId, it is to us which we can adopt for our project.  There are some tags different for specifying the output directory and package files etc. Again it's your call to adopt anyone, the second approach I see a lot of information on the internet.

Find the whole here.  

Happy Learning!!!!