When generating java sources with maven-jaxb2-plugin, how to enable setters for collections?

PotataChipz picture PotataChipz · Nov 11, 2012 · Viewed 7k times · Source

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?

Answer

PotataChipz picture PotataChipz · Nov 11, 2012

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>