Skip to content

Instantly share code, notes, and snippets.

@hmarggraff
Created September 25, 2014 15:35
Show Gist options
  • Save hmarggraff/1bfdd0cea9f59922ac0b to your computer and use it in GitHub Desktop.
Save hmarggraff/1bfdd0cea9f59922ac0b to your computer and use it in GitHub Desktop.
mimimal code for a task for jaxb code generation with xjc from a gradle build.
apply plugin: 'java'
repositories { mavenCentral() }
// we generate into the build dir
def generatedSourcesDir = new File(buildDir, 'gensrc')
// create a configuration that holds the Xjc compiler
configurations { xjc }
task xjc() {
// this configures the task (runs always)
// this allows the task to check if it needs to regenerate files
outputs.dir generatedSourcesDir
// put xjc compiler into configuration
dependencies {
xjc 'com.sun.xml.bind:jaxb-xjc:2.2.4'
}
//add the generated sources to the sources to be compiled
sourceSets {
main.java.srcDir generatedSourcesDir
}
// define the ant task
ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xjc.asPath,)
// define actual steps to execute task (only runs when task is activated)
doLast {
println "Generating jaxb sources into $generatedSourcesDir"
generatedSourcesDir.mkdirs()
ant.xjc(destdir: generatedSourcesDir,
schema: 'xsd/my.xsd', // that is where the schema resides relative to the project dir
package: 'org.my.bla') // the package of the generated classes
}
}
// make sure source have been generated before compiler starts
compileJava.dependsOn xjc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment