'Forcing jaxb2-maven-plugin to generate setters or constructors

I have a following build configuration. It is working properly, however the problem is in case of generating constructor with all args or generating setters for list.

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>


            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <id>xjc</id>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                    <clearOutputDir>false</clearOutputDir>
                    <sources>
                        <source>src/main/resources/xsd</source>
                    </sources>
                </configuration>
            </plugin>
        </plugins>
    </build>

Can you tell me how to force xjc to generate setters for lists or args-constructors ?



Solution 1:[1]

This thing works for me

<configuration>
    <args>
        <arg>-Xvalue-constructor</arg>
    </args>
    <plugins>
        <plugin>
           <groupId>org.jvnet.jaxb2_commons</groupId>
           <artifactId>jaxb2-value-constructor</artifactId>
           <version>3.0</version>
        </plugin>
        <plugin>
            <groupId>org.jvnet.jaxb2_commons</groupId>
            <artifactId>jaxb2-basics</artifactId>
            <version>0.11.1</version>
        </plugin>
    </plugins>
</configuration>

Let me know if you find any issues with this.

Solution 2:[2]

For generating setter for the collection I added the dependency to org.andromda.thirdparty.jaxb2_commons but it' works only for the new version of jaxb2-maven-plugin. I tried with 2.5.0 and it's works, using the version 2.3.1 doesn't work. Here 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>

Solution 3:[3]

To generate collection setters you have to add collection-setter-injector to jaxb plugin dependencies and for value constuctor generation - jaxb2-value-constructor. You also have to add proper args to argument list (-Xcollection-setter-injector and -Xvalue-constructor. Example plugin configuration in pom.xml:

...
<build>
        <plugins>
            <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>
                    <dependency>
                        <groupId>org.jvnet.jaxb2_commons</groupId>
                        <artifactId>jaxb2-value-constructor</artifactId>
                        <version>3.0</version>
                    </dependency>
                </dependencies>
                <executions>
                    ...
                </executions>
                <configuration>
                    <sources>
                        ...
                    </sources>
                    <clearOutputDir>true</clearOutputDir>
                    <arguments>
                        <argument>-Xcollection-setter-injector</argument>
                        <argument>-Xvalue-constructor</argument>
                    </arguments>
                    <extension>true</extension>
                </configuration>
            </plugin>
        </plugins>
    </build>
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mitul bhatnagar
Solution 2 walthor
Solution 3 Jakub Mrozi?ski