0

I'm having a maven project with some json schemas organzied into folders like this:

src/main/resources/schema/json/modules/moduleA/typesA.json src/main/resources/schema/json/modules/moduleA/typeA.json

So both schemas are located in the same folder.

The typesA schema links to the typeA schema like this:

 "$ref": "resource:/schema/json/modules/moduleA/typeA.json"

Because I'd like to generated Java POJOS for all my schemas I'm using the jsonschema2pojo-maven-plugin like this:

    <!-- Code generation out of JsonSchema files -->
<plugin>
    <groupId>org.jsonschema2pojo</groupId>
    <artifactId>jsonschema2pojo-maven-plugin</artifactId>
    <version>0.5.1</version>
    <configuration>
        <skip>false</skip>
        <sourceDirectory>${basedir}/src/main/resources/schema/json/modules/</sourceDirectory>
        <outputDirectory>${basedir}/src/gen/java/json</outputDirectory>
        <removeOldOutput>true</removeOldOutput>
        <annotationStyle>none</annotationStyle>
        <includeGetters>true</includeGetters>
        <includeSetters>true</includeSetters>
        <useCommonsLang3>true</useCommonsLang3>
        <useLongIntegers>true</useLongIntegers>
        <includeJsr303Annotations>true</includeJsr303Annotations>
        <includeAdditionalProperties>false</includeAdditionalProperties>
        <includeHashcodeAndEquals>false</includeHashcodeAndEquals>
        <dateTimeType>java.time.LocalDateTime</dateTimeType>
        <targetPackage>com.all.my.modules</targetPackage>
    </configuration>
    <executions>
       <execution>
            <phase>generate-sources</phase>
            <goals>
               <goal>generate</goal>
            </goals>
       </execution>
    </executions>
</plugin>

But when I run "mvn clean compile" it always fails with the following stacktrace:

Caused by: java.lang.IllegalArgumentException: Couldn't read content from the classpath, file not found: resource:/schema/json/modules/moduleA/typeA.json at org.jsonschema2pojo.ContentResolver.resolveFromClasspath(ContentResolver.java:94) at org.jsonschema2pojo.ContentResolver.resolve(ContentResolver.java:73) at org.jsonschema2pojo.SchemaStore.create(SchemaStore.java:58) at org.jsonschema2pojo.SchemaStore.create(SchemaStore.java:128) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:59) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:30) at org.jsonschema2pojo.rules.ArrayRule.apply(ArrayRule.java:81) at org.jsonschema2pojo.rules.ArrayRule.apply(ArrayRule.java:37) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:113) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:40) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:73) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:30) at org.jsonschema2pojo.rules.PropertyRule.apply(PropertyRule.java:75) at org.jsonschema2pojo.rules.PropertyRule.apply(PropertyRule.java:43) at org.jsonschema2pojo.rules.PropertiesRule.apply(PropertiesRule.java:70) at org.jsonschema2pojo.rules.PropertiesRule.apply(PropertiesRule.java:38) at org.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:119) at org.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:70) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:90) at org.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:40) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:73) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:66) at org.jsonschema2pojo.rules.SchemaRule.apply(SchemaRule.java:30) at org.jsonschema2pojo.SchemaMapper.generate(SchemaMapper.java:92) at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:134) at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:136) at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:136) at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:136) at org.jsonschema2pojo.Jsonschema2Pojo.generateRecursive(Jsonschema2Pojo.java:136) at org.jsonschema2pojo.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:75) at org.jsonschema2pojo.maven.Jsonschema2PojoMojo.execute(Jsonschema2PojoMojo.java:788) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132) ... 20 more

But according to classpathRefs.json and jsonschema2pojo#ref it should be possible. When I try to debug the plugin execution by running mvnDebug sometimes it works and sometimes not. So is this a known problem ? Using only

"$ref": "typeA.json"

it works always. But this is only a temporary workaround for me. Java 8 + maven 3.2.5

merlin-hst
  • 61
  • 1
  • 4

1 Answers1

1

By default jsonschema2pojo binds to the generate-sources phase. The resources of the current module won't be available on the classpath at this time.

You should move the execution of this plugin into a later phase. As long as the plugin is executed before the compile phase then your build will still work correctly. If you bind the plugin to process-resources then the resources of the current module will be available on the classpath for jsonschema2pojo to use.

See http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html

joelittlejohn
  • 11,665
  • 2
  • 41
  • 54