Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 544
PotataChipz Avatar asked Nov 11 '12 20:11

PotataChipz


2 Answers

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>
like image 92
PotataChipz Avatar answered Nov 20 '22 14:11

PotataChipz


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>
like image 1
walthor Avatar answered Nov 20 '22 13:11

walthor