25

.xsdコードを自動生成したいファイルがたくさんあります。いくつかのファイルに重複した名前があり、それらすべてを同時に生成しようとすると衝突します。

私はこれらのうちの2つを機能させることに集中しています。

これら2つが機能するようになったら、残りを修正します。しかし、今のところ、これらのファイルのうちの 2 つだけに焦点を当てています。私はそれらを制御できません。それらはベンダーからのものであり、「標準」に従っているため、複数の理由でそれらを編集することはできません。

を使用しmaven-jaxb2-pluginてこれらのファイルを処理しています。

binding.xjbの回答のリンクで提案されているようにファイルを追加mat bし、ウェブで見つけたその他の指示を追加しました。しかし、次のエラーが表示されます。出力はありません。

<?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生成されるクラスが 1 つだけであるか、各クラスが独自のパッケージ名前空間で生成されるかのいずれかで、この重複を解決するにはどうすればよいですか?

このような重複要素はこれだけではありません。重複する要素がたくさんあるため、ファイルからそれらを削除しようとすることもできません。

これhalignは複数の.xsdファイルにあり、それらを個々のパッケージに入れるか、生成された最初のファイルを使用するようコンパイラーに指示できるようにしたいと考えています。

これは、外部.xjbファイルを試す前にpackagepom.xml.

重複する構成を無視するか、それらを別のパッケージにマップするか、既存の実装にマップするようにバインディングを構成するにはどうすればよいですか?

4

5 に答える 5

11

これには中途半端な回避策がありますが、非常に時間がかかります。<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/>実行され、残りは同じディレクトリに生成しているため、実行する必要がないと考えられます。

重複を処理するための労力の少ないソリューションが必要です。

<episode/>最初のマスター .xsd を独自の .jar ファイルにビルドする別のモジュールにすると、タグを使用して同じ重複要素の生成を何度もスキップできると思います。これらは定義が同一であるためです。

それ以来、可能であれば XML と JAXB を完全に放棄することにしました。この編集の時点で、XML を解析してオブジェクトにマップするためのより新しくて優れた方法があります。

于 2011-07-14T16:33:39.163 に答える
5

これは古いことを認識してください。しかし、同じエラーが発生し、ターゲットパッケージ、つまりxjcで-bオプションを指定しないことで解決できました。次に、重複する要素はそれぞれ独自の名前空間パッケージに作成され、競合は発生しません。

于 2012-11-15T11:12:07.003 に答える
3

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
}
于 2015-05-27T18:03:05.863 に答える
1

<schemaBindings>XSD に要素を追加します。

<schemaBindings>
    <package name="com.whatever.stuff" />
</schemaBindings>

ソース

于 2011-07-13T15:21:09.713 に答える