schemagen etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
schemagen etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

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 :)