JSON Schema2Pojo Gradle - PullRequest
       50

JSON Schema2Pojo Gradle

0 голосов
/ 16 ноября 2018

Я пытаюсь преобразовать json в java-объекты, следуя этому примеру: https://github.com/joelittlejohn/jsonschema2pojo.

Однако возникают проблемы с обнаружением зависимости Schema2Pojo.

Код:

/**
 * Copyright © 2010-2014 Nokia
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jsonschema2pojo.gradle

import org.jsonschema2pojo.Jsonschema2Pojo
import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.tasks.TaskAction

/**
 * A task that performs code generation.
 *
 * @author Ben Manes (ben.manes@gmail.com)
 */
class GenerateJsonSchemaJavaTask extends DefaultTask {
  def configuration

  GenerateJsonSchemaJavaTask() {
    description = 'Generates Java classes from a json schema.'
    group = 'Build'

    project.afterEvaluate {
      configuration = project.jsonSchema2Pojo
      configuration.targetDirectory = configuration.targetDirectory ?:
        project.file("${project.buildDir}/generated-sources/js2p")

      if (project.plugins.hasPlugin('java')) {
        configureJava()
      } else {
        throw new GradleException('generateJsonSchema: Java plugin is required')
      }
      outputs.dir configuration.targetDirectory

      inputs.property("configuration", configuration.toString())
      inputs.files project.files(configuration.sourceFiles)
      outputs.files project.files(configuration.targetDirectory)
    }
  }

  def configureJava() {
    project.sourceSets.main.java.srcDirs += [ configuration.targetDirectory ]
    dependsOn(project.tasks.processResources)
    project.tasks.compileJava.dependsOn(this)

    if (!configuration.source.hasNext()) {
      configuration.source = project.files("${project.sourceSets.main.output.resourcesDir}/json")
      configuration.sourceFiles.each { it.mkdir() }
    }
  }

  @TaskAction
  def generate() {
    if (Boolean.TRUE.equals(configuration.properties.get("useCommonsLang3"))) {
      logger.warn 'useCommonsLang3 is deprecated. Please remove it from your config.'
    }

    logger.info 'Using this configuration:\n{}', configuration
    Jsonschema2Pojo.generate(configuration)
  }
}

Build.gradle (закомментированные строки - это строки, которые не работают):

apply plugin: 'java' apply plugin: 'jsonschema2pojo'

buildscript {
  repositories {
    mavenLocal()
    jcenter()
  }

  dependencies{
//    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:0.4.14'
//    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:0.5.1'
//    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:0.5.0-SNAPSHOT'
//    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:${js2p.version}'
//    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:0.4.23-SNAPSHOT'
//    see src/main/resources/json/external_dependencies.json

    classpath 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:latest.integration'
    classpath 'joda-time:joda-time:2.2'
  }
}

repositories {
  mavenCentral()
}

dependencies {
//  see src/main/resources/json/external_dependencies.json

  // https://mvnrepository.com/artifact/org.jsonschema2pojo/jsonschema2pojo-core
  compile group: 'org.jsonschema2pojo', name: 'jsonschema2pojo-core', version: '0.5.0'

  compile 'com.fasterxml.jackson.core:jackson-databind:2.9.7'
  compile 'javax.validation:validation-api:1.1.0.CR2'
  compile 'org.jsonschema2pojo:jsonschema2pojo-gradle-plugin:0.4.14'
  compile group: 'com.fasterxml.jackson.core', name: 'jackson-core', version: '2.2.2'
  compile 'joda-time:joda-time:2.2'
}

jsonSchema2Pojo {
  targetPackage = 'example'
  includeJsr303Annotations = true
  propertyWordDelimiters = ['_'] as char[]
}

EDIT: я временнорешил проблему, добавив банку вручную.Это не предпочтительное решение, хотя.

...