如何使JAXB注释并提供equlas / hash代码实现

时间:2015-05-26 13:29:07

标签: java xml jaxb

我正在尝试使用JAXB从WSDL生成类,这些类使用Jsr303Annotations进行批注,并提供equals和hashcode的实现。

这两个都是分开工作,但是当我试图达到这两个要求时,我得到错误:

在:"构建工作区"期间发生内部错误。 com.sun.tools.xjc.Plugin:Provider org.jvnet.jaxb2_commons.plugin.fixjaxb1058.FixJAXB1058Plugin无法实例化:java.lang.NoClassDefFoundError:org / jvnet / jaxb2_commons / reflection / util / Accessor

有人可以告诉我如何配置JAXB来完成这两项工作吗?

贝娄是我尝试这样做的:

<plugin>
                <groupId>org.jvnet.jaxb2.maven2</groupId>
                <artifactId>maven-jaxb2-plugin</artifactId>
                <executions>
                    <execution>
                        <id>generate-wsdl</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <schemaDirectory>src/main/resources</schemaDirectory>
                            <schemaIncludes>
                                <include>*.wsdl</include>
<!--                                <include>*.xsd</include> -->
                            </schemaIncludes>
<!--                            <forceRegenerate>true</forceRegenerate> -->
                            <!-- <removeOldOutput>true</removeOldOutput> -->
                            <!-- <cleanPackageDirectories>true</cleanPackageDirectories> -->
                            <strict>true</strict>
                            <extension>true</extension>
                            <args>
                                <arg>-XJsr303Annotations</arg>
                                <arg>-Xannotate</arg>
                                <arg>-Xequals</arg>
                                <arg>-XhashCode</arg>
                            </args>
                            <plugins>
                                <plugin>
                                <groupId>org.jvnet.jaxb2_commons</groupId>
                                <artifactId>jaxb2-basics-annotate</artifactId>
                                <version>0.6.3</version>
                                </plugin>
                                <plugin>
                                    <groupId>org.jvnet.jaxb2_commons</groupId>
                                    <artifactId>jaxb2-basics</artifactId>
                                    <version>0.9.4</version>
                                </plugin>
                                <plugin>
                                    <groupId>com.github.krasa</groupId>
                                    <artifactId>krasa-jaxb-tools</artifactId>
                                    <version>1.1</version>
                                </plugin>
                            </plugins>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

编辑:我尝试使用的相关依赖项:

        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-runtime</artifactId>
            <version>0.9.4</version>
        </dependency>

<!--        <dependency> -->
<!--            <groupId>org.jvnet.jaxb2_commons</groupId> -->
<!--            <artifactId>jaxb2-basics-annotate</artifactId> -->
<!--            <version>1.0.0</version> -->
<!--        </dependency> -->


<!--        <dependency> -->
<!--        <groupId>org.jvnet.jaxb2_commons</groupId> -->
<!--        <artifactId>jaxb2-commons-lang</artifactId> -->
<!--        <version>2.3</version> -->
<!--        </dependency> -->

只有equals / hash代码的第一个依赖项和插件的组合才会产生结果。

如果我包含其他2个依赖项,那么我会在项目中为每个依赖项丢失artifact -id消息。

注释插件在没有显式依赖项的情况下工作,在没有equals / hashcode插件的情况下使用,反之亦然。

编辑:最终解决方案以防任何人遇到同样的问题

因此需要将注释和equals / hash代码插件分离为单独的执行,这解决了所有问题。

3 个答案:

答案 0 :(得分:2)

找到解决方案。这个插件需要另外一个依赖:

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.12.3</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>                             
        <generatePackage>com.argonio.fias.entity</generatePackage>                       
        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
        <readOnly>true</readOnly>
        <removeOldOutput>true</removeOldOutput>
        <verbose>true</verbose>
        <extension>true</extension>
        <args>
            <arg>-no-header</arg>
            <arg>-Xxew</arg>
            <arg>-Xxew:instantiate lazy</arg>
            <arg>-Xxew:plural</arg>
            <arg>-Xequals</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>com.github.jaxb-xew-plugin</groupId>
                <artifactId>jaxb-xew-plugin</artifactId>
                <version>1.5</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.9.4</version>
            </plugin>
        </plugins>
    </configuration>
    <dependencies>
        <!-- This dependency is required to execute the plugin -->
        <dependency>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics-tools</artifactId>
            <version>0.9.4</version>
        </dependency>
    </dependencies>
</plugin>

答案 1 :(得分:0)

尝试添加此依赖项。顺便说一下,你的0.6.3版似乎很低。

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-commons-lang</artifactId>
    <version>2.3</version>
</dependency>

答案 2 :(得分:0)

@Viacheslav的回答是正确的。但请注意,您还需要为项目添加依赖项:

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-runtime</artifactId>
    <version>0.9.4</version> <!-- same as jaxb2-basics plugin version -->
</dependency>     

有关所有详细信息,请参阅http://confluence.highsource.org/display/J2B/JAXB2+Basics+Plugins