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?
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>
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With