从 xsd 生成 java 类 从通过目录提供的 jar 导入其他 xsd

generate java classes from xsd importing other xsd from jar provided via catalog

我有一些 XSD 其中一些具有共同的定义。我把这个通用定义分开成一个单独的XSD。到目前为止,我通过项目的相对路径导入了这个 common.xsd。 Thsi 成功了,因为所有项目都保存在 dame SCM 存储库中。

现在我将项目分离到单独的 SCM 存储库中,我需要更改对 common.xsd 已经存在于其自己的项目 (common-xml.jar) 中的访问权限。

我读了这个:http://www.sagehill.net/docbookxsl/WriteCatalog.html and https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Catalogs 但它对我不起作用。该目录未使用,xjc 在 maven-jaxb2-plugin/configuration/schemaDirectory.

中的路径配置中查找 common.xsd

即使我添加maven-jaxb2-plugin/configuration/arg>-Dxml.catalog.verbosity=999(或运行 mvn -X -Dxml.catalog.verbosity=999 ...它甚至不告诉读取目录文件。

当我使用 XML 形式并保留 <!DOCTYPE ..:> 元素时,我只得到目录文件的输出,但这会导致超时,因为代理只允许浏览器连接到互联网(公司政策)。

我也读过这个答案 Maven - Have an XSD as a dependency 但它导致了同样的问题:xjc 在模式目录中查找要包含的文件,但我想避免将它提取到可能被检查的地方一不小心进入了SCM...

我如何强制 xjc 查找 XSD 导入其他文件的位置?

这是我的maven项目配置:

   <plugin>
      <groupId>org.jvnet.jaxb2.maven2</groupId>
      <artifactId>maven-jaxb2-plugin</artifactId>
      <version>0.14.0</version>
      <configuration>
        <args>-Dxml.catalog.verbosity=999</args>
        <clearOutputDir>true</clearOutputDir>
        <removeOldOutput>true</removeOldOutput>
        <specVersion>2.2</specVersion>
        <arguments>-Xcommons-lang</arguments>
        <schemaDirectory>../src/main/xsd</schemaDirectory>
        <catalogs>
          <catalog>
            <dependencyResource>
              <groupId>my.group.id</groupId>
              <artifactId>common-xml-definitions</artifactId>
              <resource>catalog.xml</resource> 
             <!-- alternatively 
              <resource>catalog.cat</resource>
             for plain text version -->;
            </dependencyResource>
          </catalog>
        </catalogs>
        <schemaIncludes>
          <schemaInclude>*.xsd</schemaInclude>
        </schemaIncludes>
        <bindingDirectory>../src/main/xjb</bindingDirectory>
        <bindingIncludes>
          <bindingInclude>bindings.xml</bindingInclude>
        </bindingIncludes>
        <schemaLanguage>XMLSCHEMA</schemaLanguage>
        <generatePackage>a.package.name</generatePackage>
        <verbose>false</verbose>
        <args>
          <arg>-XtoString</arg>
          <arg>-Xequals</arg>
          <arg>-XhashCode</arg>
        </args>
        <plugins>
          <plugin>
            <groupId>com.buschmais.jaxbfx</groupId>
            <artifactId>jaxbfx-plugin</artifactId>
            <version>1.1.0</version>
          </plugin>
          <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.12.0</version>
          </plugin>
        </plugins>
      </configuration>
    </plugin>

这是目录文件:

tagret XSD 的开头是:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:codef="http://my.url/commonDefinitions" xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
  jxb:version="1.0" elementFormDefault="qualified">
  <xs:import namespace="http://my.url/commonDefinitions"
    schemaLocation="commonDefinitions.xsd" />
 <!-- ... -->
</xs:schema>

带有 DOCTYPE 的 XML-Catalog 的 Maven 错误消息是:

[ERROR] Failed to execute goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0:generate (MyProject generate-sources) on project MyProject: Error parsing catalog [jar:file:/d:/maven-repository/my/package/common-xml-definitions/1.0.0/common-xml-definitions-1.0.0.jar!/catalog.xml].: Connection timed out: connect -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.14.0:generate (MyProject generate-sources) on project MyProject: Error parsing catalog [jar:file:/d:/maven-repository/my/package/common-xml-definitions/1.0.0/common-xml-definitions-1.0.0.jar!/catalog.xml].
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: org.apache.maven.plugin.MojoExecutionException: Error parsing catalog [jar:file:/d:/maven-repository/my/package/common-xml-definitions/1.0.0/common-xml-definitions-1.0.0.jar!/catalog.xml].
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.parseResolvedCatalogURIs (RawXJC2Mojo.java:1205)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.setupCatalogResolver (RawXJC2Mojo.java:876)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute (RawXJC2Mojo.java:432)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute (RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.net.ConnectException: Connection timed out: connect
    at java.net.DualStackPlainSocketImpl.connect0 (Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect (DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect (AbstractPlainSocketImpl.java:350)
    at java.net.AbstractPlainSocketImpl.connectToAddress (AbstractPlainSocketImpl.java:206)
    at java.net.AbstractPlainSocketImpl.connect (AbstractPlainSocketImpl.java:188)
    at java.net.PlainSocketImpl.connect (PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect (SocksSocketImpl.java:392)
    at java.net.Socket.connect (Socket.java:589)
    at java.net.Socket.connect (Socket.java:538)
    at sun.net.NetworkClient.doConnect (NetworkClient.java:180)
    at sun.net.www.http.HttpClient.openServer (HttpClient.java:463)
    at sun.net.www.http.HttpClient.openServer (HttpClient.java:558)

带有纯文本目录的错误消息 XML 没有 DOCTYPE:

[INFO] Parsing input schema(s)...
[DEBUG] Resolving publicId [http://my.url/commonDefinitions], systemId [commonDefinitions.xsd].
resolveSystem(commonDefinitions.xsd)
resolvePublic(http://my.url/commonDefinitions,commonDefinitions.xsd)
[DEBUG] Parent resolver has resolved publicId [http://my.url/commonDefinitions], systemId [commonDefinitions.xsd] to [null].
[DEBUG] SystemId [commonDefinitions.xsd] is not a Maven dependency resource URI. Returning parent resolver result [null].
[WARNING] Error while parsing schema(s).Location [ file:/D:/data/scm-workspace/MyProject/src/main/xsd/parad.xsd{6,46}].
org.xml.sax.SAXParseException: schema_reference.4: Schemadokument 'commonDefinitions.xsd' konnte nicht gelesen werden, da 1) das Dokument nicht gefunden werden konnte; 2) das Dokument nicht gelesen werden konnte; 3) das Root-Element des Dokuments nicht <xsd:schema> ist.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException (ErrorHandlerWrapper.java:203)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.warning (ErrorHandlerWrapper.java:99)
    at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError (XMLErrorReporter.java:392)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaErr (XSDHandler.java:4154)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.reportSchemaWarning (XSDHandler.java:4149)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument1 (XSDHandler.java:2491)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.getSchemaDocument (XSDHandler.java:2193)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.resolveSchema (XSDHandler.java:2084)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.constructTrees (XSDHandler.java:1014)
    at com.sun.org.apache.xerces.internal.impl.xs.traversers.XSDHandler.parseSchema (XSDHandler.java:625)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadSchema (XMLSchemaLoader.java:610)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar (XMLSchemaLoader.java:569)
    at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaLoader.loadGrammar (XMLSchemaLoader.java:535)
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema (XMLSchemaFactory.java:254)
    at com.sun.tools.xjc.reader.xmlschema.parser.SchemaConstraintChecker.check (SchemaConstraintChecker.java:111)
    at com.sun.tools.xjc.ModelLoader.loadXMLSchema (ModelLoader.java:342)
    at com.sun.tools.xjc.ModelLoader.load (ModelLoader.java:162)
    at com.sun.tools.xjc.ModelLoader.load (ModelLoader.java:117)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.loadModel (XJC23Mojo.java:50)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:40)
    at org.jvnet.mjiip.v_2_3.XJC23Mojo.doExecute (XJC23Mojo.java:28)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute (RawXJC2Mojo.java:478)
    at org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute (RawXJC2Mojo.java:320)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:498)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:289)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:229)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:415)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:356)
Caused by: java.io.FileNotFoundException: D:\data\scm-workspace\MyProject\src\main\xsd\commonDefinitions.xsd (Das System kann die angegebene Datei nicht finden)
    at java.io.FileInputStream.open0 (Native Method)
    at java.io.FileInputStream.open (FileInputStream.java:195)
    at java.io.FileInputStream.<init> (FileInputStream.java:138)
    at java.io.FileInputStream.<init> (FileInputStream.java:93)
    at sun.net.www.protocol.file.FileURLConnection.connect (FileURLConnection.java:90)

有时候仔细阅读可以解决问题...

https://github.com/highsource/maven-jaxb2-plugin/wiki/Using-Catalogs:

Due to the bug in the XJC this only works if schemaLocation is not specified. So, unfortunately the following combination does not work at the moment.

PUBLIC "http://www.w3.org/1999/xlink" "w3c/1999/xlink.xsd"<br> <import namespace="http://www.w3.org/1999/xlink" schemaLocation="http://www.w3.org/1999/xlink.xsd" />

从导入中删除 schemaLocation 属性解决了问题。