I have a JSON File and I want to convert it to POJO, for this I am using the plugin of org.jsonschema2pojo in maven. I am not able to generate the resultant pojo.Here's the snippet from pom.xml
<build>
<plugins>
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>0.4.23</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<targetPackage>${basedir}/src/main/resources/result</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
I am using the generate sources goal in maven. My expectation is that it should give me pojo files at ${basedir}/src/main/resources/result location. However I am getting so. Please help me out. Thanks, Rajit
You want to use <outputDirectory>
instead of <targetPackage>
. More details here:
http://joelittlejohn.github.io/jsonschema2pojo/site/0.4.23/generate-mojo.html#outputDirectory
http://joelittlejohn.github.io/jsonschema2pojo/site/0.4.23/generate-mojo.html#targetPackage
Target package is the Java package you want your types to use, e.g. com.youcompany.model
.
Also, typically you want the generated output to go into the target
directory, not src
. Derived files usually go there since anything inside target
is usually omitted from source control. You don't need to specify outputDirectory
if you don't want to, by default the generated output will go into /target/java-gen
.
Below code works for me.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<executions>
<execution>
<id>1</id>
<configuration>
<annotationStyle>jackson2</annotationStyle>
<includeAdditionalProperties>false</includeAdditionalProperties>
<sourcePaths>
<sourcePath>${project.basedir}/src/main/resource/jsd/your_schema.json</sourcePath>
</sourcePaths>
<targetPackage>your target package</targetPackage>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
<execution>
<id>2</id>
<configuration>
<annotationStyle>jackson2</annotationStyle>
<includeAdditionalProperties>false</includeAdditionalProperties>
<sourcePaths>
<sourcePath>${project.basedir}/src/main/resource/jsd/your_schema2.json</sourcePath>
</sourcePaths>
<targetPackage>your target package</targetPackage>
</configuration>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
Use both targetPackage and outputDirectory.
<plugin>
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
<outputDirectory>src/main/java</outputDirectory>
<targetPackage>com.your.package</targetPackage>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With