19 Ağustos 2009 Çarşamba

converting java classes to schema: schemagen

schemagen tool comes with jaxb reference implementation and can be used to generate schemas from java classes. here is the command line version http://java.sun.com/webservices/docs/2.0/jaxb/schemagen.html and ant version https://jaxb.dev.java.net/jaxb20-ea/docs/schemagenTask.html When command line version is used, the names of the generated schema files are given automatically as schema1.xsd, schema2.xsd,.. etc. (one schema file per namespace). However, controlling names of files is possible when ant task is used, so i prefer using ant task.
Here is my sample schemagen ant file:

<?xml version="1.0" encoding="UTF-8"?>
<project name="projectname" default="generate-schema" basedir=".">

<path id="classpath">
<pathelement location="path/to/classes"/>
<fileset dir="/path/to/jaxb/lib">
<include name="**/*.jar"/>
</fileset>
</path>

<taskdef name="schemagen" classname="com.sun.tools.jxc.SchemaGenTask">
<classpath refid="classpath"/>
</taskdef>

<target name="generate-schema">
<schemagen srcdir="path/to/src" destdir="path/to/generatedfiles">
<classpath refid="classpath"/>
<schema namespace="http://namespace" file="nameOfMySchema.xsd" />
</schemagen>
</target>

</project>

However, if java classes contains jpa specific annotations, the apt tool could not handle this and throw annotation specific exceptions when the lib coming with jaxb ri is used in the classpath.
Annotations for Persistence part on http://www.devx.com/Java/Article/34069/0/page/1 solves this. download sample code on http://assets.devx.com/sourcecode/18778.zip and use the lib coming with the sample on path 18778/JAXB/lib. Actually, the above ant file is a simplified version of 18778/JAXB/3-JAXB and JPA/build.xml coming with the example..

So, it is ok for now :)

11 yorum:

  1. please elaborate the way to generate schema for annotted classes

    YanıtlaSil
  2. sure! the annotations which crashes with jaxb (mentioned above) are jpa annotations. using the lib coming with this sample(http://assets.devx.com/sourcecode/18778.zip ) prevents this crash and schemagen is able to create the schema files. btw, classes without no-arg constructors will lead to error while using schemagen, so take them out from your source folder prior to using schemagen tool.. i hope it is more clear now..

    YanıtlaSil
  3. yes..but the link you gave is not being extracted...

    YanıtlaSil
  4. the zip file is showing error..


    [End-of-central-directory signature not found. Either this file is not
    a zipfile, or it constitutes one disk of a multi-part archive. In the
    latter case the central directory and zipfile comment will be found on
    the last disk(s) of this archive.

    YanıtlaSil
  5. himm.. when i try downloading and opening the zip file in my open suse environment, there is no problem..
    then, maybe you can construct your own jaxb lib by collecting the following files:
    - activation.jar
    - jaxb-api.jar
    - jaxb-impl.jar
    - jaxb-xjc.jar
    - jsr173_1.0_api.jar
    - junit4.1.jar
    - mysql-connector-java-5.0.4-bin.jar
    - toplink-essentials-agent.jar
    - toplink-essentials.jar
    btw, the file set i listed above may not be the minimal list (i mean maybe not all of them are required), but this set works somehow!

    YanıtlaSil
  6. will you please re zip the content and send me @ jigarjm@gmail.com

    YanıtlaSil
  7. finally solved by maven plugin

    YanıtlaSil
  8. now the point is that can i generate schema xsd (request & response )from interface ?

    YanıtlaSil
  9. i tried schemagen with a sample interface and it just ignored it, no schema is created.. i don't think you can do that..

    YanıtlaSil
  10. The blog you quote is a blatant ripoff taken from
    http://www.devx.com/Java/Article/34069/0/page/1

    YanıtlaSil
  11. you are absolutely right! i replaced the link with the original one.. thanks for the correction..

    YanıtlaSil