如何在 maven 的 POM 中使用 jsonschema2pojo

Raj*_*jan 5 java pojo maven-plugin

我有一个 JSON 文件,我想将它转换为 POJO,为此我在 maven 中使用 org.jsonschema2pojo 插件。我无法生成结果 pojo。这是 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>
Run Code Online (Sandbox Code Playgroud)

我在 maven 中使用 generate source 目标。我的期望是它应该在 ${basedir}/src/main/resources/result 位置给我 pojo 文件。但是我越来越了。请帮帮我。谢谢,拉吉特

joe*_*ohn 5

您想使用<outputDirectory>而不是<targetPackage>. 更多细节在这里:

目标包是您希望类型使用的 Java 包,例如com.youcompany.model.

此外,通常您希望生成的输出进入target目录,而不是src. 派生文件通常放在那里,因为target源代码控制中通常会省略其中的任何内容。outputDirectory如果您不想,则无需指定,默认情况下生成的输出将进入/target/java-gen.


小智 5

下面的代码对我有用。

<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>
Run Code Online (Sandbox Code Playgroud)