Skip to content

Instantly share code, notes, and snippets.

@marcellodesales
Created January 23, 2015 08:31
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcellodesales/dbb9baf0c0214a4b84e8 to your computer and use it in GitHub Desktop.
Save marcellodesales/dbb9baf0c0214a4b84e8 to your computer and use it in GitHub Desktop.
Java POJO Code Generation from XSD using Gradle 2.1 (See gradle xjc screen below)
buildscript {
repositories {
maven { url "https://repo.spring.io/libs-release" }
mavenLocal()
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.10.RELEASE")
//####################### XJC - JDK 1.7/1.8 ####################
classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.5'
//######################
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
//####################### XJC - JDK 1.7/1.8 ####################
apply plugin: 'com.github.jacobono.jaxb'
//######################
jar {
baseName = 'gs-rest-service'
version = '0.1.0'
}
repositories {
mavenLocal()
mavenCentral()
maven { url "https://repo.spring.io/libs-release" }
}
dependencies {
//####################### XJC - JDK 1.7/1.8 ####################
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'
//######################
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("junit:junit")
}
//####################### XJC - JDK 1.7/1.8 ####################
jaxb {
xjc {
xsdDir = "src/main/resources/schema"
generatePackage = "com.intuit.platform.service.formsavailability.domain"
}
}
//######################
task wrapper(type: Wrapper) {
gradleVersion = '1.11'
}
$ git status .
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityForm.java
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityFormset.java
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityPlatform.java
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityProduct.java
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/FormsAvailability.java
modified: src/main/java/com/intuit/platform/service/formsavailability/domain/ObjectFactory.java
--- a/src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityForm.java
+++ b/src/main/java/com/intuit/platform/service/formsavailability/domain/AvailabilityForm.java
@@ -1,8 +1,8 @@
//
-// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
+// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.7-b41
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2015.01.22 at 11:20:47 PM PST
+// Generated on: 2015.01.23 at 12:12:55 AM PST
//
mdesales@ubuntu [01/23/2015 0:12:22] ~/dev/github-intuit/mdesales/service-forms-availability (master *) $ gradle xjc
:xsd-dependency-tree
jaxb: starting Namespace Task
There is no targetNamespace attribute for file '/home/mdesales/dev/github-intuit/mdesales/service-forms-availability/src/main/resources/schema/formsAvailability.xsd' (assigning filname as namespace to it). A Schema should ALWAYS include a targetNamespace attribute at its root element. No targetNamespace are categorized as using the Chameleon Design Pattern, which is not an advisable pattern, AT ALL!
jaxb: generating xsd namespace dependency tree
:xjc
Download https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-xjc/2.2.7-b41/jaxb-xjc-2.2.7-b41.pom
Download https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-impl/2.2.7-b41/jaxb-impl-2.2.7-b41.pom
Download https://repo1.maven.org/maven2/javax/xml/bind/jaxb-api/2.2.7/jaxb-api-2.2.7.pom
Download https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-xjc/2.2.7-b41/jaxb-xjc-2.2.7-b41.jar
Download https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-impl/2.2.7-b41/jaxb-impl-2.2.7-b41.jar
Download https://repo1.maven.org/maven2/javax/xml/bind/jaxb-api/2.2.7/jaxb-api-2.2.7.jar
BUILD SUCCESSFUL
Total time: 13.758 secs
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xs:element name="formsavail" type="FormsAvailability" />
<xs:complexType name="FormsAvailability">
<xs:sequence>
<xs:element name="platform" type="AvailabilityPlatform" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="timestamp" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="AvailabilityPlatform">
<xs:sequence>
<xs:element name="product" type="AvailabilityProduct" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="type" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="AvailabilityProduct">
<xs:sequence>
<xs:element name="formset" maxOccurs="unbounded" type="AvailabilityFormset" />
</xs:sequence>
<xs:attribute name="type" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="AvailabilityFormset">
<xs:sequence>
<xs:element name="form" type="AvailabilityForm" maxOccurs="unbounded" />
</xs:sequence>
<xs:attribute name="formsetid" use="required" type="xs:string" />
<xs:attribute name="prod" use="required" type="xs:string" />
</xs:complexType>
<xs:complexType name="AvailabilityForm">
<xs:attribute name="formid" use="required" type="xs:string" />
<xs:attribute name="id" use="required" type="xs:integer" />
</xs:complexType>
</xs:schema>
@marcellodesales
Copy link
Author

Full Instructions

  1. Create the "schema.xsd" file with the schema you need.
  2. Create the "build.gradle" with the settings from https://github.com/jacobono/gradle-jaxb-plugin
  3. Make sure that you have the dependencies for buildscript on top and then the regular dependencies
  4. Declare the plugin
  5. Declare the dependencies to the plugin
  6. Declare the Jaxb->xjc with the settings defined at https://github.com/jacobono/gradle-jaxb-plugin#xjc-convention
  7. Similar to running the command manually, this will be ready to run. See the output of "gradle xjc" for the output.
  8. If you want to add in version control, you can just verify the changes and see that at least the date of the files were changed using "git status".

You can customize Gradle to generate the source before compiling the classes.

😄

@shavo007
Copy link

Nice instructions. I changed the classes to reside in generated dir. Then add that to the main src set. Keep the main src set clean

jaxb {
xjc {
xsdDir = "src/main/resources/schema"
destinationDir = "src/generated/java"
generatePackage = "com.shane.alycon.raidr.xml.undertakr"
}
}

sourceSets {
main {
java {
srcDir 'src/generated/java'
}
}
}

@csc-bnguyen43
Copy link

Thanks shavo007, nice tips

@abhishekprasad870
Copy link

Hi shavo007 this code is not working for me...it is not generating java class

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment