JSON Schema generation and package to JAR using Gradle?

I basically want to create a Library project in which I want to generate POJO’s as part of the build and package it as a JAR.

So basically, the set of steps should be something like :

  1. generate POJO using jsonschema2pojo Java class
  2. use gradle task to execute step 1
  3. use gradle task to package it to the JAR

All of this if can be done via a single build, it would be great. The issue which I am facing currently is :

I manually have to run a gradle task to generate pojo’s and then another gradle task to package it as a JAR. How can I combine it?

This should help get you going:

task genSources {
    def outDir = new File( buildDir, 'mygensources' )
    outputs.dir( outDir )

    doLast {
        println "generating source into ${outDir}"
        new File( outDir, 'Generated.java' ).withWriter('UTF-8') {
            it.write '''
                public class Generated {
                }
            '''
        }
    }
}

sourceSets.main.java.srcDir( tasks.genSources )

Results:

$ ./gradlew compileJava
> Task :genSources
generating source into /home/cdore/projects/gen/build/mygensources
> Task :compileJava


$ ll build/mygensources/ build/classes/java/main/
build/classes/java/main/:
total 4
-rw-rw-r-- 1 cdore cdore 252 Apr  1 17:24 Generated.class

build/mygensources/:
total 4
-rw-rw-r-- 1 cdore cdore 72 Apr  1 17:24 Generated.java