Rajit s rajan
Rajit s rajan

Reputation: 595

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

Upvotes: 5

Views: 33062

Answers (3)

pat
pat

Reputation: 21

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>

Upvotes: 2

Vijay Kesanupalli
Vijay Kesanupalli

Reputation: 165

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>

Upvotes: 5

joelittlejohn
joelittlejohn

Reputation: 11832

You want to use <outputDirectory> instead of <targetPackage>. More details here:

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.

Upvotes: 6

Related Questions