How to compile multiple xsd files in Gradle JAXB/XJC?

MFIhsan picture MFIhsan · May 12, 2014 · Viewed 18.5k times · Source

I'm fairly new to Gradle and I am facing some issues trying to compile multiple schema files using Gradle Ant XJC.

Using the below code, I'm able to compile 1 schema successfully. However, I'm not sure how to do the same for multiple schema files.

Any advice please?

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task : XJC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final def packageName = "org.test.webservice.domain"
final def schemaFile = "misc/resources/schemas/employee-v3/wadl/employee.xsd"

configurations { provided }
project.ext.generatedSrcDir = file("$buildDir/generated-src")
dependencies {
    provided 'com.sun.xml.bind:jaxb-impl:2.2.6'
    provided 'com.sun.xml.bind:jaxb-xjc:2.2.6'
}
task jaxb {
    println 'Starting JAXB XJC...'
    ext {
        packagePath = packageName.replaceAll("\\.", "/")
        srcFile = file(schemaFile)
        destDir = new File(project.ext.generatedSrcDir, packagePath)
    }

    inputs.file srcFile
    outputs.dir destDir
    project.ext.generatedSrcDir.mkdirs()
    ant.taskdef(name: 'xjc',
            classname: 'com.sun.tools.xjc.XJCTask',
            classpath: configurations.provided.asPath)

    doLast {
        project.ext.generatedSrcDir.mkdirs()
        ant.xjc(schema: srcFile, package: packageName,
                destdir: project.ext.generatedSrcDir)
    }

    task generateSources() {}
    sourceSets.main.java.srcDirs += project.ext.generatedSrcDir
    generateSources.dependsOn jaxb
    compileJava.dependsOn generateSources
    eclipseClasspath.dependsOn generateSources
//    ideaClasspath.dependsOn generateSources
}

Answer

MFIhsan picture MFIhsan · May 13, 2014

I solved it myself. Here is the working solution.

//  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Task : XJC ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
final def packageName = "org.test.webservice.domain"
final def schemaDir = "misc/resources/schemas/employee-sda-v3/wadl"

configurations { provided }
project.ext.generatedSrcDir = file("$buildDir/generated-src")
dependencies {
    provided 'com.sun.xml.bind:jaxb-impl:2.2.6'
    provided 'com.sun.xml.bind:jaxb-xjc:2.2.6'
}
task jaxb {
    println 'Starting JAXB XJC...'
    ext {
        packagePath = packageName.replaceAll("\\.", "/")
        destDir = new File(project.ext.generatedSrcDir, packagePath)
    }

    outputs.dir destDir
    project.ext.generatedSrcDir.mkdirs()
    ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.provided.asPath)

    doLast {
        project.ext.generatedSrcDir.mkdirs()
        ant.xjc(package: packageName, destdir: project.ext.generatedSrcDir){
            schema(dir: ${schemaDir}, includes: "**/*.xsd")
        }
    }

    task generateSources() {}
    sourceSets.main.java.srcDirs += project.ext.generatedSrcDir
    generateSources.dependsOn jaxb
    compileJava.dependsOn generateSources
    eclipseClasspath.dependsOn generateSources
//    ideaClasspath.dependsOn generateSources
}