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

标签 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 一起使用code> 以便使用 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

这是我的地址的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:
<import schemaLocation="maven:com.test:schema:jar::!/com/test/schemas/gml/gmlprofil-1.1.xsd" namespace="http://www.opengis.net/gml/3.2"/>
  • 设置绝对 schemaLocation 并相应更新目录文件:
  • <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"
    
  • maven-jaxb2-plugin 版本降低至 0.9.1。使用此方法,我什至不需要目录文件,但我收到此错误:

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

    这可以通过交换 -Xequals-XhashCode 而不是 -XsimpleEquals-XsimpleHashCode 来解决.

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

    最佳答案

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

    您肯定知道 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 依赖项有什么不好的地方?

    关于java - 使用 equals() 从多个 Maven 模块中具有相对 schemaLocation 的模式生成 JAXB 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33241554/

    相关文章:

    java - 如何在 Java 8 中使用参数调用方法?

    java - 如何在Java中将变量值注入(inject)到两个maven模块中?

    java - JAXB:JAXB 的问题

    java - 使用 Spring MVC 和 ajax 处理对象列表

    java - 如何使用jdt core代替ecj进行批量编译

    java - 如何使用 Maven 在没有外部 Jar 的情况下配置 Annotation Processing API?

    java - karaf 容器中的 OSGI 集成测试

    java - 如何将 java.time.Duration 映射到 XML

    java - XSD 架构元素可以是两种类型之一

    Java - 如何检查 13 位 isbn 号码是否有效