Domanda

Lo scenario è il seguente: ho un pacchetto Java ( myApp.config ) che contiene tutte le mie classi annotate di JaxB, da cui vorrei generare un file schema XML. Nel pacchetto, c'è anche un file pacchetti-info.java , che contiene l'annotazione @xmlschema sull'elemento del pacchetto, quindi valido nell'intero pacchetto: Quando eseguo lo strumento schemagen che viene fornito con il JDK,

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

ottengo il risultato corretto: l'elemento root nel file generato schema1.xsd contiene il targetnamespace e altri attributi necessari:

<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" >
.

Tuttavia, quando voglio eseguire lo stesso compito all'interno del mio script ant,

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

Lo strumento Schemagen sembra ignorare completamente le annotazioni dello spazio dei nomi, perché nel file risultante schema1.xsd , non c'è traccia di loro:

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

Quando eseguo l'attività schemagen in modalità verbose e debug , diventa ovvio che lo strumento carica tutti i . java file, tranne pacchetto-info.java , ecco perché non sa nulla dei nomi.

Hai già incontrato questo strano comportamento? Perché lo strumento della riga di comando elabora il file di informazioni del pacchetto e l'attività ant no? Cosa dovrei fare per rendere il compito delle formiche?

Grazie in anticipo,
BUC

È stato utile?

Soluzione

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top