HOw to use jsonschema2pojo in maven's POM
Asked Answered
E

3

5

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

Expugnable answered 26/5, 2016 at 13:29 Comment(0)
K
6

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.

Kape answered 30/5, 2016 at 13:45 Comment(4)
Thnks for the reply. I have removed the line" <targetPackage>${basedir}/src/main/resources/result</targetPackage>" but still the files are not getting generated in the target folder. Now my configuration file is <configuration> <sourceDirectory>${basedir}/src/main/resources/schema/jsonSample.json</sourceDirectory> </configuration> Note: Iam using generate-sources phase of maven to build. My target folder now contains only maven-status folder. Please suggest.Expugnable
How to get a jar file created from the generated pojo .class files using maven?Rubber
@Pradeep_Evol mvn packageKape
If you are generating code from JSON (not from JSON schema), use <sourceType>json</sourceType> in your jsonschema2pojo plugin configuration.Mole
F
5

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>
Faustofaustus answered 28/7, 2020 at 12:47 Comment(1)
yes , it works for me . i tried same script.Parthenia
C
2

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>
Cleat answered 12/12, 2020 at 23:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.