jaxb,xsd导入无法识别

时间:2014-09-24 07:53:06

标签: java maven jaxb xsd maven-jaxb2-plugin

我的jaxb插件存在很大问题。我有一个项目A,我有src / main / resources / xsd / common.xsd文件。在这个项目中,我使用cxf-xjc-plugin 生成java类。我还有我的episod文件在src / main / resources / META-INF下名为sun-jaxb.episode

接下来,我有项目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"

在项目B中,我有一个带有jaxb插件的pom文件

<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>

接下来,我在项目B

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

我使用common.xsd

中的类型定义的文件

我有xmlns:cmns =&#34; http://www.some_path" //它与catalog.txt中的相同  和

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

但问题是我得到错误未定义简单或复杂类型,因为它不识别cmns

1 个答案:

答案 0 :(得分: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>

项目A的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>

您必须在项目B

之前执行项目A的mvn安装

添加此插件以添加到源目标生成的类

          <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 - 更新项目