jaxb、xsd 导入无法识别

时间:2023-01-12
本文介绍了jaxb、xsd 导入无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a big problem with jaxb plugin. I have a project A where I have src/main/resources/xsd/common.xsd file. In this project I use cxf-xjc-plugin to generate java classes. I have also my episod file under src/main/resources/META-INF called sun-jaxb.episode

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

Next, I have project B which has maven dependency to project A. In this project I have 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 文件

In project B I have pom file with jaxb plugin

<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 的类型定义的文件

file where I use type definitions from common.xsd

我有 xmlns:cmns="http://www.some_path"//和 catalog.txt 中的一样和

I have xmlns:cmns="http://www.some_path" // it is the same as in catalog.txt and

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

但问题是我收到错误 undefined simple or complex type ,因为它无法识别 cmns

but the problem is that I get error undefined simple or complex type , because it doesn't recognize cmns

推荐答案

我用这个插件解决xsd的public import

I use this plugin to solve the public import of the 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 安装

You must perform a mvn install of project A before the project B

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

Add this plugin to add to source target generated classes

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

Remember to perform Maven - update project

这篇关于jaxb、xsd 导入无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:如何创建通用 XSD 类型 下一篇:使用 Java 代码进行 XML 验证

相关文章

最新文章