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?

4

2 回答 2

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>
于 2012-11-11T20:28:18.480 回答
1

为了为集合生成 setter,我发现只有一个适合我的解决方案。您必须将依赖项添加到org.andromda.thirdparty.jaxb2_commons。但是,此解决方案适用于jaxb2-maven-plugin版本2.5.0,因为版本2.3.1不起作用。这是一个例子:

 <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>
于 2021-05-07T16:17:23.097 回答