Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
}
like image 415
MFIhsan Avatar asked May 12 '14 21:05

MFIhsan


2 Answers

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
}
like image 121
MFIhsan Avatar answered Sep 22 '22 09:09

MFIhsan


jacobono's gradle-jaxb-plugin compiles all schemas in the input directory. I find this plugin more stable than Ant XJC. In my project, on some machines Ant XJC does not generate classes for no reason, although the results is successful. Maybe this is matter of configuration but IMHO even setup is easier with the plugin.

like image 26
Bartek Avatar answered Sep 21 '22 09:09

Bartek