2014-09-24 79 views
2

我有一个jaxb插件的大问题。我有一个项目A,其中我有src/main/resources/xsd/common.xsd文件。在这个项目中,我使用cxf-xjc-插件 来生成java类。我也有我的节目文件在src/main/resources/META-INF下,名为sun-jaxb.episodejaxb,xsd import not recognized

接下来,我有项目B,它对项目A有Maven依赖关系。在这个项目中,我有src/main/resources /catalog.txt

PUBLIC "http://www.some_path" "maven:GROUP_ID_OF_PROJECT_A:ARTIFACT_ID_OF_PROJECT_A:jar::!/common.xsd" 

在BI项目与JAXB插件

<plugin> 
      <groupId>org.jvnet.jaxb2.maven2</groupId> 
      <artifactId>maven-jaxb2-plugin</artifactId> 
      <configuration> 
       <extension>true</extension> 
       <episodes> 
        <episode> 
         <groupId>GROUP_ID_OF_PROJECT_A</groupId> 
         <artifactId>ARTIFACT_ID_OF_PROJECT_A</artifactId> 
        </episode> 
       </episodes> 
       <catalogs> 
        <catalog>src/main/resources/catalog.txt</catalog> 
       </catalogs> 
      </configuration> 
</plugin> 

下一页POM文件,我有B工程

src/main/resources/other/xsd my main.xsd 

文件,其中我使用类型定义从common.xsd

我的xmlns:CMNS = “HTTP://www.some_path” //这是相同catalog.txt 和

<xs:import namespace="http://www.some_path"/> 

但问题是,我得到错误未定义简单或复杂的类型,因为它不承认CMNS

回答

3

我使用这个插件来解决XSD的公共进口

<plugin> 
    <groupId>org.jvnet.jaxb2.maven2</groupId> 
    <artifactId>maven-jaxb2-plugin</artifactId> 
    <version>0.8.1</version> 
    <executions> 
     <execution> 
      <phase>generate-sources</phase> 
      <goals> 
       <goal>generate</goal> 
      </goals> 
     </execution> 
    </executions> 
    <configuration> 
     <args> 
      <arg>-Xannotate</arg> 
      <arg>-Xnamespace-prefix</arg> 
      <arg>-nv</arg> 
     </args> 
     <extension>true</extension> 
     <forceRegenerate>true</forceRegenerate> 
     <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory> 
     <bindingIncludes> 
      <include>*.xjb</include> 
     </bindingIncludes> 
     <schemas> 
      <schema> 
       <fileset> 
        <directory>${basedir}/src/main/resources/xsd</directory> 
        <includes> 
         <include>*.xsd</include> 
        </includes> 
       </fileset> 
      </schema> 
      <schema> 
       <dependencyResource> 
        <groupId>groupID</groupId> 
        <artifactId>artifactID</artifactId> 
        <resource>target.xsd</resource> 
       </dependencyResource> 
      </schema> 

     </schemas> 
     <episodes> 
      <episode> 
       <groupId>groupID</groupId> 
       <artifactId>artifactID</artifactId> 
      </episode> 
     </episodes> 
     <debug>true</debug> 
     <verbose>true</verbose> 
     <plugins> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-basics</artifactId> 
       <version>0.6.2</version> 
      </plugin> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-basics-annotate</artifactId> 
       <version>0.6.2</version> 
      </plugin> 
      <plugin> 
       <groupId>org.jvnet.jaxb2_commons</groupId> 
       <artifactId>jaxb2-namespace-prefix</artifactId> 
       <version>1.1</version> 
      </plugin> 
     </plugins> 
    </configuration> 
</plugin> 

插件项目的pom.xml的一个

<plugin> 
     <groupId>org.jvnet.jaxb2.maven2</groupId> 
     <artifactId>maven-jaxb2-plugin</artifactId> 
     <version>0.8.1</version> 
     <executions> 
      <execution> 
       <phase>generate-sources</phase> 
       <goals> 
        <goal>generate</goal> 
       </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <args> 
       <arg>-Xannotate</arg> 
       <arg>-nv</arg> 
       <arg>-Xnamespace-prefix</arg> 
      </args> 
      <extension>true</extension> 
      <schemas> 
       <schema> 
        <fileset> 
         <directory>${basedir}/src/main/resources/xsd/</directory> 
         <includes> 
          <include>A.xsd</include> 
         </includes> 
        </fileset> 
       </schema> 
      </schemas> 
      <bindingDirectory>src/main/resources/xjb</bindingDirectory> 
      <bindingIncludes> 
       <include>*.xjb</include> 
      </bindingIncludes> 
      <debug>true</debug> 
      <verbose>true</verbose> 
      <forceRegenerate>true</forceRegenerate> 
      <plugins> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics</artifactId> 
        <version>0.6.0</version> 
       </plugin> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics-annotate</artifactId> 
        <version>0.6.0</version> 
       </plugin> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-namespace-prefix</artifactId> 
        <version>1.1</version> 
       </plugin> 
      </plugins> 
     </configuration> 
    </plugin> 

插件项目B的pom.xml中

<plugin> 
     <groupId>org.jvnet.jaxb2.maven2</groupId> 
     <artifactId>maven-jaxb2-plugin</artifactId> 
     <version>0.8.1</version> 
     <executions> 
      <execution> 
       <phase>generate-sources</phase> 
       <goals> 
        <goal>generate</goal> 
       </goals> 
      </execution> 
     </executions> 
     <configuration> 
      <args> 
       <arg>-Xannotate</arg> 
       <arg>-Xnamespace-prefix</arg> 
       <arg>-nv</arg> 
      </args> 
      <extension>true</extension> 
      <forceRegenerate>true</forceRegenerate> 
      <bindingDirectory>${basedir}/src/main/resources/xjb</bindingDirectory> 
      <bindingIncludes> 
       <include>*.xjb</include> 
      </bindingIncludes> 
      <schemas> 
       <schema> 
        <fileset> 
         <directory>${basedir}/src/main/resources/xsd/</directory> 
         <includes> 
          <include>B.xsd</include> 
         </includes> 
        </fileset> 
       </schema> 
       <schema> 
        <dependencyResource> 
         <groupId>AgroupID</groupId> 
         <artifactId>AartifactID</artifactId> 
         <resource>xsd/A.xsd</resource> 
        </dependencyResource> 
       </schema> 
      </schemas> 
      <episodes> 
       <episode> 
        <groupId>AgroupID</groupId> 
        <artifactId>AartifactID</artifactId> 
       </episode> 
      </episodes> 
      <debug>true</debug> 
      <verbose>true</verbose> 
      <plugins> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics</artifactId> 
        <version>0.6.2</version> 
       </plugin> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-basics-annotate</artifactId> 
        <version>0.6.2</version> 
       </plugin> 
       <plugin> 
        <groupId>org.jvnet.jaxb2_commons</groupId> 
        <artifactId>jaxb2-namespace-prefix</artifactId> 
        <version>1.1</version> 
       </plugin> 
      </plugins> 
     </configuration> 
    </plugin> 

必须执行MVN项目之前安装项目A的B


添加这个插件添加到源目标生成的类

  <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>build-helper-maven-plugin</artifactId> 
       <version>1.1</version> 
       <executions> 
        <execution> 
         <id>add-source</id> 
         <phase>generate-sources</phase> 
         <goals> 
          <goal>add-source</goal> 
         </goals> 
         <configuration> 
          <sources> 
           <source>target/generated-sources/xjc</source> 
          </sources> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

记住要执行的Maven - 更新项目

+1

+1使用官方JAXB插件。 https://jaxb.java.net/ https://java.net/projects/maven-jaxb2-plugin/pages/Home – Puce 2014-09-24 08:04:10

+0

它不起作用 – Unyx 2014-09-24 08:16:19

+0

奇怪的是,我在一个基于这个插件的建筑群中工作。看到这个[链接](http://confluence.highsource.org/display/MJIIP/User+Guide#UserGuide-Separateschemacompilation)分开编译和[this](http://confluence.highsource.org/display/MJIIP/用户+指南#UserGuide-Resourceentries)引用资源。 – Xstian 2014-09-24 08:24:29