Maven jaxb2 plugin in Gradle

Farzad picture Farzad · May 9, 2016 · Viewed 6.9k times · Source

I need to migrate a maven project to gradle. The maven project uses the maven-jaxb2-plugin like this (version for the plugin is set in a root pom.xml):

<plugin>
  <groupId>...</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>generate</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <generatePackage>package for generated classes</generatePackage>
    <schemaDirectory>directory containing XSD files</schemaDirectory>
    <includeSchemas>
        <includeSchema>XSD file name</includeSchema>
        <includeSchema>XSD file name</includeSchema>
        ...
    </includeSchemas>
    <strict>true</strict>
    <verbose>true</verbose>
    <extension>true</extension>
  </configuration>
<plugin>

So, I wanted to achieve the same functionality in gradle, and this is what I have:

plugins {
  id "com.github.jacobono.jaxb" version "1.3.5"
}
dependencies {
  jaxb "org.glassfish.jaxb:jaxb-runtime:2.2.11"
  jaxb "org.glassfish.jaxb:jaxb-xjc:2.2.11"
}
jaxb {
  xsdDir = "directory containing XSD files"
  xjc {
    taskClassname = "com.sun.tools.xjc.XJC2Task"
    generatedPackage = "package for generated classes"
  }
}
compileJava.dependsOn xjc

This project is part of a multi-project build with dependencies on other projects etc., but I don't think those are relevant.

Am I on the right track?? I'm asking because the behavior doesn't seem to be the same when I do mvn clean install and gradle clean build

Question:

  • Is there a way to specify the XSD file names we want to use in gradle (as we do using includeSchema in maven)?

My problem:

Answer

user3011958 picture user3011958 · Oct 18, 2016

This is how I achieved this task after doing the research. This worked as expected. Add followings to your build file.

buildscript {

    dependencies {
        classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.5'
    }
}

apply plugin: 'com.github.jacobono.jaxb'

dependencies {

    jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.7-b41'
    jaxb 'com.sun.xml.bind:jaxb-impl:2.2.7-b41'
    jaxb 'javax.xml.bind:jaxb-api:2.2.7'
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-ant:0.6.5"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics:0.6.4"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-basics-annotate:0.6.4"
    jaxb "org.jvnet.jaxb2_commons:jaxb2-value-constructor:3.0"
}


jaxb {

    System.setProperty('javax.xml.accessExternalSchema', 'all') //To solve external schema dependencies
    xsdDir = "src/main/resources/schema/" //xsd directory
    xjc {
        taskClassname = "org.jvnet.jaxb2_commons.xjc.XJC2Task" // This is for setter plugin
        args = ["-Xsetters","-Xsetters-mode=direct"]
    }
}

you should run 'gradle xjc' to generate related java files from xsd files.