Generating hashCode() and equals() when creating Java classes using Mojo Jaxb2 maven plugin
Asked Answered
A

7

11

The code I'm working on is using jaxb2-maven-plugin from org.codehaus.mojo to generate Java classes from XSD schema. I'm looking for a way to automatically implement equals() and hashCode() methods for those classes, but it seems there is not a way. I know that there are other JAXB2 Maven plugins that do that (http://confluence.highsource.org/display/J2B/Home for example), but I was wondering if anyone of you encountered this issue before and if there's a way for fixing it. I'm generating the classes using the xjc goal.

Armhole answered 30/1, 2012 at 10:37 Comment(0)
L
18

JAXB2 Basics you're mentioning is not a property of maven-jaxb2-plugin, it is a standalone set of JAXB 2.x plugins you can use with XJC - or jaxb2-maven-plugin or whatever.

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.3.1</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
           </executions>
           <configuration>
                <arguments>
                     <argument>-Xequals</argument>
                     <argument>-XhashCode</argument>
                </arguments>
           </configuration>
           <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.12.0</version>
                </dependency>
           </dependencies>
       </plugin>

What I wanted to ask - why not just use maven-jaxb2-plugin? It has so much more functionality compared to the Codehaus plugin - including configuration support for JAXB2 plugins.

Lauricelaurie answered 3/2, 2012 at 22:58 Comment(9)
I went for it actually! Thanks :)Armhole
Are you sure this is working? I get "Failed to execute goal org.codehaus.mojo:jaxb2-maven-plugin:1.5:xjc (xjc) on project XXX: unrecognized parameter -Xequals" OR "Could not find artifact" depending on the versions I use. As for the other plugin all links towards it are broken.Geoponic
@Geoponic No, I'm not sure about this specific XML, only that jaxb2-basics do not depend on any specific Maven plugin. I only use maven-jaxb2-plugin. The plugin is hosted in the central Maven repo so it is surely available: mvnrepository.com/artifact/org.jvnet.jaxb2.maven2/… I'm restarting the docs server.Lauricelaurie
@Geoponic Make sure you wrap each argument in its own element: <arguments><argument>-Xequals</argument><argument>-XhashCode</argument></arguments>Elsie
@Elsie Thank you, I've improved the answer.Lauricelaurie
@Lauricelaurie Any reason the equals and hashcode plugins do not offer the option to use java 7 Objects methods ?Boilermaker
@JonathanDrapeau equals and hashCode plugins generate "strategic" methods where you can pass equals/hashCode strategies from the outside. Objects don't do this.Lauricelaurie
@Lauricelaurie I understood that but offering the option to use Objects methods when you don't need special strategies would be niceBoilermaker
@JonathanDrapeau There are non-strategic simpleHashCode and simpleEquals plugins as well.Lauricelaurie
M
6

This is the easiest way to do. Please update version as per your requirements.

<plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>jaxb2-maven-plugin</artifactId>
                    <version>1.5</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.jvnet.jaxb2_commons</groupId>
                            <artifactId>jaxb2-commons-lang</artifactId>
                            <version>2.3</version>
                        </dependency>
                    </dependencies>
                    <executions>
                        <execution>
                            <id>JAXB generate content classes</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>xjc</goal>
                            </goals>
                            <configuration>
                                <schemaDirectory>${project.basedir}/src/main/resources/schema</schemaDirectory>
                                <outputDirectory>${project.build.directory}/generated-sources/jaxb</outputDirectory>
                                <schemaFiles>**/*.xsd</schemaFiles>
                                <packageName>com.lexus.web.content.model</packageName>
                                <arguments>-Xcommons-lang</arguments>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
Mccain answered 29/10, 2013 at 15:41 Comment(0)
P
5

I would strongly disagree with using JAXB generated classes as business objects in your code. The classes that are generated by JAXB are beans that are just meant to essentially move element information from the xml file, to the bean's fields. I personally always have my SOAP service convert these generated bean classes to my actual business objects, as XML->Java and vice versa conversion is not black and white all the time. Note that this is my personal opinion, and would love for some others to weigh in on what they do in this situation.

To answer your question though, use a different plug in, or just your use your IDE to make some equals and hashCode methods.

Hope that helps.

EDIT:

I forgot to put my reasoning for this, apologies. Let us say in the next version of your project you want to add some more behavior to your JAXB generated classes, but also want to make some changes to your schema. Now you are regenerating the JAXB generated classes, putting the old behaviors back in, and making your application far more susceptible to bugs in my opinion. The JAXB generated classes are supposed to reflect your XML schema types (and thus your SOAP messages) not your business logic. Hope that makes sense.

Pasco answered 30/1, 2012 at 18:38 Comment(3)
I agree with that, and we do have separate Business Objects. The main reason I would need that is for writing unit tests, but thanks for the tip however!Armhole
manub: You can use Apache Commons Lang's EqualsBuilder.reflectionEquals to compare your JAXB generated classes without adding hashCode and equals through a Maven plugin.Pearle
@ChrisEineke With all the performance penalties of reflection.Lauricelaurie
E
2

You might also want to consider using a fluent builder interface (facilitates the handling of the generated JAXB classes) and a reference to a catalog file in case you are referencing an xmldsig-core-schema (speeds up the generation process, as no remote Schemas from W3C are queried - their servers usually delay the response).

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>1.5</version>
            <dependencies>
                <dependency>
                    <groupId>net.java.dev.jaxb2-commons</groupId>
                    <artifactId>jaxb-fluent-api</artifactId>
                    <version>2.0.1</version>
                    <exclusions>
                        <exclusion>
                            <groupId>com.sun.xml</groupId>
                            <artifactId>jaxb-xjc</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-basics</artifactId>
                    <version>0.6.4</version>
                    <exclusions>
                        <exclusion>
                            <groupId>com.sun.xml</groupId>
                            <artifactId>jaxb-xjc</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>${basedir}/src/main/resources/xsd</schemaDirectory>
                <packageName>[your package name goes here]</packageName>
                <outputDirectory>${build.directory}/generated/src/main/java</outputDirectory>
                <bindingDirectory>${basedir}/src/main/resources/xsd</bindingDirectory>
                <extension>true</extension>
                <catalog>${basedir}/src/main/resources/xsd-catalog/catalog.cat</catalog>
                <extension>true</extension>
                <arguments>-Xfluent-api -Xequals -XhashCode -XtoString</arguments>
            </configuration>
        </plugin>

Here is how the catalog file looks like:

--
  sample catalog file.

  double hyphens are used to begin and end a comment section.
--

SYSTEM "http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/xmldsig-core-schema.xsd" "xmldsig-core-schema.xsd"

And here is the link to the xmldisg-core-schema.xsd: http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd

Please note that the link is not broken - the W3C server is just delaying the response a few seconds. Can me very cumbersome if that happens during your automated build process, which includes JAXB generation.

Ezzell answered 9/10, 2012 at 16:33 Comment(0)
C
1

My answer is for those who can't afford any third part dependency on their generated code.

The org.jvnet.jaxb2_commons:jaxb2-basics plugin adds a lots of org.jvnet includes in the generated code and your project will depend on org.jvnet.jaxb2_commons:jaxb2-basics-runtime.

The org.andromda.thirdparty.jaxb2_commons:commons-lang-plugin plugin generates code which depends on commons-lang:commons-lang. This dependency might be easier to bear depending on the situation.

I finally found this source code, it might be included in com.sun.xml.bind:jaxb-xjc at some point (version 2.2.4 ?), but until then I can't see any other solution than writing your own plugin.

PS: I can't access confluence.highsource.org, I get a 404. I guess it might have been helpful.

PPS: In my situation, the application I am building is targeted to an environment which has a very limited set of allowed Java libraries (incongruous security restrictions). I was a bit disappointed by the fact that jaxb2_commons includes a bunch of org.jvnet dependencies in the generated source, just for adding a boring equals method. I can understand the strategy of jvnet, their tools are very powerful, I may just be trying and using a sledgehammer to crack a nut. Still, I was sorry to note that it was so difficult to find a suitable tool for my situation.

Codex answered 4/9, 2013 at 10:2 Comment(1)
it is important to say that the option to use with andromda plugin is -Xcommons-lang.Moberg
V
1

There is also a fluent-builder plugin for JAXB that doesn't necessarily generate dependencies to third-party code. Plus, it is a true "Builder" design pattern, whereas the "fluent-api" plugin just adds builder-like methods to the generated classes. It is: https://github.com/mklemm/jaxb2-rich-contract-plugin It also has options to make the generated classes immutable and it can generate methods to copy the state of existing objects into a new builder instance, also supporting "partial" object copy.

Venusian answered 23/2, 2015 at 10:33 Comment(0)
S
1

For me the simplest way to do is using maven-jaxb2-plugin (as mentioned in the accepted answer):

  1. Add in pom.xml <dependencies>
<dependency> 
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics</artifactId>
    <version>0.12.0</version>
</dependency>
  1. Add the plugin
<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.14.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <schemaDirectory>src/main/resources</schemaDirectory>
                <generateDirectory>target/generated-sources</generateDirectory>
                <generatePackage>my.package</generatePackage>
            </configuration>
        </execution>
    </executions>
    <configuration>
        <extension>true</extension>
        <args>
            <arg>-XtoString</arg>
            <arg>-Xequals</arg>
            <arg>-XhashCode</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.12.0</version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

See

Supernational answered 31/7, 2019 at 12:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.