Git Product home page Git Product logo

gradle-jsonschema2pojo-plugin's Introduction

Gradle jsonschema2pojo plugin (deprecated)

jsonschema2pojo 0.3.8 includes an official plugin based on this one.

jsonschema2pojo generates a Java representation of your json schema. The schema reference describes the rules and their effect on generated Java types.

Usage

This plugin is hosted on the Maven Central Repository. All actions are logged at the info level.

apply plugin: 'jsonschema2pojo'

buildscript {
  repositories {
    mavenCentral()
  }

  dependencies {
    // this plugin
    classpath 'com.github.ben-manes:gradle-jsonschema2pojo-plugin:0.1'
    // add additional dependencies here if you wish to reference instead of generate them (see example directory)
  }
}

repositories {
  mavenCentral()
}

dependencies {
  // Required if generating equals, hashCode, or toString methods
  compile 'commons-lang:commons-lang:2.6'
  // Required if generating JSR-303 annotations
  compile 'javax.validation:validation-api:1.1.0.CR2'
  // Required if generating Jackson 2 annotations
  compile 'com.fasterxml.jackson.core:jackson-databind:2.1.4'
}

// Each configuration is set to the default value
jsonSchema2Pojo {
  // Whether to generate builder-style methods of the form withXxx(value) (that return this),
  // alongside the standard, void-return setters.
  generateBuilders = false

  // Whether to use primitives (long, double, boolean) instead of wrapper types where possible
  // when generating bean properties (has the side-effect of making those properties non-null).
  usePrimitives = false

  // Location of the JSON Schema file(s). This may refer to a single file or a directory of files.
  source = files("${sourceSets.main.output.resourcesDir}/json")

  // Target directory for generated Java source files. The plugin will add this directory to the
  // java source set so the compiler will find and compile the newly generated source files.
  targetDirectory = file("${project.buildDir}/generated-sources/js2p")

  // Package name used for generated Java classes (for types where a fully qualified name has not
  // been supplied in the schema using the 'javaType' property).
  targetPackage = ''

  // The characters that should be considered as word delimiters when creating Java Bean property
  // names from JSON property names. If blank or not set, JSON properties will be considered to
  // contain a single word when creating Java Bean property names.
  propertyWordDelimiters = [] as char[]

  // Whether to use the java type long (or Long) instead of int (or Integer) when representing the
  // JSON Schema type 'integer'.
  useLongIntegers = false

  // Whether to include hashCode and equals methods in generated Java types.
  includeHashcodeAndEquals = true

  // Whether to include a toString method in generated Java types.
  includeToString = true

  // The style of annotations to use in the generated Java types. Supported values:
  //  - jackson (alias of jackson2)
  //  - jackson2 (apply annotations from the Jackson 2.x library)
  //  - jackson1 (apply annotations from the Jackson 1.x library)
  //  - none (apply no annotations at all)
  annotationStyle = 'jackson'

  // A fully qualified class name, referring to a custom annotator class that implements
  // com.googlecode.jsonschema2pojo.Annotator and will be used in addition to the one chosen
  // by annotationStyle. If you want to use the custom annotator alone, set annotationStyle to none.
  customAnnotator = 'com.googlecode.jsonschema2pojo.NoopAnnotator'

  // Whether to include JSR-303 annotations (for schema rules like minimum, maximum, etc) in
  // generated Java types. Schema rules and the annotation they produce:
  //  - maximum = @DecimalMax
  //  - minimum = @DecimalMin
  //  - minItems,maxItems = @Size
  //  - minLength,maxLength = @Size
  //  - pattern = @Pattern
  //  - required = @NotNull
  // Any Java fields which are an object or array of objects will be annotated with @Valid to
  // support validation of an entire document tree.
  includeJsr303Annotations = false

  // The type of input documents that will be read. Supported values:
  //  - jsonschema (schema documents, containing formal rules that describe the structure of json data)
  //  - json (documents that represent an example of the kind of json data that the generated Java types
  //          will be mapped to)
  sourceType = 'jsonschema'
}

Working with pre-existing java classes

jsonschema2pojo allows to reference any pre-existing java classes. In general, if the generator finds a class already exists on the classpath, then it will not be generated but only referenced. To make this work as expected with this gradle plugin, the dependencies in question must be added to the buildscript classpath, the project classpath alone will not suffice. For a little example of how to do this have a look at the example directory.

Tasks

generateJsonSchema2Pojo

This task will automatically run in a project where the jsonSchema2Pojo configuration closure is present. It will invoke the jsonschema2pojo generator, make the compileJava task dependent of itself and add the targetDirectory to the main/java source set so the java compiler will find and compile the newly generated source files.

gradle-jsonschema2pojo-plugin's People

Contributors

ben-manes avatar joelittlejohn avatar stackmagic avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

dashao598

gradle-jsonschema2pojo-plugin's Issues

Documentation Update

Hi

Just a thought. I think it would make sense to extend your readme so it mentions how to make the compile task dependent on the js2p plugin. It took me a while to figure this out because the actual task is named differently than the js2p closure:

compileJava.dependsOn generateJsonSchema2Pojo

I'd guess most people will want this dependency anyway.

Patrick

Referencing pre-existing classes

Hi Ben

I had a little issue last week when trying to use joda-time instead of the jvm time classes. The generator would generate new classes instead of re-using the existing ones. The joda-time lib was on the project classpath and the generator would only not generate the extra classes when the library is on the buildscript-classpath too.

Here's the discussion I had with the jsonschema2pojo maintainer: https://code.google.com/p/jsonschema2pojo/issues/detail?id=110

Could you change the plugin so it adds the project classpath to the plugin/task classpath before invoking the generator? Then the behaviour would be consistent with the plugins' maven/ant counterparts and work more intuitively IMO.

Thank you
Patrick

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.