简体   繁体   English

无法在Maven环境中从JAXB生成类

[英]Unable to generate classes from JAXB in Maven environment

I'm using the xjc plugin in Maven environment and trying to generate classes from the Schema I'm able to do this (Create classes) using xjc from command line, but unable to do the same using maven target generate-sources. 我在Maven环境中使用xjc插件,并尝试从Schema生成类,我能够从命令行使用xjc做到这一点(创建类),但是无法使用maven target generate-sources来做到这一点。
Getting the following exception 得到以下异常

[ERROR] null[5,30]
org.xml.sax.SAXParseException: A class/interface with the same name "<className>" is already in use. Use a class customization
to resolve this conflict.
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:100)
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:61)
        at com.sun.tools.xjc.generator.bean.ImplStructureStrategy$1.createClasses(ImplStructureStrategy.java:42)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generateClassDef(BeanGenerator.java:371)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.getClazz(BeanGenerator.java:403)
        at com.sun.tools.xjc.generator.bean.BeanGenerator$1.onBean(BeanGenerator.java:291)
        at com.sun.tools.xjc.generator.bean.BeanGenerator$1.onBean(BeanGenerator.java:299)
        at com.sun.tools.xjc.model.CClassInfo.accept(CClassInfo.java:352)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.getContainer(BeanGenerator.java:281)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.getUsedPackages(BeanGenerator.java:337)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:169)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:151)
        at com.sun.tools.xjc.model.Model.generateCode(Model.java:230)
        at com.sun.tools.xjc.Driver.run(Driver.java:317)
        at org.codehaus.mojo.jaxb2.XjcMojo.execute(XjcMojo.java:301)
        at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPluginManager.java:490)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(DefaultLifecycleExecutor.java:694)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLifecycle(DefaultLifecycleExecutor.java:556)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(DefaultLifecycleExecutor.java:535)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHandleFailures(DefaultLifecycleExecutor.java:387)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegments(DefaultLifecycleExecutor.java:348)
        at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLifecycleExecutor.java:180)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:328)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:138)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:362)
        at org.apache.maven.cli.compat.CompatibleMain.main(CompatibleMain.java:60)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
        at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
        at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
        at org.codehaus.classworlds.Launcher.main(Launcher.java:375)

Can anyone help me find whats missing here. 谁能帮我找到这里缺少的内容。

As stated on the JAXB site, use the following Maven plugin: 如JAXB网站上所述,使用以下Maven插件:

        <plugin>
.           <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

http://java.net/projects/maven-jaxb2-plugin/pages/Home http://java.net/projects/maven-jaxb2-plugin/pages/Home

http://jaxb.java.net/ http://jaxb.java.net/

It seems, you're using a different plugin (org.codehaus.mojo.jaxb2.XjcMojo on your stack trace for which Google leads me to http://mojo.codehaus.org/jaxb2-maven-plugin/ ). 看来,您使用的是其他插件(堆栈跟踪上的org.codehaus.mojo.jaxb2.XjcMojo,Google将其引导至http://mojo.codehaus.org/jaxb2-maven-plugin/ )。

Since it works command line but not in Maven the class with the conflict must appear somewhere on your classpath. 由于它可以在命令行中运行,但不能在Maven中运行,因此具有冲突的类必须出现在类路径上。 Any chance that is happening? 有什么机会吗?

For an example on how to resolve name conflicts see: 有关如何解决名称冲突的示例,请参见:

May be you have two sub-elements with the same name, so the generated class name is the same? 可能您有两个具有相同名称的子元素,因此生成的类名称是否相同?

If this is the case, you can customized the produced class name using the schema annotations: 在这种情况下,您可以使用模式注释自定义产生的类名称:

First add the following namespaces to your xsd: 首先将以下名称空间添加到您的xsd中:

<xs:schema xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0">

Second add annotation for the relevant element: 第二个为相关元素添加注释:

<xs:complexType name="ComplexType">
    <xs:annotation><xs:appinfo>
       <jaxb:class name="MyClass"> 
           <jaxb:javadoc>This is my class.</jaxb:javadoc>
       </jaxb:class>
    </xs:appinfo></xs:annotation>
</xs:complexType>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM