简体   繁体   中英

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?

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.

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>

To generate collection setters you have to add to jaxb plugin dependencies and for value constuctor generation - .添加到 jaxb 插件依赖项和值构造函数生成 - You also have to add proper args to argument list ( and . 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>
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM