Skip to content
Pedro Xavier Leite Cavadas edited this page Sep 5, 2024 · 3 revisions

The js2pets project provides extended functionality for customizing the conversion of JSON schemas into Java POJOs. Below is a guide on how to configure and effectively use js2pets in your Maven or Gradle projects.

Configuring js2pets in Maven

To use js2pets with Maven, you will need to include the plugin in your pom.xml. Below is an example of how to set it up:

<plugin>
  <groupId>org.jsonschema2pojo</groupId>
  <artifactId>jsonschema2pojo-maven-plugin</artifactId>
  <version>${jsonschema2pojo.version}</version>
  <executions>
    <execution>
      <id>generate-pojos</id>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <dependencies>
    <dependency>
      <groupId>io.github.lengors</groupId>
      <artifactId>js2pets</artifactId>
      <version>${js2pets.version}</version>
    </dependency>
  </dependencies>
</plugin>

Configuring js2pets in Gradle

To use js2pets with Gradle, include the plugin in your build.gradle.kts file:

plugins {
    id("org.jsonschema2pojo") version jsonschema2pojoVersion
}

buildscript {
    repositories {
        mavenLocal()
    }

    dependencies {
        classpath("io.github.lengors:js2pets:$js2petsVersion")
    }
}

Custom Rule Configurations

js2pets allows you to set a custom rules for skipping generating no-args constructors for the generated POJOs. Here's an example of how to control constructor generation:

Maven

<configuration>
  <includeConstructors>true</includeConstructors>
  <includeNoArgsConstructor>false</includeNoArgsConstructor>
  <customRuleFactory>io.github.lengors.js2pets.factories.EnhancedRuleFactory</customRuleFactory>
</configuration>

Gradle

import io.github.lengors.js2pets.factories.EnhancedRuleFactory

jsonSchema2Pojo {
    setCustomRuleFactory(EnhancedRuleFactory.ExcludeNoArgsConstructor::class.java)
    includeConstructors = true
}

In this example:

  • includeConstructors: Enables generation of constructors for the POJOs.
  • includeNoArgsConstructor: Disables generation of no-args constructor.
  • customRuleFactory: Setting it to io.github.lengors.js2pets.factories.EnhancedRuleFactory automatically uses the appropriate constructor rule that will skip generation of no-args constructor based on the jsonschema2pojo's configuration.

For Gradle you need to specify one of the two IncludeNoArgsConstructor or ExcludeNoArgsConstructor as io.github.lengors.js2pets.factories.EnhancedRuleFactory doesn't currently support inferring how to behave for Gradle based projects.

For more detailed configuration options, refer to the jsonschema2pojo documentation and tailor the options to suit your project's requirements.