当前位置: 首页 > 知识库问答 >
问题:

使用具有相同基本模式的不同模式处理具有多个maven-jaxb2-plugin执行的重复类

何越
2023-03-14

我目前正在使用maven-jaxb2-plugin v0.13.3。

插件配置有多个执行,每个执行都处理wsdl文件。其中有几个是完全独立的,但有几个只是同一企业中不同的wsdl,其模式导入公共的基本模式。

请注意,我无法更改WSDL或模式。

原始代码使用设置为true的“forceRegen”,以及所有写入相同目标目录的代码。这在命令行构建中运行良好,但正如许多人现在所知,这会导致Eclipse中的无限构建循环。

我正在逐步解决这个问题。简单的步骤是将“ForceRegenate”设置为false,并为每次执行向“generateDirectory”目录添加后缀目录,以使其唯一。然后,我必须更改Eclipse项目属性,以删除它们都写入到的公共位置的原始源目录,并将其替换为每个现在唯一的源目录的单个引用。

到目前为止,如果所有执行都引用完全独立的模式,这将正常工作。

如果其中两个或多个执行指定了一个wsdl,该wsdl具有引用公共基模式的模式,这里就是这种情况,那么在构建时会出现重复的类错误。

如果有帮助的话,下面是一些从pom中略去的示例代码:

<execution>
  <id>unifiedServices</id>
  <goals>
    <goal>generate</goal>
  </goals>
  <configuration>
    <specVersion>2.2</specVersion>
    <schemaDirectory>src/main/resources/schemas/csi_UnifiedServices_240.0_schema</schemaDirectory>
    <schemaIncludes>
      <include>*.wsdl</include>
    </schemaIncludes>
    <generateDirectory>target/generated-sources/jaxb/us</generateDirectory>
    <forceRegenerate>false</forceRegenerate>
  </configuration>
</execution>
<execution>
  <id>iuclp</id>
  <goals>
    <goal>generate</goal>
  </goals>
  <configuration>
    <specVersion>2.2</specVersion>
    <schemaDirectory>src/main/resources/schemas/csi_OrderAndSubscriptionManagementMobility</schemaDirectory>
    <schemaIncludes>
      <include>*.wsdl</include>
    </schemaIncludes>
    <generateDirectory>target/generated-sources/jaxb/osmm</generateDirectory>
    <forceRegenerate>false</forceRegenerate>
  </configuration>
</execution>

我已经看到许多帖子谈论这个变体,其中许多是指插件的旧版本。

有哪些实用的解决方案仍然满足我的约束(不能修改wsdl/模式)?

更新:

正如第一个答案中建议的那样,我正在继续实现绑定文件。它不太起作用,我不确定我做错了什么。

这是第一次执行的配置块(我只更改了一个执行块,一旦确定它正在生成合理的代码,我将处理其他执行块,然后更改代码中的引用)。

<configuration>
    <bindingDirectory>src/main/resources/bindings</bindingDirectory>
    <bindingIncludes>
        <bindingInclude>unifiedservices.xjb</bindingInclude>
    </bindingIncludes>
  <specVersion>2.2</specVersion>
  <schemaDirectory>src/main/resources/schemas/csi_UnifiedServices_240.0_schema</schemaDirectory>
  <schemaIncludes>
    <include>*.wsdl</include>
  </schemaIncludes>
  <generateDirectory>target/generated-sources/jaxb/us</generateDirectory>
  <forceRegenerate>false</forceRegenerate>
</configuration>

这是略为省略的绑定文件(unifiedservices.xjb):

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2000/10/XMLSchema-instance"
    xs:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jxb:version="2.0">
    <jxb:bindings schemaLocation="http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd"
                  node="/xs:schema">
        <jxb:schemaBindings>
            <nameXmlTransform>
                <typeName suffix="us"/>
            </nameXmlTransform>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

这是略为省略的“CommonDataModel.xsd”的标题:

<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
 xmlns='http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd'
 targetNamespace='http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd'
 elementFormDefault='qualified'
 version='240.0.03'>

当我运行此构建时,第一个错误是:

[ERROR] Error while parsing schema(s).Location [ file:/C:/.../src/main/resources/bindings/unifiedservices.xjb{8,37}].
com.sun.istack.SAXParseException2: "http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd" is not a part of this compilation. Is this a mistake for "file:/C:/.../src/main/resources/schemas/csi_UnifiedServices_240.0_schema/CommonDataModel.xsd"?

更新:

我更新了绑定文件,以便“schemaLocation”只是相关模式文件的相对路径,但现在我得到了一个不同的错误。

这是我的新绑定文件:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2000/10/XMLSchema-instance"
    xs:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jxb:version="2.0">
    <jxb:bindings schemaLocation="../schemas/csi_UnifiedServices_240.0_schema/CommonDataModel.xsd"
                  node="/xs:schema">
        <jxb:schemaBindings>
            <nameXmlTransform>
                <typeName suffix="us"/>
            </nameXmlTransform>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

当我用这个构建时,我得到:

[ERROR] Error while parsing schema(s).Location [ file:/C:/.../src/main/resources/bindings/unifiedservices.xjb{8,37}].
com.sun.istack.SAXParseException2: XPath evaluation of "/xs:schema" results in empty target node

这是“CommonDataModel.xsd”文件中略去的标题:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ... -->
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'
 xmlns='http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd'
 targetNamespace='http://.../Namespaces/UnifiedServices/Types/Public/CommonDataModel.xsd'
 elementFormDefault='qualified'
 version='240.0.03'>

我不得不问自己,这种相对路径是否正确。我想如果它是错误的,我会得到一个更具体的错误消息。为了进行一点确认,我运行了SysInternals ProcessMonitor,查看“CommonDataModel.xsd”,它们都位于同一个文件中,并且都成功了。

更新:

我想我找到了最后一个问题的可能原因。下面指出了一个问题:JAXB绑定文件中的XPath求值导致目标节点为空。

因此,我确保两个“xs”名称空间是相同的。这消除了xjc错误。然而,它似乎也没有做任何事情。

我当前的绑定文件是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xs:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
    jxb:version="2.0">
    <jxb:bindings schemaLocation="../schemas/csi_UnifiedServices_240.0_schema/CommonDataModel.xsd"
                  node="/xs:schema">
        <jxb:schemaBindings>
            <jxb:nameXmlTransform>
                <jxb:typeName suffix="us"/>
            </jxb:nameXmlTransform>
        </jxb:schemaBindings>
    </jxb:bindings>
</jxb:bindings>

当我运行构建时,我会看到如下输出行块:

[INFO] --- maven-jaxb2-plugin:0.13.3:generate (unifiedServices) @ UnifiedAccountMs ---
[INFO] Sources are not up-to-date, XJC will be executed.
[INFO] Episode file [C:\...\target\generated-sources\jaxb\us\META-INF\sun-jaxb.episode] was augmented with if-exists="true" attributes.

看起来不错,但没告诉我什么。当它最终完成编译时,会出现如下错误:

[ERROR] /C:/.../target/generated-sources/jaxb/us/com/cingular/csi/csi/namespaces/unifiedservices/infrastructurecommon/types/_public/messageheader/ObjectFactory.java:[32,8] duplicate class: com.cingular.csi.csi.namespaces.unifiedservices.infrastructurecommon.types._public.messageheader.ObjectFactory

当我搜索该fqcn时,我确实找到了它的多次出现(省略的部分是相同的):

./target/generated-sources/jaxb/ausn/com/.../namespaces/unifiedservices/infrastructurecommon/types/_public/messageheader/ObjectFactory.java
./target/generated-sources/jaxb/us/com/.../namespaces/unifiedservices/infrastructurecommon/types/_public/messageheader/ObjectFactory.java

我可能误解了“typeName”转换应该做什么,但它似乎没有改变任何东西。无论如何,我有一种感觉,在类名后面附加后缀并不完全是我想要的。我认为更改包名称更合适,但我甚至无法让类名转换做任何事情。

共有1个答案

丰岳
2023-03-14

一种解决方案是在每次执行中添加一个单独的绑定文件(*.xjb),以生成特定的类(并避免名称冲突)。

<configuration>
  <bindingDirectory>src/main/bindings</bindingDirectory>
  <bindingIncludes>
    <include>unifiedServices.xjb</include>
  </bindingIncludes>

请参阅XJC文档,但例如,可以为所有类名添加后缀:

<schemaBindings>
  <nameXmlTransform>
    <typeName suffix="_SuffixA"/>
  </nameXmlTransform>
</schemaBindings>

这样做,如果名称空间“company.com”的相同“Foo”类型是由多个wsdl导入的相同公共模式的一部分,那么最终可能会得到不同的类:

  • com。公司Foo\u后缀(在“unifiedServices”中生成)
  • com。公司Foo\U SUFFEXB(在“iuclp”中生成)
 类似资料:
  • 我有多个xsd模式,我想将它们解组到同一文件夹下的不同包中。我尝试了这两个插件,两个插件似乎都能很好地处理这两种配置,但在maven-jaxb2-plugin的情况下,eclipse插件会无限期地生成类(因为=true),但是如果我没有指定forceRegenate,当我运行我的配置有任何问题吗? jaxb2 maven插件 maven-jaxb2-plugin 以及build helper ma

  • 问题内容: 我想将多个xsd模式解组到 同一 文件夹下的不同程序包中。我尝试了两个插件,并且似乎都可以在这两种配置下正常工作,但是在使用maven- jaxb2-plugin的情况下,eclipse插件会无限期地生成类(由于= true),但是如果我不指定forceRegenerate,它将不会生成运行时完全没有第二组和第三组类,我的配置是否有问题? jaxb2-maven-插件 maven-ja

  • 我有一个gradle android项目,有两个模块: 穿 在我的gradle配置中,我有不同的构建类型。默认设置(debug和release,每个都有自定义设置)以及dev和beta构建类型(也有自定义签名、自定义proguard和自定义applicationIdSuffix)。 我现在想做的是使用构建类型测试版(gradle清洁组件测试版)构建应用程序包。这就开始在测试版中构建应用程序,看到它

  • 我有一个Maven项目,包含三个模块:、和。模式包含两个XSD:-一个简单的GML配置文件和使用相对模式位置导入GML模式,如下所示: 在模块和模块中,我使用和来生成带有equals()和hashCode()的类。模块工作正常,但在运行时,我遇到以下错误: 好的一份目录文件来拯救我们! 看起来绝对路径已解决,但随后出现了畸形的异常。 这是我的的pom: 我尝试过一些解决方案: 将Maven URL

  • 我有一个场景,其中一个url“serachUser”可能带有两个不同的值(请求参数)userId或UserName。 为此我创造了两种方法 但我得到模糊映射发现异常。Spring能处理这种情况吗?

  • 我唯一关心的是,如果我没有泄露任何设计原则,如开放和关闭或类似的东西,并且如果我需要添加一个新的通知,我需要在所有其他类中实现。(这是痛苦的--想象一下10个甚至更多的观察者)。 我想在make this difference中,只创建一个接口,然后我可以继承它创建其他通知,但有一个问题,观察者如何确定每种不同类型的通知? 示例: