我有几十个.xsd
文件想要为其自动生成代码。有几个文件具有重复的名称,当我试图同时生成所有这些文件时,它们会发生冲突。
我只专注于尝试让其中的两个工作。
当我得到这两个工作,我会解决其余的。但我现在只关注其中的两个文件。我不能控制它们,它们来自供应商并且遵循一个“标准”,因此出于多种原因,不能选择编辑它们。
我正在使用maven-jaxb2-plugin
来处理这些文件。
我按照mat b
答案中的链接和我在网上找到的其他说明中的建议添加了一个binding.xjb
文件,但我得到了以下错误,没有输出。
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="2.1"
xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation=" http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd">
<jxb:bindings schemaLocation="mac-3.4.xsd">
<jxb:schemaBindings>
<jxb:package name="my.company.mac"/>
</jxb:schemaBindings>
</jxb:bindings>
<jxb:bindings schemaLocation="mac-stylesheet-3.4.xsd">
<jxb:schemaBindings>
<jxb:package name="my.company.stylesheet"/>
</jxb:schemaBindings>
</jxb:bindings>
</jxb:bindings>
给出以下错误
[ERROR] Error while parsing schema(s).Location [ file:/C:/Users/Jarrod%20Roberson/Projects/spa-tools/spa-lib/src/main/sc
hema/mac-stylesheet-3.4.xsd{165,33}].
org.xml.sax.SAXParseException: 'halign' is already defined
令人不快的元素是:(还有很多其他元素,这只是第一个冲突的元素)
<xsd:simpleType name="halign">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="left" />
<xsd:enumeration value="center" />
<xsd:enumeration value="right" />
</xsd:restriction>
</xsd:simpleType>
并且在每个.xsd
文件中都是相同的,那么在只生成一个类或在各自的包命名空间中生成每个类的情况下,如何解决这种重复问题呢?
这并不是唯一这样的重复元素,有很多这样的元素,所以尝试从文件中删除它们也不是一个可行的选择。
这个halign
在多个.xsd
文件中,我希望将它们放在各自的包中,或者能够告诉编译器使用生成的第一个包。
这是我在尝试外部.xjb
文件之前开始的地方,只需在pom.xml
中指定package
即可。
如何配置绑定以忽略重复的配置,将它们映射到单独的包,或者将它们映射到现有实现?
发布于 2011-07-15 00:33:39
我对此有一个半途而废的解决方案,但它非常耗时。我必须为每个有重复条目的文件创建一个单独的<execution/>
。
<executions>
<execution>
<id>jaxb-mac</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<generatePackage>my.company.mac</generatePackage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>mac-3.4.xsd</include>
</schemaIncludes>
</configuration>
</execution>
<execution>
<id>jaxb-stylesheet</id>
<phase>generate-sources</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<forceRegenerate>true</forceRegenerate>
<generatePackage>my.company.stylesheet</generatePackage>
<schemaDirectory>src/main/schema</schemaDirectory>
<schemaIncludes>
<include>mac-stylesheet-3.4.xsd</include>
</schemaIncludes>
</configuration>
</execution>
<forceRegenerate>true</forceRegenerate>
很重要,否则只有第一个<execution/>
会运行,其余的会认为没有必要运行,因为我正在生成到相同的目录中。
我仍然希望有一种不那么劳力密集的解决方案来处理重复的东西。
我认为如果我将第一个主.xsd作为一个单独的模块构建到它自己的.jar文件中,那么我就可以使用<episode/>
标记,让它跳过一次又一次生成相同的重复元素,因为它们在定义上是相同的。
从那以后,我决定尽可能放弃XML和JAXB。从这次编辑开始,有更新、更好的方法来解析XML并将其映射到对象。
发布于 2012-11-15 19:12:07
意识到这是旧的,但我有相同的错误,它可以通过没有指定目标包来解决它吗,即使用xjc的-b选项。然后,每个重复的元素都会在它们自己的名称空间包中创建,并且不会发生冲突。
发布于 2015-05-28 02:03:06
我发布了我的gradle解决方案,它解决了重复问题,并且不需要xjb文件:
task generateJaxb() {
ext.sourcesDir = "${buildDir}/generated-sources/jaxb"
ext.classesDir = "${buildDir}/classes/jaxb"
ext.schemaDir = "src/resources/schemas"
ext.tmp = "${buildDir}/tmp/xjc"
doLast() {
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
delete(dir: tmp)
mkdir(dir: tmp)
delete(dir: sourcesDir)
delete(dir: classesDir)
mkdir(dir: sourcesDir)
mkdir(dir: classesDir)
}
fileTree(schemaDir){
include '**/*.xsd'
include '**/*.wsdl'
}.each{File file->
//println file
project.ant {
taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask",
classpath: configurations.jaxb.asPath
xjc(destdir: tmp, schema:"${file.getAbsolutePath()}") {
arg(value: "-wsdl")
produces(dir: tmp, includes: "**/*.java")
}
copy(todir: sourcesDir) {
fileset(dir: tmp, erroronmissingdir: false) {
include(name: "**/*.java")
}
}
delete(dir: tmp)
mkdir(dir: tmp)
}
}
project.ant {
javac(destdir: classesDir, source: 1.6, target: 1.6, debug: true,
debugLevel: "lines,vars,source",
classpath: configurations.jaxb.asPath) {
src(path: sourcesDir)
include(name: "**/*.java")
include(name: "*.java")
}
copy(todir: classesDir) {
fileset(dir: sourcesDir, erroronmissingdir: false) {
exclude(name: "**/*.java")
}
}
}
}
}
configurations {
jaxb
}
dependencies {
jaxb("com.sun.xml.bind:jaxb-xjc:2.2.4-1")
compile(files(generateJaxb.classesDir).builtBy(generateJaxb))
}
jar {
from generateJaxb.classesDir
}
https://stackoverflow.com/questions/6681265
复制相似问题