Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOw to use jsonschema2pojo in maven's POM

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

like image 456
Rajit s rajan Avatar asked May 26 '16 13:05

Rajit s rajan


3 Answers

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.

like image 178
joelittlejohn Avatar answered Nov 15 '22 19:11

joelittlejohn


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>
like image 36
Vijay Kesanupalli Avatar answered Nov 15 '22 21:11

Vijay Kesanupalli


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>
like image 1
pat Avatar answered Nov 15 '22 19:11

pat