使用多个Maven模块中相对schemaLocation的模式中的equals()生成JAXB类

时间:2015-10-20 16:09:10

标签: java maven jaxb maven-jaxb2-plugin

我有一个包含3个模块的Maven项目:schemagmladress。 schema包含两个XSD:gmlprofil-1.1.xsd - 一个简单的GML配置文件和adress.xsd,它使用相对schemaLocation导入GML模式,如下所示:

<import schemaLocation="../gml/gmlprofil-1.1.xsd" namespace="http://www.opengis.net/gml/3.2"/>

gml模块和adress模块中,我使用maven-jaxb2-pluginjaxb2-basics来生成具有equals()和hashCode()的类。 gml模块工作正常,但在运行adress时,我收到此错误:

com.sun.istack.SAXParseException2; IOException thrown when processing "../gml/gmlprofil-1.1.xsd". Exception: java.io.FileNotFoundException: C:\code\gml\gmlprofil-1.1.xsd

精细。救援目录文件!

REWRITE_SYSTEM "../gml/gmlprofil-1.1.xsd" "maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd"

看起来绝对路径已经解决好了,但是发生了MalformedURLException。

[DEBUG] Parent resolver has resolved publicId [null], systemId [../gml/gmlprofil-1.1.xsd] to [maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd].
[DEBUG] Resolving systemId [maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd] as Maven dependency resource.
[DEBUG] Resolved dependency resource [Dependency {groupId=com.test, artifactId=schema, version=1.0-SNAPSHOT, type=jar, classifier=null, resource=com/test/schemas/gml/gmlprofil-1.1.xsd}] to resource URL [jar:file:/C:/code/jaxb-test/schema/target/schema-1.0-SNAPSHOT.jar!/com/test/schemas/gml/gmlprofil-1.1.xsd].
[DEBUG] Resolved systemId [maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd] to [jar:file:/C:/code/jaxb-test/schema/target/schema-1.0-SNAPSHOT.jar!/com/test/schemas/gml/gmlprofil-1.1.xsd].
[ERROR] Error while parsing schema(s).Location [ maven:com.test:schema:jar::!/com/test/schemas/adress/adress.xsd{8,97}].
org.xml.sax.SAXParseException; systemId: maven:com.test:schema:jar::!/com/test/schemas/adress/adress.xsd; lineNumber: 8; columnNumber: 97; unknown protocol: maven
...
Caused by: java.net.MalformedURLException: unknown protocol: maven

这是adress

的pom
  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.13.0</version>
    <executions>
      <execution>
        <phase>generate-sources</phase>
        <goals>
          <goal>generate</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <bindingDirectory>src/main/resources</bindingDirectory>
      <episode>true</episode>
      <useDependenciesAsEpisodes>true</useDependenciesAsEpisodes>
      <extension>true</extension>
      <forceRegenerate>false</forceRegenerate>
      <schemaLanguage>XMLSCHEMA</schemaLanguage>
      <strict>false</strict>
      <verbose>false</verbose>
      <catalog>src/main/resources/catalog.cat</catalog>
      <args>
        <arg>-npa</arg>
        <arg>-XsimpleEquals</arg>
        <arg>-XsimpleHashCode</arg>
      </args>
      <plugins>
        <plugin>
          <groupId>org.jvnet.jaxb2_commons</groupId>
          <artifactId>jaxb2-basics</artifactId>
          <version>0.9.5</version>
        </plugin>
      </plugins>
      <schemas>
        <schema>
          <dependencyResource>
            <groupId>com.test</groupId>
            <artifactId>schema</artifactId>
            <resource>com/test/schemas/adress/adress.xsd</resource>
          </dependencyResource>
        </schema>
      </schemas>
    </configuration>
  </plugin>

我尝试了一些解决方案:

  1. 将Maven URL设置为schemaLocation:
  2. <import schemaLocation="maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd" namespace="http://www.opengis.net/gml/3.2"/>
    
    1. 设置绝对schemaLocation并相应地更新目录文件:
    2. <import schemaLocation="http://test.com/schemas/gml/gmlprofil-1.1.xsd" namespace="http://www.opengis.net/gml/3.2"/>
      
      REWRITE_SYSTEM "http://test.com/schemas" "maven:com.test:schema:jar::!/com/test/schemas"
      
      1. maven-jaxb2-plugin版本降低到0.9.1。使用这种方法,我甚至不需要目录文件,但是我得到了这个错误:

        [错误]无法执行目标org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.9.1:生成(默认)项目地址:目标执行默认值org.jvnet.jaxb2.maven2:maven- jaxb2-plugin:0.9.1:生成失败:执行org.jvnet.jaxb2.maven2时缺少必需的类:maven-jaxb2-plugin:0.9.1:generate:com / sun / tools / xjc / model / Aspect < / p>

        可以通过更换-Xequals-XhashCode代替-XsimpleEquals-XsimpleHashCode来解决此问题。

      2. 所有三种解决方案都有效,但没有一种是完美的。前两个需要操作模式,而第三个需要在生成的类中引入对jaxb2-basics-runtime的不必要的依赖。后者是可以接受的,但如果有人知道如何使用我的设置运行simpleEquals,我将非常感激。

1 个答案:

答案 0 :(得分:2)

好吧,欢迎来到我痛苦的世界。

你肯定知道ogc-schema项目,对吧?

有趣的是,目录甚至似乎正确解决了。不知道那里发生了什么,但这是我最终发现最好的解决方案:

  • 编译绝对URL:

            <schemas>
                <schema>
                    <url>http://test.com/schemas/adres/adres.xsd</url>
                </schema>
            </schemas>
    
  • 使用目录重写

    REWRITE_SYSTEM“http://test.com/schemas”“maven:com.test:schema:jar ::!/ com / test / schemas”

这基本上是您的第三个解决方案,但您不必更改架构。只需从绝对URL开始,它就会被目录重写。

啊,是的:

免责声明我是以及上述ogc-schema项目的作者。

顺便说一句,jaxb2-basics-runtime依赖关系有什么不好?

相关问题