Question

The scenario is the following: I have a java package (myapp.config) that contains all my JAXB-annotated classes, from which I'd like to generate an XML schema file. In the package, there is also a package-info.java file, which contains the @XmlSchema annotation on the package element, thus valid in the whole package:

@XmlSchema(namespace="http://myapp/config",
           xmlns = @XmlNs(prefix="myapp", namespaceURI="http://myapp/config"),
           elementFormDefault=XmlNsForm.QUALIFIED)
package myapp.config;

When I run the schemagen command-line tool that comes with the JDK,

D:\Projects\Myapp>"%JAVA_HOME%/bin/schemagen" -cp "%JAVA_HOME%/lib/tools.jar"
src/myapp/config/*.java

I get the correct result: the root element in the generated schema1.xsd file contains the targetNamespace and other necessary attributes:

<xs:schema elementFormDefault="qualified" version="1.0"
           targetNamespace="http://myapp/config"
           xmlns:tns="http://myapp/config"
           xmlns:myapp="http://myapp/config">
           xmlns:xs="http://www.w3.org/2001/XMLSchema" >

However, when I want to perform the same task within my Ant script,

<schemagen srcdir="src/myapp/config" destdir="dist">
    <include name="*.java"/>
</schemagen>

The schemagen tool seems to completely ignore the namespace annotations, because in the resulting schema1.xsd file, there is no trace of them:

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

When I run the schemagen task in verbose and debug mode, it becomes obvious that the tool does load all the .java files, except package-info.java, that's why it doesn't know anything about the namespaces.

Have you met this strange behavior before? Why the command-line tool processes the package info file, and the Ant task doesn't? What should I do to make the Ant task work?

Thanks in advance,
buc

Was it helpful?

Solution

Fortunately I found the solution, I post it for those who run into the same problem in the future:

The reason it did not work is that I called the schemagen task in the wrong way (this can happen, if there is so little documentation or examples as in the case of schemagen). Instead of writing:

<schemagen srcdir="src/myapp/config" destdir="dist">
    <include name="*.java"/>
</schemagen>

You should move the specific part of the path into the include element:

<schemagen srcdir="src" destdir="dist">
    <include name="myapp/config/*.java"/>
</schemagen>

I'm still not sure why this is happening, but this way, the schemagen processor recognizes that the myapp/config directory in fact designates the java package myapp.config and by knowing that it is a package, it will also read the package info file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top