方案如下:我有一个包含所有JAXB注释类的Java包( myapp.config ),我想生成XML模式文件。在包中,还有一个 package-info.java 文件,其中包含包元素上的 @xmlschema 注释,因此在整个包装中有效:

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

当我运行jdk附带的 schemagen 命令行工具时,

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

我得到了正确的结果:生成的 schema1.xsd 文件中的根元素包含 targetnamespace 和其他必要的属性:

<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" >
. 但是,当我想在我的Ant脚本中执行相同的任务时,
<schemagen srcdir="src/myapp/config" destdir="dist">
    <include name="*.java"/>
</schemagen>
.

模式工具似乎完全忽略了命名空间注释,因为在生成的 schema1.xsd 文件中,没有迹象:

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

当我在 verbose debug 模式下运行 schemagen 任务时,很明显该工具确实加载了所有。 java 文件,除 package-info.java 之外,这就是为什么它对命名空间不了解。

你以前遇到过这个奇怪的行为吗?为什么命令行工具处理包信息文件,ant任务没有?我该怎么做才能使蚂蚁任务工作?

提前感谢,
buc

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top