9

I am generating POJOs from XSD schemas using the maven-jab2-plugin. My generated classes don't have setters for any fields that are collections. How do I generate setters for collections?

Can anyone explain the reasoning for setters to not be enabled by default?

PotataChipz
  • 505
  • 5
  • 11

2 Answers2

10

Use the Setters plugin included in JAXB2-Basics, as documented here.

I've copy-pasted their usage example (and modified it to show setters specifically):

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.7.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <args>
            <arg>-Xsetters</arg>
        </args>
        <plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version><!-- Current version --></version>
            </plugin>
        </plugins>
    </configuration>
</plugin>

And include the JAXB2 Basics Runtime package in your dependencies:

<dependency>
    <groupId>org.jvnet.jaxb2_commons</groupId>
    <artifactId>jaxb2-basics-runtime</artifactId>
    <version><!-- Current version --></version>
</dependency>
PotataChipz
  • 505
  • 5
  • 11
  • After spending too long googling, I am asking/answering to document the answer. I could find lots of references to the xjc collection-setter-injector plugin, but couldn't seem to get anything working. (I would just get "Unrecognized parameter -Xcollection-setter-injector" and never figured out how I was supposed to include/point to the correct dependency.) JAXB2-Basics worked with no hassle. – PotataChipz Nov 11 '12 at 20:29
  • This plugin interferes with [com.github.jaxb-xew-plugin](https://github.com/dmak/jaxb-xew-plugin/issues/16) (or vice versa)... – Lukas Eder Dec 30 '13 at 22:09
1

For generating setter for collection I have found only a solution that works for me. You have to add dependency to org.andromda.thirdparty.jaxb2_commons. However, this solution works for jaxb2-maven-plugin version 2.5.0, for version 2.3.1 doesn't work. Here's an example:

 <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>jaxb2-maven-plugin</artifactId>
    <version>2.5.0</version>
    <dependencies>
      <dependency>
         <groupId>org.andromda.thirdparty.jaxb2_commons</groupId>
         <artifactId>collection-setter-injector</artifactId>
         <version>1.0</version>
      </dependency>
   </dependencies>
   <executions>
      <execution>
             ......
       </execution>
    </executions>
    <configuration>
       <sources>
             ......
       </sources>
       <arguments>-Xcollection-setter-injector</arguments>
       <clearOutputDir>false</clearOutputDir>
       <extension>true</extension>
    </configuration>
</plugin>
walthor
  • 96
  • 3